Added customer composite key

This commit is contained in:
Nedim
2025-02-27 07:45:54 +01:00
parent a28cdc6a6d
commit 92a61159ca
35 changed files with 1776 additions and 115 deletions

View File

@@ -0,0 +1,97 @@
class ModifyCustomersTable < ActiveRecord::Migration[7.1]
def up
# First, remove the foreign key constraint from reservations
remove_foreign_key :reservations, :customers if foreign_key_exists?(:reservations, :customers)
# Create temporary columns without constraints
add_column :customers, :first_name, :string unless column_exists?(:customers, :first_name)
add_column :customers, :surname, :string unless column_exists?(:customers, :surname)
add_column :customers, :original_phone, :string unless column_exists?(:customers, :original_phone)
# Copy data
execute <<-SQL
UPDATE customers
SET first_name = name,
surname = '',
original_phone = phone
SQL
# Now add the NOT NULL constraints
change_column_null :customers, :surname, false
change_column_null :customers, :original_phone, false
# Remove old name column
remove_column :customers, :name if column_exists?(:customers, :name)
# Remove existing indexes
remove_index :customers, name: :index_customers_composite_with_company, if_exists: true
remove_index :customers, name: :index_customers_on_name_and_company_id, if_exists: true
remove_index :customers, name: :index_customers_on_company_id, if_exists: true
remove_index :customers, name: :index_customers_on_composite_key, if_exists: true
remove_index :customers, name: :index_customers_on_composite_key_and_company, if_exists: true
# Create new indexes
add_index :customers, [:first_name, :surname, :original_phone],
unique: true,
name: 'index_customers_on_composite_key'
add_index :customers, [:first_name, :surname, :original_phone, :company_id],
unique: true,
name: 'index_customers_on_composite_key_and_company'
# Add a non-unique index on company_id for performance
add_index :customers, :company_id,
name: 'index_customers_on_company_id'
# Add new columns to reservations
add_column :reservations, :customer_first_name, :string
add_column :reservations, :customer_surname, :string
add_column :reservations, :customer_original_phone, :string
# Update reservations data
execute <<-SQL
UPDATE reservations
SET customer_first_name = (SELECT first_name FROM customers WHERE customers.id = reservations.customer_id),
customer_surname = (SELECT surname FROM customers WHERE customers.id = reservations.customer_id),
customer_original_phone = (SELECT original_phone FROM customers WHERE customers.id = reservations.customer_id)
SQL
# Add foreign key constraint
add_foreign_key :reservations, :customers,
primary_key: [:first_name, :surname, :original_phone],
column: [:customer_first_name, :customer_surname, :customer_original_phone]
# Remove old columns
remove_column :customers, :id if column_exists?(:customers, :id)
remove_column :reservations, :customer_id if column_exists?(:reservations, :customer_id)
end
def down
# Add back the id columns
add_column :customers, :id, :primary_key
add_column :reservations, :customer_id, :integer
# Remove the composite foreign key
remove_foreign_key :reservations, :customers if foreign_key_exists?(:reservations, :customers)
remove_column :reservations, :customer_first_name
remove_column :reservations, :customer_surname
remove_column :reservations, :customer_original_phone
# Add back the original foreign key
add_foreign_key :reservations, :customers
# Drop composite indexes safely
execute <<-SQL
DROP INDEX IF EXISTS index_customers_on_composite_key;
DROP INDEX IF EXISTS index_customers_on_composite_key_and_company;
SQL
# Restore the original columns
add_column :customers, :name, :string
execute "UPDATE customers SET name = first_name"
remove_column :customers, :first_name
remove_column :customers, :surname
remove_column :customers, :original_phone
end
end

19
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2025_02_17_185300) do
ActiveRecord::Schema[7.1].define(version: 2025_02_18_071800) do
create_table "companies", force: :cascade do |t|
t.string "name"
t.string "id_number"
@@ -25,8 +25,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_02_17_185300) do
t.datetime "updated_at", null: false
end
create_table "customers", force: :cascade do |t|
t.string "name"
create_table "customers", id: false, force: :cascade do |t|
t.string "phone"
t.text "notes"
t.string "email"
@@ -34,12 +33,16 @@ ActiveRecord::Schema[7.1].define(version: 2025_02_17_185300) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "company_id"
t.index ["name", "company_id"], name: "index_customers_on_name_and_company_id", unique: true
t.string "first_name"
t.string "surname", null: false
t.string "original_phone", null: false
t.index ["company_id"], name: "index_customers_on_company_id"
t.index ["first_name", "surname", "original_phone", "company_id"], name: "index_customers_on_composite_key_and_company", unique: true
t.index ["first_name", "surname", "original_phone"], name: "index_customers_on_composite_key", unique: true
end
create_table "reservations", force: :cascade do |t|
t.integer "company_id", null: false
t.integer "customer_id", null: false
t.string "title"
t.text "description"
t.datetime "start_time"
@@ -47,8 +50,10 @@ ActiveRecord::Schema[7.1].define(version: 2025_02_17_185300) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "team_id", null: false
t.string "customer_first_name"
t.string "customer_surname"
t.string "customer_original_phone"
t.index ["company_id"], name: "index_reservations_on_company_id"
t.index ["customer_id"], name: "index_reservations_on_customer_id"
t.index ["team_id"], name: "index_reservations_on_team_id"
end
@@ -61,7 +66,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_02_17_185300) do
end
add_foreign_key "reservations", "companies"
add_foreign_key "reservations", "customers"
add_foreign_key "reservations", "customers", column: ["customer_first_name", "customer_surname", "customer_original_phone"], primary_key: ["first_name", "surname", "original_phone"]
add_foreign_key "reservations", "teams"
add_foreign_key "teams", "companies"
end