mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Drop bookmark trigger correctly (#15486)
Apparently `DROP FUNCTION X CASCADE` is the better way to do this, we were running into cross-schema errors on try.
This commit is contained in:
@@ -2,30 +2,35 @@
|
|||||||
|
|
||||||
class AddTriggerForPolymorphicBookmarkColumnsToSyncData < ActiveRecord::Migration[6.1]
|
class AddTriggerForPolymorphicBookmarkColumnsToSyncData < ActiveRecord::Migration[6.1]
|
||||||
def up
|
def up
|
||||||
DB.exec <<~SQL
|
# Note from Martin:
|
||||||
CREATE OR REPLACE FUNCTION sync_bookmarks_polymorphic_column_data()
|
#
|
||||||
RETURNS TRIGGER
|
# Addition to the note below, we don't want to delete this migration but
|
||||||
LANGUAGE PLPGSQL AS $rcr$
|
# we can stop it from creating things we are going to delete anyway.
|
||||||
BEGIN
|
#
|
||||||
IF NEW.for_topic
|
# DB.exec <<~SQL
|
||||||
THEN
|
# CREATE OR REPLACE FUNCTION sync_bookmarks_polymorphic_column_data()
|
||||||
NEW.bookmarkable_id = (SELECT topic_id FROM posts WHERE posts.id = NEW.post_id);
|
# RETURNS TRIGGER
|
||||||
NEW.bookmarkable_type = 'Topic';
|
# LANGUAGE PLPGSQL AS $rcr$
|
||||||
ELSE
|
# BEGIN
|
||||||
NEW.bookmarkable_id = NEW.post_id;
|
# IF NEW.for_topic
|
||||||
NEW.bookmarkable_type = 'Post';
|
# THEN
|
||||||
END IF;
|
# NEW.bookmarkable_id = (SELECT topic_id FROM posts WHERE posts.id = NEW.post_id);
|
||||||
RETURN NEW;
|
# NEW.bookmarkable_type = 'Topic';
|
||||||
END
|
# ELSE
|
||||||
$rcr$;
|
# NEW.bookmarkable_id = NEW.post_id;
|
||||||
SQL
|
# NEW.bookmarkable_type = 'Post';
|
||||||
|
# END IF;
|
||||||
|
# RETURN NEW;
|
||||||
|
# END
|
||||||
|
# $rcr$;
|
||||||
|
# SQL
|
||||||
|
|
||||||
DB.exec <<~SQL
|
# DB.exec <<~SQL
|
||||||
CREATE TRIGGER bookmarks_polymorphic_data_sync
|
# CREATE TRIGGER bookmarks_polymorphic_data_sync
|
||||||
BEFORE INSERT OR UPDATE OF post_id, for_topic ON bookmarks
|
# BEFORE INSERT OR UPDATE OF post_id, for_topic ON bookmarks
|
||||||
FOR EACH ROW
|
# FOR EACH ROW
|
||||||
EXECUTE FUNCTION sync_bookmarks_polymorphic_column_data();
|
# EXECUTE FUNCTION sync_bookmarks_polymorphic_column_data();
|
||||||
SQL
|
# SQL
|
||||||
|
|
||||||
# sync data that already exists in the table
|
# sync data that already exists in the table
|
||||||
#
|
#
|
||||||
@@ -50,7 +55,6 @@ class AddTriggerForPolymorphicBookmarkColumnsToSyncData < ActiveRecord::Migratio
|
|||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
DB.exec("DROP TRIGGER IF EXISTS bookmarks_polymorphic_data_sync ON bookmarks")
|
DB.exec("DROP FUNCTION IF EXISTS sync_bookmarks_polymorphic_column_data CASCADE")
|
||||||
DB.exec("DROP FUNCTION IF EXISTS sync_bookmarks_polymorphic_column_data")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
class DropBookmarkPolymorphicTrigger < ActiveRecord::Migration[6.1]
|
class DropBookmarkPolymorphicTrigger < ActiveRecord::Migration[6.1]
|
||||||
def up
|
def up
|
||||||
DB.exec("DROP TRIGGER IF EXISTS bookmarks_polymorphic_data_sync ON bookmarks")
|
DB.exec("DROP FUNCTION IF EXISTS sync_bookmarks_polymorphic_column_data CASCADE")
|
||||||
DB.exec("DROP FUNCTION IF EXISTS sync_bookmarks_polymorphic_column_data")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
|
|||||||
Reference in New Issue
Block a user