Keep old customers.name column for compatibilty

Our app code will try to access the old attribute while the migration is
running. That would lead to errors during checkout.
This commit is contained in:
Maikel Linke
2022-02-15 11:43:43 +11:00
parent 9a12957e27
commit 68193efcf6
2 changed files with 6 additions and 5 deletions

View File

@@ -1,6 +1,9 @@
# 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
@@ -8,7 +11,6 @@ class SplitCustomersName < ActiveRecord::Migration[6.1]
def up
add_column :customers, :first_name, :string, null: false, default: ""
add_column :customers, :last_name, :string, null: false, default: ""
rename_column :customers, :name, :backup_name
migrate_customer_name_data!
end
@@ -16,12 +18,11 @@ class SplitCustomersName < ActiveRecord::Migration[6.1]
def down
remove_column :customers, :first_name
remove_column :customers, :last_name
rename_column :customers, :backup_name, :name
end
def migrate_customer_name_data!
Customer.where(first_name: "", last_name: "").where.not(backup_name: [nil, ""]).find_each do |customer|
name_words = customer.backup_name.split(' ')
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(

View File

@@ -51,7 +51,7 @@ ActiveRecord::Schema.define(version: 2022_01_18_053107) do
t.datetime "updated_at", null: false
t.integer "bill_address_id"
t.integer "ship_address_id"
t.string "backup_name", limit: 255
t.string "name", limit: 255
t.boolean "allow_charges", default: false, null: false
t.datetime "terms_and_conditions_accepted_at"
t.string "first_name", default: "", null: false