diff --git a/db/migrate/20220105085729_split_customers_name.rb b/db/migrate/20220105085729_split_customers_name.rb new file mode 100644 index 0000000000..2d1cd9f5fb --- /dev/null +++ b/db/migrate/20220105085729_split_customers_name.rb @@ -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 \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index b1c9499ddb..3fdb1f4cbf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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