I have a bash script to run migrations on our MySQL databases.
Is it possible to wrap migrations within a single transaction when running multiple migration scripts from the command line?
Occasionally we've had schema changes that require some data manipulation to migrate that into the new structure and maintain referential integrity.
I'd love to do something like: (assume all vars are set accordingly)
mysql `mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e "START TRANSACTION;"`
for i in $( ls $MIGRATIONS_FOLDER/migration-* ); do
# check database for record of this file
result=`mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e
"SELECT id,filename,ran FROM tbl_migrations WHERE filename='$i'"
--batch --raw | wc -l`
# if there is a record in tbl_migrations for this file, skip it
if (($result > 1)); then
continue
fi;
echo "Importing $i";
result=`mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME < $i`
if [[summatz screwed up]]
then
mysql `mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e "ROLLBACK;"`
exit
fi
mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e
"INSERT INTO tbl_migrations (filename, ran) VALUES ('$i', 1)"
if [ "$i" = "$END" ]; then
echo "Ending migration at user specified file $i"
break
fi
done
mysql `mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e "COMMIT;"`