Call `on_drop` only when tables/columns are dropped
This commit is contained in:
parent
4ad401bac5
commit
b945a2dc39
|
@ -44,9 +44,10 @@ module Migration
|
|||
LIMIT 1
|
||||
SQL
|
||||
|
||||
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 Migration
|
|||
after_migration: @after_migration).to_a.length > 0
|
||||
end
|
||||
|
||||
def new_table_exists
|
||||
def table_exists(table_name_placeholder)
|
||||
<<~SQL
|
||||
EXISTS(
|
||||
SELECT 1
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE table_schema = 'public' AND
|
||||
table_name = :new_name
|
||||
table_name = #{table_name_placeholder}
|
||||
)
|
||||
SQL
|
||||
end
|
||||
|
|
|
@ -65,6 +65,19 @@ RSpec.describe Migration::ColumnDropper do
|
|||
|
||||
expect(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)
|
||||
end
|
||||
|
||||
it "drops the columns immediately if the first migration was less than 10 minutes ago" do
|
||||
|
|
|
@ -86,6 +86,19 @@ describe Migration::TableDropper do
|
|||
|
||||
expect(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)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -112,6 +125,18 @@ describe Migration::TableDropper do
|
|||
|
||||
expect(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)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue