A platform for community discussion. Free, open, simple.
Call `on_drop` only when tables/columns are dropped
Gerhard Schlagerb945a2dc39fb
diff --git a/lib/migration/table_dropper.rb b/lib/migration/table_dropper.rbindex 02d383849e7..78a9c7172f5 100644--- a/lib/migration/table_dropper.rb+++ b/lib/migration/table_dropper.rb@@ -44,9 +44,10 @@ module MigrationLIMIT 1SQL- builder.where(new_table_exists) if @new_name.present?+ builder.where(table_exists(":new_name")) if @new_name.present?builder.where("table_schema = 'public'")+ .where(table_exists(":old_name")).where(previous_migration_done).exec(old_name: @old_name,new_name: @new_name,@@ -54,13 +55,13 @@ module Migrationafter_migration: @after_migration).to_a.length > 0end- def new_table_exists+ def table_exists(table_name_placeholder)<<~SQLEXISTS(SELECT 1FROM INFORMATION_SCHEMA.TABLESWHERE table_schema = 'public' AND- table_name = :new_name+ table_name = #{table_name_placeholder})SQLenddiff --git a/spec/components/migration/column_dropper_spec.rb b/spec/components/migration/column_dropper_spec.rbindex 6ae3f06f508..9a0bd536e2b 100644--- a/spec/components/migration/column_dropper_spec.rb+++ b/spec/components/migration/column_dropper_spec.rb@@ -65,6 +65,19 @@ RSpec.describe Migration::ColumnDropper doexpect(has_column?('topics', 'junk')).to eq(false)expect(dropped_proc_called).to eq(true)++ dropped_proc_called = false++ Migration::ColumnDropper.drop(+ table: 'topics',+ after_migration: migration_name,+ columns: ['junk'],+ delay: 10.minutes,+ on_drop: ->() { dropped_proc_called = true }+ )++ # it should call "on_drop" only when there are columns to drop+ expect(dropped_proc_called).to eq(false)endit "drops the columns immediately if the first migration was less than 10 minutes ago" dodiff --git a/spec/components/migration/table_dropper_spec.rb b/spec/components/migration/table_dropper_spec.rbindex e6dc4a1a063..0b798f82bcb 100644--- a/spec/components/migration/table_dropper_spec.rb+++ b/spec/components/migration/table_dropper_spec.rb@@ -86,6 +86,19 @@ describe Migration::TableDropper doexpect(table_exists?('table_with_old_name')).to eq(false)expect(dropped_proc_called).to eq(true)++ dropped_proc_called = false++ described_class.delayed_rename(+ old_name: 'table_with_old_name',+ new_name: 'table_with_new_name',+ after_migration: migration_name,+ delay: 10.minutes,+ on_drop: ->() { dropped_proc_called = true }+ )++ # it should call "on_drop" only when there is a table to drop+ expect(dropped_proc_called).to eq(false)endend@@ -112,6 +125,18 @@ describe Migration::TableDropper doexpect(table_exists?('table_with_old_name')).to eq(false)expect(dropped_proc_called).to eq(true)++ dropped_proc_called = false++ described_class.delayed_drop(+ table_name: 'table_with_old_name',+ after_migration: migration_name,+ delay: 10.minutes,+ on_drop: ->() { dropped_proc_called = true }+ )++ # it should call "on_drop" only when there is a table to drop+ expect(dropped_proc_called).to eq(false)endendend