mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 08:57:10 -06:00
Protect against accidental table renames
This commit is contained in:
parent
f2c060bdf2
commit
19c5afc69d
@ -84,26 +84,26 @@ class Migration::SafeMigrate
|
||||
end
|
||||
|
||||
def self.protect!(sql)
|
||||
if sql =~ /^\s*drop\s+table/i
|
||||
if sql =~ /^\s*(?:drop\s+table|alter\s+table.*rename\s+to)\s+/i
|
||||
$stdout.puts("", <<~STR)
|
||||
WARNING
|
||||
-------------------------------------------------------------------------------------
|
||||
An attempt was made to drop a table in a migration
|
||||
An attempt was made to drop or rename a table in a migration
|
||||
SQL used was: '#{sql}'
|
||||
Please use the deferred pattrn using Migration::TableDropper in db/seeds to drop
|
||||
the table.
|
||||
Please use the deferred pattern using Migration::TableDropper in db/seeds to drop
|
||||
or rename the table.
|
||||
|
||||
This protection is in place to protect us against dropping tables that are currently
|
||||
in use by live applications.
|
||||
STR
|
||||
raise Discourse::InvalidMigration, "Attempt was made to drop a table"
|
||||
elsif sql =~ /^\s*alter\s+table.*(rename|drop)/i
|
||||
elsif sql =~ /^\s*alter\s+table.*(?:rename|drop)\s+/i
|
||||
$stdout.puts("", <<~STR)
|
||||
WARNING
|
||||
-------------------------------------------------------------------------------------
|
||||
An attempt was made to drop or rename a column in a migration
|
||||
SQL used was: '#{sql}'
|
||||
Please use the deferred pattrn using Migration::ColumnDropper in db/seeds to drop
|
||||
Please use the deferred pattern using Migration::ColumnDropper in db/seeds to drop
|
||||
or rename columns.
|
||||
|
||||
Note, to minimize disruption use self.ignored_columns = ["column name"] on your
|
||||
|
@ -37,6 +37,22 @@ describe Migration::SafeMigrate do
|
||||
expect(User.first).not_to eq(nil)
|
||||
end
|
||||
|
||||
it "bans all table renames" do
|
||||
Migration::SafeMigrate.enable!
|
||||
|
||||
path = File.expand_path "#{Rails.root}/spec/fixtures/migrate/rename_table"
|
||||
|
||||
output = capture_stdout do
|
||||
expect(lambda do
|
||||
ActiveRecord::Migrator.up([path])
|
||||
end).to raise_error(StandardError)
|
||||
end
|
||||
|
||||
expect(output).to include("TableDropper")
|
||||
|
||||
expect(User.first).not_to eq(nil)
|
||||
end
|
||||
|
||||
it "bans all column removal" do
|
||||
Migration::SafeMigrate.enable!
|
||||
|
||||
|
9
spec/fixtures/migrate/rename_table/20990309014014_rename_table.rb
vendored
Normal file
9
spec/fixtures/migrate/rename_table/20990309014014_rename_table.rb
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
class RenameTable < ActiveRecord::Migration[5.1]
|
||||
def up
|
||||
rename_table :users, :persons
|
||||
end
|
||||
|
||||
def down
|
||||
raise "not tested"
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user