Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for validated foreign key constraint #62

Closed
wants to merge 14 commits into from

Conversation

iamvery
Copy link
Contributor

@iamvery iamvery commented Feb 21, 2019

Following #59, this PR adds a new check to protect against the addition of a validated foreign key constraint which acquires an AccessExclusiveLock which may result in significant performance problems on large, active tables.

Copy link
Contributor Author

@iamvery iamvery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to your input, so I can fix up this branch and get it ready for release :)

lib/strong_migrations.rb Outdated Show resolved Hide resolved
lib/strong_migrations.rb Outdated Show resolved Hide resolved
lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
test/strong_migrations_test.rb Outdated Show resolved Hide resolved
Copy link
Owner

@ankane ankane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @iamvery, it's a great start. Added some comments inline.

lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
test/strong_migrations_test.rb Outdated Show resolved Hide resolved
TODO Outdated Show resolved Hide resolved
@@ -119,6 +119,16 @@ def method_missing(method, *args, &block)
raise_error :change_column_null,
code: backfill_code(table, column, default)
end
when :add_foreign_key
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we'll have to reject any call to this method on Rails < 5.2 since validate: true is implied.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

@iamvery iamvery force-pushed the iamvery/validating-constraints branch from b744528 to 5cc38c8 Compare April 1, 2019 17:30
validated = options.fetch(:validate) { true }

five_point_two = Gem::Version.new("5.2")
if postgresql? && ActiveRecord.version < five_point_two
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is a better way to do gem version comparisons than boolean operations on ActiveRecord::VERSION

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep it consistent for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #74

@iamvery
Copy link
Contributor Author

iamvery commented Apr 1, 2019

@ankane finally found some time to update this PR. Let me know what you think!

@@ -368,8 +374,13 @@ def test_down
assert_safe SafeUp, direction: :down
end

def test_add_foreign_key
skip unless postgres?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been a really long time since I've used mysql. How confident are we that the unsafe foreign key does not apply to it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's safe with MySQL, it's fine to start with just Postgres.

Copy link
Owner

@ankane ankane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @iamvery, thanks for updating. Added a few comments.

@@ -368,8 +374,13 @@ def test_down
assert_safe SafeUp, direction: :down
end

def test_add_foreign_key
skip unless postgres?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's safe with MySQL, it's fine to start with just Postgres.

test/test_helper.rb Show resolved Hide resolved
validated = options.fetch(:validate) { true }

five_point_two = Gem::Version.new("5.2")
if postgresql? && ActiveRecord.version < five_point_two
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep it consistent for now.

lib/strong_migrations/migration.rb Outdated Show resolved Hide resolved
@@ -119,6 +119,16 @@ def method_missing(method, *args, &block)
raise_error :change_column_null,
code: backfill_code(table, column, default)
end
when :add_foreign_key
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

iamvery added a commit to iamvery/strong_migrations that referenced this pull request May 17, 2019
It's hard to see the benefit of this with the current set of
constraints. However, it is more apparently when including minor
versions in the check as we do in ankane#62.
@iamvery iamvery force-pushed the iamvery/validating-constraints branch from 42f963f to 01070a7 Compare May 17, 2019 20:08
options ||= {}
validated = options.fetch(:validate) { true }

if postgresql? && ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 2)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, this would be much simpler by comparing Gem::Version objects. See #74

ankane added a commit that referenced this pull request May 27, 2019
Co-authored-by: Jay Hayes <ur@iamvery.com>
@ankane
Copy link
Owner

ankane commented May 27, 2019

Thanks @iamvery, merged in cf25a6c.

@ankane ankane closed this May 27, 2019
@iamvery iamvery deleted the iamvery/validating-constraints branch May 28, 2019 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants