Allow delayed dropping and renaming of tables
This commit is contained in:
parent
6381bc6ce1
commit
eebe1d8c56
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
require 'migration/table_dropper'
|
require 'migration/table_dropper'
|
||||||
|
|
||||||
Migration::TableDropper.delayed_drop(
|
Migration::TableDropper.delayed_rename(
|
||||||
old_name: 'topic_status_updates',
|
old_name: 'topic_status_updates',
|
||||||
new_name: 'topic_timers',
|
new_name: 'topic_timers',
|
||||||
after_migration: 'RenameTopicStatusUpdatesToTopicTimers',
|
after_migration: 'RenameTopicStatusUpdatesToTopicTimers',
|
||||||
|
|
|
@ -2,7 +2,13 @@ require_dependency 'migration/base_dropper'
|
||||||
|
|
||||||
module Migration
|
module Migration
|
||||||
class Migration::TableDropper < BaseDropper
|
class Migration::TableDropper < BaseDropper
|
||||||
def self.delayed_drop(old_name:, new_name:, after_migration:, delay: nil, on_drop: nil)
|
def self.delayed_drop(table_name:, after_migration:, delay: nil, on_drop: nil)
|
||||||
|
validate_table_name(table_name)
|
||||||
|
|
||||||
|
TableDropper.new(table_name, nil, after_migration, delay, on_drop).delayed_drop
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.delayed_rename(old_name:, new_name:, after_migration:, delay: nil, on_drop: nil)
|
||||||
validate_table_name(old_name)
|
validate_table_name(old_name)
|
||||||
validate_table_name(new_name)
|
validate_table_name(new_name)
|
||||||
|
|
||||||
|
@ -38,9 +44,10 @@ module Migration
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
|
builder.where(new_table_exists) if @new_name.present?
|
||||||
|
|
||||||
builder.where("table_schema = 'public'")
|
builder.where("table_schema = 'public'")
|
||||||
.where(previous_migration_done)
|
.where(previous_migration_done)
|
||||||
.where(new_table_exists)
|
|
||||||
.exec(old_name: @old_name,
|
.exec(old_name: @old_name,
|
||||||
new_name: @new_name,
|
new_name: @new_name,
|
||||||
delay: "#{@delay} seconds",
|
delay: "#{@delay} seconds",
|
||||||
|
|
|
@ -4,33 +4,37 @@ require_dependency 'migration/table_dropper'
|
||||||
describe Migration::TableDropper do
|
describe Migration::TableDropper do
|
||||||
|
|
||||||
def table_exists?(table_name)
|
def table_exists?(table_name)
|
||||||
sql = <<-SQL
|
sql = <<~SQL
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM INFORMATION_SCHEMA.TABLES
|
FROM INFORMATION_SCHEMA.TABLES
|
||||||
WHERE table_schema = 'public' AND
|
WHERE table_schema = 'public' AND
|
||||||
table_name = '#{table_name}'
|
table_name = '#{table_name}'
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
ActiveRecord::Base.exec_sql(sql).to_a.length > 0
|
ActiveRecord::Base.exec_sql(sql).to_a.length > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#delayed_drop' do
|
let(:migration_name) do
|
||||||
|
ActiveRecord::Base
|
||||||
|
.exec_sql("SELECT name FROM schema_migration_details LIMIT 1")
|
||||||
|
.getvalue(0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
ActiveRecord::Base.exec_sql "CREATE TABLE table_with_old_name (topic_id INTEGER)"
|
||||||
|
|
||||||
|
Topic.exec_sql("UPDATE schema_migration_details SET created_at = :created_at WHERE name = :name",
|
||||||
|
name: migration_name, created_at: 15.minutes.ago)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#delayed_rename" do
|
||||||
it "can drop a table after correct delay and when new table exists" do
|
it "can drop a table after correct delay and when new table exists" do
|
||||||
ActiveRecord::Base.exec_sql "CREATE TABLE table_with_old_name (topic_id INTEGER)"
|
|
||||||
|
|
||||||
name = ActiveRecord::Base
|
|
||||||
.exec_sql("SELECT name FROM schema_migration_details LIMIT 1")
|
|
||||||
.getvalue(0, 0)
|
|
||||||
|
|
||||||
Topic.exec_sql("UPDATE schema_migration_details SET created_at = :created_at WHERE name = :name",
|
|
||||||
name: name, created_at: 15.minutes.ago)
|
|
||||||
|
|
||||||
dropped_proc_called = false
|
dropped_proc_called = false
|
||||||
|
|
||||||
described_class.delayed_drop(
|
described_class.delayed_rename(
|
||||||
old_name: 'table_with_old_name',
|
old_name: 'table_with_old_name',
|
||||||
new_name: 'table_with_new_name',
|
new_name: 'table_with_new_name',
|
||||||
after_migration: name,
|
after_migration: migration_name,
|
||||||
delay: 20.minutes,
|
delay: 20.minutes,
|
||||||
on_drop: ->() { dropped_proc_called = true }
|
on_drop: ->() { dropped_proc_called = true }
|
||||||
)
|
)
|
||||||
|
@ -38,10 +42,10 @@ describe Migration::TableDropper do
|
||||||
expect(table_exists?('table_with_old_name')).to eq(true)
|
expect(table_exists?('table_with_old_name')).to eq(true)
|
||||||
expect(dropped_proc_called).to eq(false)
|
expect(dropped_proc_called).to eq(false)
|
||||||
|
|
||||||
described_class.delayed_drop(
|
described_class.delayed_rename(
|
||||||
old_name: 'table_with_old_name',
|
old_name: 'table_with_old_name',
|
||||||
new_name: 'table_with_new_name',
|
new_name: 'table_with_new_name',
|
||||||
after_migration: name,
|
after_migration: migration_name,
|
||||||
delay: 10.minutes,
|
delay: 10.minutes,
|
||||||
on_drop: ->() { dropped_proc_called = true }
|
on_drop: ->() { dropped_proc_called = true }
|
||||||
)
|
)
|
||||||
|
@ -51,10 +55,36 @@ describe Migration::TableDropper do
|
||||||
|
|
||||||
ActiveRecord::Base.exec_sql "CREATE TABLE table_with_new_name (topic_id INTEGER)"
|
ActiveRecord::Base.exec_sql "CREATE TABLE table_with_new_name (topic_id INTEGER)"
|
||||||
|
|
||||||
described_class.delayed_drop(
|
described_class.delayed_rename(
|
||||||
old_name: 'table_with_old_name',
|
old_name: 'table_with_old_name',
|
||||||
new_name: 'table_with_new_name',
|
new_name: 'table_with_new_name',
|
||||||
after_migration: name,
|
after_migration: migration_name,
|
||||||
|
delay: 10.minutes,
|
||||||
|
on_drop: ->() { dropped_proc_called = true }
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(table_exists?('table_with_old_name')).to eq(false)
|
||||||
|
expect(dropped_proc_called).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#delayed_drop" do
|
||||||
|
it "can drop a table after correct delay" do
|
||||||
|
dropped_proc_called = false
|
||||||
|
|
||||||
|
described_class.delayed_drop(
|
||||||
|
table_name: 'table_with_old_name',
|
||||||
|
after_migration: migration_name,
|
||||||
|
delay: 20.minutes,
|
||||||
|
on_drop: ->() { dropped_proc_called = true }
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(table_exists?('table_with_old_name')).to eq(true)
|
||||||
|
expect(dropped_proc_called).to eq(false)
|
||||||
|
|
||||||
|
described_class.delayed_drop(
|
||||||
|
table_name: 'table_with_old_name',
|
||||||
|
after_migration: migration_name,
|
||||||
delay: 10.minutes,
|
delay: 10.minutes,
|
||||||
on_drop: ->() { dropped_proc_called = true }
|
on_drop: ->() { dropped_proc_called = true }
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue