aboutsummaryrefslogtreecommitdiffstats
path: root/db
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2011-05-11 12:54:33 +0100
committerStruan Donald <struan@exo.org.uk>2011-05-11 12:54:33 +0100
commita615dc88e4fbcdb42e2594571c9c08e8dd5da4f4 (patch)
tree2694e38ded96fa56253aec69a2e8c58b7b044a1c /db
parent5f1db7700255e00ce492d57939f861f76b6ddadf (diff)
schema changes to use users for alerts
Diffstat (limited to 'db')
-rw-r--r--db/schema_0003-create_users_from_alerts_and_link.sql29
1 files changed, 29 insertions, 0 deletions
diff --git a/db/schema_0003-create_users_from_alerts_and_link.sql b/db/schema_0003-create_users_from_alerts_and_link.sql
new file mode 100644
index 000000000..6861e5340
--- /dev/null
+++ b/db/schema_0003-create_users_from_alerts_and_link.sql
@@ -0,0 +1,29 @@
+begin;
+
+-- create any users that don't already exist
+INSERT INTO users (email)
+ SELECT distinct( lower( alert.email ) ) FROM alert
+ LEFT JOIN users ON lower( users.email ) = lower( alert.email )
+ WHERE users.id IS NULL;
+
+ALTER table alert
+ ADD COLUMN user_id INT REFERENCES users(id);
+
+-- populate the user ids in the alert table
+UPDATE alert
+ SET user_id = (
+ SELECT id
+ FROM users
+ WHERE users.email = lower( alert.email )
+ );
+
+CREATE INDEX alert_user_id_idx on alert ( user_id );
+
+-- tidy up now everythings in place
+ALTER table alert
+ ALTER COLUMN user_id SET NOT NULL;
+
+ALTER table alert
+ DROP COLUMN email;
+
+commit;