Permalink
Browse files

Make migration script more generic

  • Loading branch information...
1 parent 65c3731 commit 1198445a834854a4c93da4a62ef9fe2603e53e29 authmillenon committed Mar 16, 2012
Showing with 29 additions and 22 deletions.
  1. +9 −2 db_migration.sh
  2. +20 −20 db_migration.sql
View
11 db_migration.sh
@@ -1,11 +1,18 @@
#!/bin/bash
if [[ $# < 1 ]]; then
- echo "Usage: $0 <username>" >&2
+ echo "Usage: $0 <username> [<database>]" >&2
exit 1
fi
+if [[ $# == 2 ]]; then
+ DATABASE=$2
+else
+ DATABASE=datalovers
+fi
+
USERNAME=$1
python manage.py syncdb --noinput > /dev/null
python manage.py reset --noinput auth piston give > /dev/null
-mysql -p -u ${USERNAME} datalovers < db_migration.sql
+cat db_migration.sql | sed "s/{{datalovers}}/$DATABASE/g" | mysql -p -u ${USERNAME} ${DATABASE}
+
View
40 db_migration.sql
@@ -1,40 +1,40 @@
BEGIN;
-INSERT INTO datalovers.auth_user (username, password, email, last_login, is_active, date_joined)
+INSERT INTO {{datalovers}}.auth_user (username, password, email, last_login, is_active, date_joined)
SELECT nickname, password, email, last_changed, TRUE, last_changed
- FROM datalovers.users;
+ FROM {{datalovers}}.users;
-INSERT INTO datalovers.give_lovableobject (id, received_love)
+INSERT INTO {{datalovers}}.give_lovableobject (id, received_love)
SELECT new.id, old.received_love
- FROM datalovers.users old
- INNER JOIN datalovers.auth_user new
+ FROM {{datalovers}}.users old
+ INNER JOIN {{datalovers}}.auth_user new
ON new.username = old.nickname;
-INSERT INTO datalovers.give_dataloveprofile (lovableobject_ptr_id, user_id, available_love,last_love_update)
+INSERT INTO {{datalovers}}.give_dataloveprofile (lovableobject_ptr_id, user_id, available_love,last_love_update)
SELECT new.id, new.id, old.available_love, old.last_changed
- FROM datalovers.users old
- INNER JOIN datalovers.auth_user new
+ FROM {{datalovers}}.users old
+ INNER JOIN {{datalovers}}.auth_user new
ON new.username = old.nickname;
-INSERT INTO datalovers.give_datalovehistory (sender_id, recipient_id, amount, timestamp)
+INSERT INTO {{datalovers}}.give_datalovehistory (sender_id, recipient_id, amount, timestamp)
SELECT sender.id, rec_id, old.amount, old.timestamp
- FROM datalovers.auth_user sender INNER JOIN datalovers.history old
+ FROM {{datalovers}}.auth_user sender INNER JOIN {{datalovers}}.history old
ON sender.username = old.sender
INNER JOIN ( SELECT user.username AS rec_username, profile.lovableobject_ptr_id AS rec_id
- FROM datalovers.auth_user user
- INNER JOIN datalovers.give_dataloveprofile profile
+ FROM {{datalovers}}.auth_user user
+ INNER JOIN {{datalovers}}.give_dataloveprofile profile
ON user.id = profile.user_id) receiver
ON rec_username = old.recipient
ORDER BY timestamp;
-INSERT INTO datalovers.give_userwebsite (user_id, url)
+INSERT INTO {{datalovers}}.give_userwebsite (user_id, url)
SELECT user.id, LOWER(website.website)
- FROM datalovers.user_websites website
- JOIN datalovers.auth_user user
+ FROM {{datalovers}}.user_websites website
+ JOIN {{datalovers}}.auth_user user
ON user.username = website.nickname;
-DROP TABLE datalovers.user_websites;
-DROP TABLE datalovers.user_sessions;
-DROP TABLE datalovers.history;
-DROP TABLE datalovers.users;
-DROP TABLE datalovers.sessions;
+DROP TABLE {{datalovers}}.user_websites;
+DROP TABLE {{datalovers}}.user_sessions;
+DROP TABLE {{datalovers}}.history;
+DROP TABLE {{datalovers}}.users;
+DROP TABLE {{datalovers}}.sessions;
COMMIT;

0 comments on commit 1198445

Please sign in to comment.