]> source.dussan.org Git - redmine.git/commitdiff
Delete orphaned query and role ids from habtm join table (#36844).
authorMarius Balteanu <marius.balteanu@zitec.com>
Sun, 2 Oct 2022 20:39:06 +0000 (20:39 +0000)
committerMarius Balteanu <marius.balteanu@zitec.com>
Sun, 2 Oct 2022 20:39:06 +0000 (20:39 +0000)
git-svn-id: https://svn.redmine.org/redmine/trunk@21893 e93f8b46-1217-0410-a6f0-8f06a7374b81

db/migrate/20221002193055_delete_orphaned_query_and_role_from_queries_roles.rb [new file with mode: 0644]

diff --git a/db/migrate/20221002193055_delete_orphaned_query_and_role_from_queries_roles.rb b/db/migrate/20221002193055_delete_orphaned_query_and_role_from_queries_roles.rb
new file mode 100644 (file)
index 0000000..aa86450
--- /dev/null
@@ -0,0 +1,14 @@
+class DeleteOrphanedQueryAndRoleFromQueriesRoles < ActiveRecord::Migration[6.1]
+  def self.up
+    queries_roles = "#{Query.table_name_prefix}queries_roles#{Query.table_name_suffix}"
+    queries = Query.table_name
+    roles = Role.table_name
+
+    ActiveRecord::Base.connection.execute "DELETE FROM #{queries_roles} WHERE query_id NOT IN (SELECT DISTINCT(id) FROM #{queries})"
+    ActiveRecord::Base.connection.execute "DELETE FROM #{queries_roles} WHERE role_id NOT IN (SELECT DISTINCT(id) FROM #{roles})"
+  end
+
+  def self.down
+    # no-op
+  end
+end