Last active
September 12, 2018 08:45
-
-
Save meriy100/c9113a8b88803fbea339eb0ff0a76d24 to your computer and use it in GitHub Desktop.
安全な! migration ファイルの作り方 ref: https://qiita.com/meriy100/items/b63f44496a31e2f7c9fc
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
class ChangeColumnsToBooks < ActiveRecord::Migration[5.0] | |
def change | |
add_column :books, :name, :string | |
add_index :books, :isin, unique: true # <= ここでコケると面倒 | |
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
class AddNameToBooks < ActiveRecord::Migration[5.0] | |
def change | |
add_column :books, :name, :string | |
end | |
end | |
class AddIsinIndexToBooks < ActiveRecord::Migration[5.0] | |
add_index :books, :isin, unique: true # <= ここでコケても次回またここからはじまる | |
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
class AddIsinIndexToBooks < ActiveRecord::Migration[5.0] | |
def change | |
Book.order(isin: :asc).each_cons(2) do |first, second| | |
first.destroy if first.isin == second | |
end | |
add_index :books, :isin, unique: true | |
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
class AddIsinIndexToBooks < ActiveRecord::Migration[5.0] | |
class まだ重複しているレコードがあるよエラー < StandardError; end | |
def change | |
ActiveRecord::Base.transaction do | |
Book.each_cons(2) do |first, second| | |
first.destroy if first.isin == second | |
end | |
raise まだ重複しているレコードがあるよエラー if Book.pluck(:isin).uniq.count != Book.count | |
end | |
add_index :books, :isin, unique: true | |
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
class AddIsinIndexToBooks < ActiveRecord::Migration[5.0] | |
class Book < ActiveRecord::Base; end # <= アプリケーションの Book の実装に依存しないように | |
def change | |
Book.order(isin: :asc).each_cons(2) do |first, second| # <= ここで使われるのは `AddIsinIndexToBooks::Book` になる | |
first.destroy if first.isin == second | |
end | |
add_index :books, :isin, unique: true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment