-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support sharding for background migrations
- Loading branch information
Showing
24 changed files
with
613 additions
and
117 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
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
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
18 changes: 18 additions & 0 deletions
18
lib/generators/online_migrations/templates/add_sharding_to_online_migrations.rb.tt
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,18 @@ | ||
class AddShardingToOnlineMigrations < <%= migration_parent %> | ||
def change | ||
safety_assured do | ||
remove_index :background_migrations, [:migration_name, :arguments], unique: true | ||
|
||
change_table :background_migrations do |t| | ||
t.bigint :parent_id | ||
t.string :shard | ||
t.boolean :runnable, default: true, null: false | ||
|
||
t.foreign_key :background_migrations, column: :parent_id, on_delete: :cascade | ||
|
||
t.index [:migration_name, :arguments, :shard], | ||
unique: true, name: :index_background_migrations_on_unique_configuration | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails/generators" | ||
require "rails/generators/active_record/migration" | ||
|
||
module OnlineMigrations | ||
# @private | ||
class UpgradeGenerator < Rails::Generators::Base | ||
include ActiveRecord::Generators::Migration | ||
|
||
source_root File.expand_path("templates", __dir__) | ||
|
||
def copy_templates | ||
migrations_to_be_applied.each do |migration| | ||
migration_template("#{migration}.rb", File.join(db_migrate_path, "#{migration}.rb")) | ||
end | ||
end | ||
|
||
private | ||
def migrations_to_be_applied | ||
connection = BackgroundMigrations::Migration.connection | ||
columns = connection.columns(BackgroundMigrations::Migration.table_name).map(&:name) | ||
|
||
migrations = [] | ||
migrations << "add_sharding_to_online_migrations" if !columns.include?("shard") | ||
migrations | ||
end | ||
|
||
def migration_parent | ||
"ActiveRecord::Migration[#{Utils.ar_version}]" | ||
end | ||
end | ||
end |
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
Oops, something went wrong.