mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Split migration into two parts for easier testing
This commit is contained in:
committed by
Maikel Linke
parent
68193efcf6
commit
feaa92406a
@@ -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
|
||||
|
||||
24
db/migrate/20220105085730_migrate_customers_data.rb
Normal file
24
db/migrate/20220105085730_migrate_customers_data.rb
Normal 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
|
||||
Reference in New Issue
Block a user