Add migration to split Customers.name into first and last names

This commit is contained in:
Adrien Chauve
2022-01-06 16:37:31 +01:00
committed by Maikel Linke
parent 7c7a09a75c
commit d0f347efaa
2 changed files with 25 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
class SplitCustomersName < ActiveRecord::Migration[6.1]
class Customer < ActiveRecord::Base
end
def change
add_column :customers, :first_name, :string
add_column :customers, :last_name, :string
rename_column :customers, :name, :backup_name
reversible do |dir|
dir.up { migrate_customer_name_data }
end
end
def migrate_customer_name_data
Customer.where("backup_name LIKE '% %'").find_each do |customer|
first_name, last_name = customer.backup_name.split(' ', 2)
customer.first_name = first_name
customer.last_name = last_name
customer.save
end
end
end

View File

@@ -51,9 +51,11 @@ 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 "name", limit: 255
t.string "backup_name", limit: 255
t.boolean "allow_charges", default: false, null: false
t.datetime "terms_and_conditions_accepted_at"
t.string "first_name"
t.string "last_name"
t.index ["bill_address_id"], name: "index_customers_on_bill_address_id"
t.index ["email"], name: "index_customers_on_email"
t.index ["enterprise_id", "code"], name: "index_customers_on_enterprise_id_and_code", unique: true