Allow delayed dropping and renaming of tables
This commit is contained in:
parent
6381bc6ce1
commit
eebe1d8c56
|
@ -2,7 +2,7 @@
|
|||
|
||||
require 'migration/table_dropper'
|
||||
|
||||
Migration::TableDropper.delayed_drop(
|
||||
Migration::TableDropper.delayed_rename(
|
||||
old_name: 'topic_status_updates',
|
||||
new_name: 'topic_timers',
|
||||
after_migration: 'RenameTopicStatusUpdatesToTopicTimers',
|
||||
|
|
|
@ -2,7 +2,13 @@ require_dependency 'migration/base_dropper'
|
|||
|
||||
module Migration
|
||||
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(new_name)
|
||||
|
||||
|
@ -38,9 +44,10 @@ module Migration
|
|||
LIMIT 1
|
||||
SQL
|
||||
|
||||
builder.where(new_table_exists) if @new_name.present?
|
||||
|
||||
builder.where("table_schema = 'public'")
|
||||
.where(previous_migration_done)
|
||||
.where(new_table_exists)
|
||||
.exec(old_name: @old_name,
|
||||
new_name: @new_name,
|
||||
delay: "#{@delay} seconds",
|
||||
|
|
|
@ -4,33 +4,37 @@ require_dependency 'migration/table_dropper'
|
|||
describe Migration::TableDropper do
|
||||
|
||||
def table_exists?(table_name)
|
||||
sql = <<-SQL
|
||||
SELECT 1
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE table_schema = 'public' AND
|
||||
table_name = '#{table_name}'
|
||||
sql = <<~SQL
|
||||
SELECT 1
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE table_schema = 'public' AND
|
||||
table_name = '#{table_name}'
|
||||
SQL
|
||||
|
||||
ActiveRecord::Base.exec_sql(sql).to_a.length > 0
|
||||
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
|
||||
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
|
||||
|
||||
described_class.delayed_drop(
|
||||
described_class.delayed_rename(
|
||||
old_name: 'table_with_old_name',
|
||||
new_name: 'table_with_new_name',
|
||||
after_migration: name,
|
||||
after_migration: migration_name,
|
||||
delay: 20.minutes,
|
||||
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(dropped_proc_called).to eq(false)
|
||||
|
||||
described_class.delayed_drop(
|
||||
described_class.delayed_rename(
|
||||
old_name: 'table_with_old_name',
|
||||
new_name: 'table_with_new_name',
|
||||
after_migration: name,
|
||||
after_migration: migration_name,
|
||||
delay: 10.minutes,
|
||||
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)"
|
||||
|
||||
described_class.delayed_drop(
|
||||
described_class.delayed_rename(
|
||||
old_name: 'table_with_old_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,
|
||||
on_drop: ->() { dropped_proc_called = true }
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue