forked from metabrainz/musicbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.sh
executable file
·147 lines (115 loc) · 5.27 KB
/
upgrade.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -u
set -o errexit
MB_SERVER_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd "$MB_SERVER_ROOT"
source admin/config.sh
: ${DB_SCHEMA_SEQUENCE:=$(perl -Ilib -e 'use DBDefs; print DBDefs->DB_SCHEMA_SEQUENCE;')}
: ${REPLICATION_TYPE:=$(perl -Ilib -e 'use DBDefs; print DBDefs->REPLICATION_TYPE;')}
: ${DATABASE:=MAINTENANCE}
: ${SKIP_EXPORT:=0}
NEW_SCHEMA_SEQUENCE=27
OLD_SCHEMA_SEQUENCE=$((NEW_SCHEMA_SEQUENCE - 1))
RT_MASTER=1
RT_MIRROR=2
RT_STANDALONE=3
SQL_DIR='./admin/sql/updates/schema-change'
EXTENSIONS_SQL="$SQL_DIR/$NEW_SCHEMA_SEQUENCE.extensions.sql"
MASTER_ONLY_SQL="$SQL_DIR/$NEW_SCHEMA_SEQUENCE.master_only.sql"
MIRROR_ONLY_SQL="$SQL_DIR/$NEW_SCHEMA_SEQUENCE.mirror_only.sql"
################################################################################
# Assert pre-conditions
if [ "$DB_SCHEMA_SEQUENCE" != "$OLD_SCHEMA_SEQUENCE" ]
then
echo `date` : Error: Schema sequence must be $OLD_SCHEMA_SEQUENCE when you run this script
exit -1
fi
################################################################################
# Backup and disable replication triggers
if [ "$REPLICATION_TYPE" = "$RT_MASTER" ]
then
if [[ "$SKIP_EXPORT" == "0" ]]
then
echo `date` : Export pending db changes
./admin/RunExport
fi
echo `date` : 'Drop replication triggers (musicbrainz)'
./admin/psql "$DATABASE" < ./admin/sql/DropReplicationTriggers.sql
./admin/psql "$DATABASE" < ./admin/sql/DropReplicationTriggers2.sql
for schema in caa documentation eaa statistics wikidocs
do
echo `date` : "Drop replication triggers ($schema)"
./admin/psql "$DATABASE" < ./admin/sql/$schema/DropReplicationTriggers.sql
./admin/psql "$DATABASE" < ./admin/sql/$schema/DropReplicationTriggers2.sql
done
fi
if [ "$REPLICATION_TYPE" != "$RT_MIRROR" ]
then
echo `date` : Disabling last_updated triggers
OUTPUT=`./admin/psql --system "$DATABASE" < ./admin/sql/DisableLastUpdatedTriggers.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
fi
################################################################################
# Scripts that should run on *all* nodes (master/mirror/standalone)
echo `date` : 'Running upgrade scripts for all nodes'
if [ -e "$EXTENSIONS_SQL" ]
then
./admin/psql --system "$DATABASE" < "$EXTENSIONS_SQL" || exit 1
fi
./admin/psql "$DATABASE" < $SQL_DIR/${NEW_SCHEMA_SEQUENCE}.mirror.sql || exit 1
################################################################################
# Migrations that apply for only masters
if [ "$REPLICATION_TYPE" = "$RT_MASTER" ]
then
if [ -e "$MASTER_ONLY_SQL" ]
then
echo `date` : 'Running upgrade scripts for master nodes'
./admin/psql "$DATABASE" < "$MASTER_ONLY_SQL" || exit 1
fi
fi
################################################################################
# Migrations that apply for only mirrors
if [ "$REPLICATION_TYPE" = "$RT_MIRROR" ]
then
if [ -e "$MIRROR_ONLY_SQL" ]
then
echo `date` : 'Running upgrade scripts for mirror nodes'
./admin/psql "$DATABASE" < "$MIRROR_ONLY_SQL" || exit 1
fi
fi
################################################################################
# Add constraints that apply only to master/standalone (FKS)
if [ "$REPLICATION_TYPE" != "$RT_MIRROR" ]
then
echo `date` : 'Running upgrade scripts for master/standalone nodes'
./admin/psql "$DATABASE" < $SQL_DIR/${NEW_SCHEMA_SEQUENCE}.standalone.sql || exit 1
echo `date` : Enabling last_updated triggers
OUTPUT=`./admin/psql --system "$DATABASE" < ./admin/sql/EnableLastUpdatedTriggers.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
fi
################################################################################
# Re-enable replication
if [ "$REPLICATION_TYPE" = "$RT_MASTER" ]
then
echo `date` : 'Refreshing dbmirror2.column_info'
OUTPUT=`./admin/psql --system "$DATABASE" < ./admin/sql/dbmirror2/RefreshColumnInfo.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
echo `date` : 'Create replication triggers (musicbrainz)'
OUTPUT=`./admin/psql "$DATABASE" < ./admin/sql/CreateReplicationTriggers.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
OUTPUT=`./admin/psql "$DATABASE" < ./admin/sql/CreateReplicationTriggers2.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
for schema in caa documentation eaa statistics wikidocs
do
echo `date` : "Create replication triggers ($schema)"
OUTPUT=`./admin/psql "$DATABASE" < ./admin/sql/$schema/CreateReplicationTriggers.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
OUTPUT=`./admin/psql "$DATABASE" < ./admin/sql/$schema/CreateReplicationTriggers2.sql 2>&1` || ( echo "$OUTPUT" ; exit 1 )
done
fi
################################################################################
# Bump schema sequence
echo `date` : Going to schema sequence $NEW_SCHEMA_SEQUENCE
echo "UPDATE replication_control SET current_schema_sequence = $NEW_SCHEMA_SEQUENCE;" | ./admin/psql "$DATABASE"
# ignore superuser-only vacuum tables
echo `date` : Vacuuming DB.
echo "SET statement_timeout = 0; VACUUM ANALYZE;" | ./admin/psql MAINTENANCE 2>&1 | grep -v 'only superuser can vacuum it'
################################################################################
# Prompt for final manual intervention
echo `date` : Done
echo `date` : UPDATE THE DB_SCHEMA_SEQUENCE IN DBDefs.pm TO $NEW_SCHEMA_SEQUENCE !
# eof