
* Moving all sql logs to their own log file to avoid cluttering stdout with things that aren't relevant to the immediate goal here
72 lines
1.7 KiB
Bash
72 lines
1.7 KiB
Bash
#!/bin/bash
|
|
|
|
# Points to the folder that contains all pertinent database tables
|
|
# Each table directory contains:
|
|
# dir/
|
|
# up.sql
|
|
# down.sql
|
|
|
|
|
|
# First some parameter checks
|
|
if [ ! $1 ];then
|
|
echo Failure: Insufficient arguments provided
|
|
echo ./build-db.sh /migrations/folder/path
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! $2 ];then
|
|
echo Failure: Insufficient arguments provided
|
|
echo ./build-db.sh /migrations/folder/path /path/to/.env file
|
|
exit 1
|
|
fi
|
|
|
|
# Log paths
|
|
migration_logs=sql-migration.log
|
|
|
|
migration_loc=$1
|
|
env_path=$2
|
|
|
|
source $env_path
|
|
|
|
# This is kinda dumb but we have to check and make sure the sample default user
|
|
# configuration is all there
|
|
if [ ! $DATABASE_NAME ];then
|
|
echo No DATABASE_NAME set
|
|
exit 1
|
|
fi
|
|
if [ ! $DATABASE_PASS ];then
|
|
echo No DATABASE_PASS set
|
|
exit 1
|
|
fi
|
|
if [ ! $DATABASE_USER ];then
|
|
echo No DATABASE_USER set
|
|
exit 1
|
|
fi
|
|
if [ ! $DATABASE_HOST ];then
|
|
echo No DATABASE_HOST set
|
|
exit 1
|
|
fi
|
|
if [ ! $DATABASE_PORT ];then
|
|
echo No DATABASE_PORT set
|
|
exit 1
|
|
fi
|
|
|
|
# Next we setup the database user
|
|
mysql -e "CREATE USER '$DATABASE_USER'@'$DATABASE_HOST' IDENTIFIED BY '$DATABASE_PASS';" \
|
|
> $migration_logs 2>&1
|
|
# Yet another reason to not use this in prod to setup you're database
|
|
mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'$DATABASE_HOST';" \
|
|
>> $migration_logs 2>&1
|
|
|
|
# First we'll setup the databse that we want to use
|
|
for dir in $(ls $migration_loc);do
|
|
# Port field is ignored in this script as its straight up not used in Docker tests
|
|
# NOTE: there should **not** be a space between -p and the database password
|
|
echo Migrating $migration_loc/$dir
|
|
mysql -u $DATABASE_USER -p$DATABASE_PASS \
|
|
-h $DATABASE_HOST -D $DATABASE_NAME \
|
|
< $migration_loc/$dir/up.sql >> $migration_logs 2>&1
|
|
done
|
|
|
|
|