forked from LendingHome/zero_downtime_migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes LendingHome#15
- Loading branch information
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
lib/zero_downtime_migrations/validation/add_foreign_key.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module ZeroDowntimeMigrations | ||
class Validation | ||
class AddForeignKey < Validation | ||
def validate! | ||
return unless requires_validate_constraints? | ||
error!(message) unless foreign_key_not_validated? | ||
end | ||
|
||
private | ||
|
||
def message | ||
<<-MESSAGE.strip_heredoc | ||
Adding a foreign key causes an `ShareRowExclusiveLock` (or `AccessExclusiveLock` on PostgreSQL before 9.5) on both tables. | ||
It is possible to add a foreign key in one step and validate later (which only causes RowShareLocks) | ||
class Add#{foreign_table}ForeignKeyTo#{table} < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :#{table}, #{foreign_table}, validate: false | ||
end | ||
end | ||
class Validate#{foreign_table}ForeignKeyOn#{table} < ActiveRecord::Migration | ||
def change | ||
validate_foreign_key :#{table}, :#{foreign_table} | ||
end | ||
end | ||
Note, both `add_foreign_key` and `validate_foreign_key` accept `name` and `column` options. | ||
MESSAGE | ||
end | ||
|
||
def foreign_key_not_validated? | ||
options[:validate] == false | ||
end | ||
|
||
def foreign_table | ||
args[1] | ||
end | ||
|
||
def table | ||
args[0] | ||
end | ||
|
||
def requires_validate_constraints? | ||
supports_validate_constraints? | ||
end | ||
|
||
def supports_validate_constraints? | ||
ActiveRecord::Base.connection.respond_to?(:supports_validate_constraints?) && | ||
ActiveRecord::Base.connection.supports_validate_constraints? | ||
end | ||
end | ||
end | ||
end |
77 changes: 77 additions & 0 deletions
77
spec/zero_downtime_migrations/validation/add_foreign_key_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
RSpec.describe ZeroDowntimeMigrations::Validation::AddForeignKey do | ||
let(:error) { ZeroDowntimeMigrations::UnsafeMigrationError } | ||
|
||
context "when the database supports_validate_constraints" do | ||
context "adding a foreign key and not specifying validate" do | ||
let(:migration) do | ||
Class.new(ActiveRecord::Migration[5.0]) do | ||
def change | ||
create_table :order | ||
add_column :users, :order_id, :integer | ||
add_foreign_key :users, :order | ||
end | ||
end | ||
end | ||
|
||
it "raises an unsafe migration error" do | ||
expect { migration.migrate(:up) }.to raise_error(error) | ||
end | ||
end | ||
|
||
context "adding a foreign key and specifying validate true" do | ||
let(:migration) do | ||
Class.new(ActiveRecord::Migration[5.0]) do | ||
def change | ||
create_table :order | ||
add_column :users, :order_id, :integer | ||
add_foreign_key :users, :order, validate: true | ||
end | ||
end | ||
end | ||
|
||
it "raises an unsafe migration error" do | ||
expect { migration.migrate(:up) }.to raise_error(error) | ||
end | ||
end | ||
|
||
context "adding a foreign key and specifying validate false" do | ||
let(:migration) do | ||
Class.new(ActiveRecord::Migration[5.0]) do | ||
def change | ||
create_table :order | ||
add_column :users, :order_id, :integer | ||
add_foreign_key :users, :order, validate: false | ||
end | ||
end | ||
end | ||
|
||
it "raises nothing" do | ||
expect { migration.migrate(:up) }.to_not raise_error | ||
end | ||
end | ||
end | ||
|
||
context "when the database does not supports_validate_constraints" do | ||
before do | ||
allow_any_instance_of(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter).to receive( | ||
:supports_validate_constraints? | ||
).and_return(false) | ||
end | ||
|
||
context "adding a foreign key and not specifying validate" do | ||
let(:migration) do | ||
Class.new(ActiveRecord::Migration[5.0]) do | ||
def change | ||
create_table :order | ||
add_column :users, :order_id, :integer | ||
add_foreign_key :users, :order | ||
end | ||
end | ||
end | ||
|
||
it "raises nothing" do | ||
expect { migration.migrate(:up) }.to_not raise_error | ||
end | ||
end | ||
end | ||
end |