-
Notifications
You must be signed in to change notification settings - Fork 193
Add add_column_safely and backfill_column_safely migration helpers #105
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| module StrongMigrations | ||
| module MigrationHelpers | ||
| include Util | ||
|
|
||
| def add_foreign_key_safely(from_table, to_table, **options) | ||
| ensure_postgresql(__method__) | ||
| ensure_not_in_transaction(__method__) | ||
|
|
@@ -73,16 +75,85 @@ def remove_null_constraint_safely(table_name, column_name, name: nil) | |
| end | ||
| end | ||
|
|
||
| def add_column_safely(table_name, column_name, type, **options) | ||
| default = options.delete(:default) | ||
|
|
||
| if column_can_be_added_safely?(default) | ||
| safety_assured { add_column(table_name, column_name, type, default: default, **options) } | ||
| else | ||
| ensure_not_in_transaction(__method__) | ||
|
|
||
| reversible do |dir| | ||
| dir.up do | ||
| transaction do | ||
| add_column(table_name, column_name, type, default: nil, **options) | ||
| change_column_default(table_name, column_name, default) | ||
| end | ||
|
|
||
| backfill_column_safely(table_name, column_name, default) | ||
|
|
||
| allow_null = options[:null] | ||
| add_null_constraint_safely(table_name, column_name) unless allow_null | ||
| end | ||
|
|
||
| dir.down do | ||
| remove_column(table_name, column_name) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def backfill_column_safely(table_name, column_name, value) | ||
| ensure_not_in_transaction(__method__) | ||
|
|
||
| table = Arel::Table.new(table_name) | ||
| primary_key = connection.primary_key(table_name) | ||
|
|
||
| start_arel = table | ||
| .project(table[primary_key]) | ||
| .where(table[column_name].eq(nil)) | ||
| .order(table[primary_key].asc) | ||
| .take(1) | ||
|
|
||
| result = connection.exec_query(start_arel.to_sql) | ||
| return if result.empty? | ||
|
|
||
| start_pk = result.first[primary_key] | ||
| batch_size = 10_000 | ||
|
|
||
| loop do | ||
| finish_arel = table | ||
| .project(table[primary_key]) | ||
| .where(table[primary_key].gteq(start_pk)) | ||
| .order(table[primary_key].asc) | ||
| .skip(batch_size) | ||
| .take(1) | ||
|
|
||
| finish_result = connection.exec_query(finish_arel.to_sql).first | ||
|
|
||
| update_arel = Arel::UpdateManager.new | ||
| .table(table) | ||
| .set([[table[column_name], value]]) | ||
| .where(table[primary_key].gteq(start_pk)) | ||
|
|
||
| if finish_result | ||
| finish_pk = finish_result[primary_key] | ||
| update_arel = update_arel.where(table[primary_key].lt(finish_pk)) | ||
| start_pk = finish_pk | ||
| end | ||
|
|
||
| safety_assured { execute(update_arel.to_sql) } | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to throttle - |
||
| break unless finish_pk | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ensure_postgresql(method_name) | ||
| raise StrongMigrations::Error, "`#{method_name}` is intended for Postgres only" unless postgresql? | ||
| end | ||
|
|
||
| def postgresql? | ||
| %w(PostgreSQL PostGIS).include?(connection.adapter_name) | ||
| end | ||
|
|
||
| def ensure_not_in_transaction(method_name) | ||
| if connection.transaction_open? | ||
| raise StrongMigrations::Error, "Cannot run `#{method_name}` inside a transaction. Use `disable_ddl_transaction` to disable the transaction." | ||
|
|
@@ -110,8 +181,8 @@ def on_delete_update_statement(delete_or_update, action) | |
| end | ||
| end | ||
|
|
||
| def quote_identifiers(statement, identifiers) | ||
| statement % identifiers.map { |v| connection.quote_table_name(v) } | ||
| def column_can_be_added_safely?(default) | ||
| default.nil? || (postgresql? && postgresql_version >= Gem::Version.new("11")) || (mysql? && mysql_version >= Gem::Version.new("8.0.12")) || (mariadb? && mariadb_version >= Gem::Version.new("10.3.2")) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,45 @@ | ||
| module StrongMigrations | ||
| module Util | ||
| def postgresql? | ||
| connection.adapter_name =~ /postg/i # PostgreSQL, PostGIS | ||
| end | ||
|
|
||
| def postgresql_version | ||
| @postgresql_version ||= begin | ||
| target_version(StrongMigrations.target_postgresql_version) do | ||
| # only works with major versions | ||
| connection.select_all("SHOW server_version_num").first["server_version_num"].to_i / 10000 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def mysql? | ||
| connection.adapter_name =~ /mysql/i && !connection.try(:mariadb?) | ||
| end | ||
|
|
||
| def mysql_version | ||
| @mysql_version ||= begin | ||
| target_version(StrongMigrations.target_mysql_version) do | ||
| connection.select_all("SELECT VERSION()").first["VERSION()"].split("-").first | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def mariadb? | ||
| connection.adapter_name =~ /mysql/i && connection.try(:mariadb?) | ||
| end | ||
|
|
||
| def mariadb_version | ||
| @mariadb_version ||= begin | ||
| target_version(StrongMigrations.target_mariadb_version) do | ||
| connection.select_all("SELECT VERSION()").first["VERSION()"].split("-").first | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def quote_identifiers(statement, identifiers) | ||
| # not all identifiers are tables, but this method of quoting should be fine | ||
| statement % identifiers.map { |v| connection.quote_table_name(v) } | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this also need to add
.where(table[column_name].not_eq(value))to avoid reprocessing rows during a retry? Same question for the actual update a few lines down.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. This method updates rows in primary key order, so when restarting, the whole process will start from first (
start_pk) unmatched row, which was last when update crashes, and move forward.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true that the process will pickup where it left off before the restart but won't it unnecessarily update rows that were created after the column default was set but before the backfill process completes (i.e. rows were the DB filled in the column default on insert)? Might not be much of an issue in practice though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this.