Split migration into two parts for easier testing

This commit is contained in:
Matt-Yorkley
2022-02-15 14:52:19 +00:00
committed by Maikel Linke
parent 68193efcf6
commit feaa92406a
2 changed files with 24 additions and 21 deletions

View File

@@ -1,34 +1,13 @@
# frozen_string_literal: true
class SplitCustomersName < ActiveRecord::Migration[6.1]
class SpreeAddress < ApplicationRecord
end
class Customer < ApplicationRecord
belongs_to :bill_address, class_name: "SpreeAddress"
end
def up
add_column :customers, :first_name, :string, null: false, default: ""
add_column :customers, :last_name, :string, null: false, default: ""
migrate_customer_name_data!
end
def down
remove_column :customers, :first_name
remove_column :customers, :last_name
end
def migrate_customer_name_data!
Customer.where(first_name: "", last_name: "").where.not(name: [nil, ""]).find_each do |customer|
name_words = customer.name.split(' ')
next if name_words.empty?
customer.update(
first_name: name_words.first,
last_name: name_words[1..].join(' ')
)
end
end
end

View File

@@ -0,0 +1,24 @@
# frozen_string_literal: true
class MigrateCustomersData < ActiveRecord::Migration[6.1]
class SpreeAddress < ApplicationRecord; end
class Customer < ApplicationRecord
belongs_to :bill_address, class_name: "SpreeAddress"
end
def up
migrate_customer_name_data!
end
def migrate_customer_name_data!
Customer.where(first_name: "", last_name: "").where.not(name: [nil, ""]).find_each do |customer|
name_words = customer.name.split(' ')
next if name_words.empty?
customer.update(
first_name: name_words.first,
last_name: name_words[1..].join(' ')
)
end
end
end