summaryrefslogtreecommitdiff
path: root/db/migrate/20170418103908_delete_orphan_notification_settings.rb
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2017-04-19 10:15:35 +0200
committerJames Lopez <james@jameslopez.es>2017-04-19 10:15:35 +0200
commit1c6710e235e00d74b33c3712e2a981a8891d2a82 (patch)
treefce0d8e3cac774dca606f1e54c26d97648050057 /db/migrate/20170418103908_delete_orphan_notification_settings.rb
parentec9f6180bc4684521444ee0681308bf4c9c71297 (diff)
downloadgitlab-ce-1c6710e235e00d74b33c3712e2a981a8891d2a82.tar.gz
Add migration to remove orphaned notification settings
Diffstat (limited to 'db/migrate/20170418103908_delete_orphan_notification_settings.rb')
-rw-r--r--db/migrate/20170418103908_delete_orphan_notification_settings.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/db/migrate/20170418103908_delete_orphan_notification_settings.rb b/db/migrate/20170418103908_delete_orphan_notification_settings.rb
new file mode 100644
index 00000000000..e4b9cf65936
--- /dev/null
+++ b/db/migrate/20170418103908_delete_orphan_notification_settings.rb
@@ -0,0 +1,24 @@
+class DeleteOrphanNotificationSettings < ActiveRecord::Migration
+ DOWNTIME = false
+
+ def up
+ execute("DELETE FROM notification_settings WHERE EXISTS (SELECT true FROM (#{orphan_notification_settings}) AS ns WHERE ns.id = notification_settings.id)")
+ end
+
+ def down
+ # This is a no-op method to make the migration reversible.
+ # If someone is trying to rollback for other reasons, we should not throw an Exception.
+ # raise ActiveRecord::IrreversibleMigration
+ end
+
+ def orphan_notification_settings
+ <<-SQL
+ SELECT notification_settings.id
+ FROM notification_settings
+ LEFT OUTER JOIN namespaces
+ ON namespaces.id = notification_settings.source_id
+ WHERE notification_settings.source_type = 'Namespace'
+ AND namespaces.id IS NULL
+ SQL
+ end
+end