mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-01 21:47:16 +00:00
Add migration to split Customers.name into first and last names
This commit is contained in:
committed by
Maikel Linke
parent
7c7a09a75c
commit
d0f347efaa
22
db/migrate/20220105085729_split_customers_name.rb
Normal file
22
db/migrate/20220105085729_split_customers_name.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user