I am coordinating a number of PHP development projects. I have setup a workflow where developers all work with one Subversion repository. When they are ready to take a look at their application on staging servers upload a MySQL DB dump and create a tag.
I have configured a shell script that will automatically deploy the PHP code to the DocumentRoot and configure the proper permissions. I now need to script the loading of the database snapshot.
When I first configure a new application I create a database and user account like so:
CREATE DATABASE foodb;
GRANT ALL ON foodb.* TO user1@localhost IDENTIFIED BY 'password';
Now when the developers check a new version of the MySQL dump into Subversion I need to delete the old DB and restore a new snapshot. I'm currently using the following workflow.
1.) Login to MySQL as root
DROP DATABASE foodb;
CREATE DATABASE foodb;
GRANT ALL ON foodb.* TO user1@localhost IDENTIFIED BY 'password';
2.) Login to MySQL "user1"
mysql -h localhost -u user1 --password="drumitFed" foodb << new-db-dump-from-subversion.sql
I would like to combine steps 1 and 2 above. I imagine it would look something like this. The code should not only delete the tables but also views, indexes, and stored procedures.
note, that it has to run as the database user, not a superuser, and the database user does not have DROP DATABASE or CREATE DATABASE permissions.
mysql -h localhost -u user1 --password="password" foodb << delete-all-tables-and-views.sql
mysql -h localhost -u user1 --password="password" foodb << new-db-dump-from-subversion.sql