-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.sh
executable file
·333 lines (277 loc) · 7.17 KB
/
migrate.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/sh
# Get information for database: access, driver, queries, etc.
. ./.env.sh
parse_up_down_args()
{
count=1
yes=''
for arg in "$@"; do
if [ "$arg" = "$arg_yes_short" ] || [ "$arg" = "$arg_yes_long" ]; then
yes=1
elif [ "$arg" -gt 1 ] 2>/dev/null; then
count=$arg
fi
done
}
find_one_file()
{
# Find the files that match either of the 2 patterns
pattern=$1
count=`find $migration_sql_dir -type f -name $pattern -exec printf %c {} + | wc -c`
file=`find $migration_sql_dir -type f -name $pattern`
# Error if there are more than 1
if [ $count -gt 1 ]; then
echo "Error: too many files for the migration name $name"
echo "find $migration_sql_dir -type f -name $pattern"
exit
fi
# Echo the only name
echo $file
}
color_end='\033[0m'
echo_header()
{
echo -e "$color_header$1$color_end"
}
echo_migration_up()
{
echo -e "Migration name: $color_up$1$color_end"
}
echo_migration_down()
{
echo -e "Migration name: $color_down$1$color_end"
}
confirm_migration()
{
choice=''
while [ "$choice" != 'y' ]; do
printf 'Are you sure? (y/m/n) '
read choice
if [ "$choice" = 'n' ]; then
echo 'Cancelled'
exit
elif [ "$choice" = 'm' ]; then
cat $file
fi
done
}
init()
{
query0=`query_create_user`
query1=`query_create_database`
query2=`query_create_migrations_table`
me=`whoami`
if [ $me = 'root' ]; then
echo_header 'Creating user'
su $admin -c "$driver $driver_cmd \"$query0\""
echo
echo_header 'Creating database'
su $admin -c "$driver $driver_cmd \"$query1\""
echo
echo_header 'Creating migrations table'
su $admin -c "$driver $driver_db $database $driver_cmd \"$query2\""
elif [ $me = "$admin" ]; then
echo_header 'Creating user'
$driver $driver_cmd "$query0"
echo
echo_header 'Creating database'
$driver $driver_cmd "$query1"
echo
echo_header 'Creating migrations table'
$driver $driver_db $database $driver_cmd "$query2"
else
echo_header 'Creating user and database'
echo "Run as $admin/root to create a user and a database"
echo
echo_header 'Creating migrations table'
$driver $driver_db $database $driver_cmd "$query2"
fi
}
migration_status()
{
echo_header 'Migration status'
# Get the status of migrations
query=`query_select_all_for_migration_status`
$driver $driver_db $database $driver_cmd "$query"
}
migrate_up()
{
echo_header 'Migrating up'
# Get name of migration
query=`query_select_name_for_migration_up`
name=`$driver $driver_db $database $driver_only_results $driver_cmd "$query"`
if [ $? -ne 0 ]; then
exit
fi
# Finished if all migrations are up
if [ "$name" = '' ]; then
echo 'All migrations are up'
exit
fi
# Get path of the SQL file
file=`find_one_file *-$name-up.sql`
# Get confirmation
echo_migration_up $name
if ! [ "$yes" ]; then
confirm_migration
fi
# Run the SQL file
$driver $driver_db $database $driver_file "$file"
if [ $? -ne 0 ]; then
exit
fi
echo
echo_header 'Marking the migration as up'
# Mark the migration as up
query=`query_update_for_migration_up $name`
$driver $driver_db $database $driver_cmd "$query"
}
migrate_down()
{
echo_header 'Migrating down'
# Get name of migration
query=`query_select_name_for_migration_down`
name=`$driver $driver_db $database $driver_only_results $driver_cmd "$query"`
# Finished if all migrations are down
if [ "$name" = '' ]; then
echo 'All migrations are down'
exit
fi
# Get path of the SQL file
file=`find_one_file *-$name-down.sql`
# Get confirmation
echo_migration_down $name
if ! [ "$yes" ]; then
confirm_migration
fi
# Run the SQL file
$driver $driver_db $database $driver_file "$file"
if [ $? -ne 0 ]; then
exit
fi
echo
echo_header 'Marking the migration as down'
# Mark the migration as down
query=`query_update_for_migration_down $name`
$driver $driver_db $database $driver_cmd "$query"
}
sort_migrations_by_prefix_and_get_names()
{
pattern=*-*-up.sql
find $migration_sql_dir -type f -name $pattern | rev | cut -d / -f 1 | rev | sort | rev | cut -d - -f 2 | rev
}
create_all_migrations()
{
echo_header 'Making sure that all SQL migration names are in the database'
total=0
for name in `sort_migrations_by_prefix_and_get_names`; do
query=`query_insert_migration $name`
result=`$driver $driver_db $database $driver_cmd "$query" 2> /dev/null | cut -d ' ' -f 3`
if [ "$result" ] && [ $result -eq 1 ]; then
total=$(($total + 1))
echo "Inserted $name"
fi
done
echo "Inserted $total names of existing SQL migration files"
}
create_migration()
{
echo_header 'Inserting row into migrations table'
# Insert into database
name=$1
query=`query_insert_migration $name`
$driver $driver_db $database $driver_cmd "$query"
echo
echo_header 'Creating files for migration'
# Error if file exists
up=`find_one_file *-$name-up.sql`
down=`find_one_file *-$name-down.sql`
if [ "$up" ] || [ "$down" ]; then
echo 'Error: an SQL file with that migration name already exists'
if [ "$up" ]; then echo $up; fi
if [ "$down" ]; then echo $down; fi
exit
fi
# Create files
date=`date +%Y-%m-%d-%H-%M-%S`
up="$migration_sql_dir$date-$name-up.sql"
down="$migration_sql_dir$date-$name-down.sql"
touch $up
touch $down
echo $up
echo $down
}
help()
{
echo_header 'Migration help'
echo
echo ' Usage:'
echo ' ./migrate.sh MIG_NAME'
echo ' ./migrate.sh up EXTRA_ARGS'
echo ' ./migrate.sh down EXTRA_ARGS'
echo
echo ' Examples:'
echo ' ./migrate.sh init'
echo ' ./migrate.sh create_users_table'
echo ' ./migrate.sh up'
echo ' ./migrate.sh up 2'
echo ' ./migrate.sh down 3 yes'
echo ' ./migrate.sh status'
echo
echo ' Primary Arguments:'
echo ' init: Initialize the database and create the migrations table'
echo
echo ' MIG_NAME: Create a migration (usually snake_case) with the given migration name'
echo
echo ' up: Use a migration to change the database'
echo ' down: Undo a migration to change the database back'
echo
echo ' status: Get the status of all migrations'
echo
echo ' Extra Arguments (EXTRA_ARGS):'
echo ' yes: Ignore confirmation for each migration (dangerous)'
echo ' INTEGER: Instead of only 1 migration, migrate up/down many times'
echo
echo ' Tips:'
echo ' Folders: Organize SQL files into folders if you want to'
}
# Main: parse arguments
if [ "$1" = "$arg_init_short" ] || [ "$1" = "$arg_init_long" ]; then
init
echo
create_all_migrations
elif [ "$1" = "$arg_status_short" ] || [ "$1" = "$arg_status_long" ]; then
create_all_migrations
echo
migration_status
elif [ "$1" = "$arg_up_short" ] || [ "$1" = "$arg_up_long" ]; then
create_all_migrations
echo
parse_up_down_args $*
i=0
while [ $i -lt $count ]; do
migrate_up
if [ $count -gt 1 ]; then
echo
fi
i=$(($i + 1))
done
elif [ "$1" = "$arg_down_short" ] || [ "$1" = "$arg_down_long" ]; then
create_all_migrations
echo
parse_up_down_args $*
i=0
while [ $i -lt $count ]; do
migrate_down
if [ $count -gt 1 ]; then
echo
fi
i=$(($i + 1))
done
elif [ "$1" = '' ] || [ "$1" = "$arg_help_short" ] || [ "$1" = "$arg_help_long" ] || [ "$1" = '--help' ]; then
help
else
create_all_migrations
echo
create_migration $1
fi