From feaa92406aea283f621706bc88ce45579c7e52e4 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 15 Feb 2022 14:52:19 +0000 Subject: [PATCH] Split migration into two parts for easier testing --- .../20220105085729_split_customers_name.rb | 21 ---------------- .../20220105085730_migrate_customers_data.rb | 24 +++++++++++++++++++ 2 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20220105085730_migrate_customers_data.rb diff --git a/db/migrate/20220105085729_split_customers_name.rb b/db/migrate/20220105085729_split_customers_name.rb index ccf7bd364e..d52f122b9c 100644 --- a/db/migrate/20220105085729_split_customers_name.rb +++ b/db/migrate/20220105085729_split_customers_name.rb @@ -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 diff --git a/db/migrate/20220105085730_migrate_customers_data.rb b/db/migrate/20220105085730_migrate_customers_data.rb new file mode 100644 index 0000000000..096465d7b7 --- /dev/null +++ b/db/migrate/20220105085730_migrate_customers_data.rb @@ -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