From d8e726213d93b0cb3f51b799ff9e0e06da802398 Mon Sep 17 00:00:00 2001 From: Harkamal Randhawa Date: Mon, 20 Apr 2026 06:02:05 -0600 Subject: [PATCH] idempotent schema indexes --- .../db/migration/V1__target_baseline.sql | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/backend/src/main/resources/db/migration/V1__target_baseline.sql b/backend/src/main/resources/db/migration/V1__target_baseline.sql index 6f03cf58..41210e0a 100644 --- a/backend/src/main/resources/db/migration/V1__target_baseline.sql +++ b/backend/src/main/resources/db/migration/V1__target_baseline.sql @@ -27,7 +27,10 @@ CREATE TABLE IF NOT EXISTS users ( tokenVersion INT NOT NULL DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - CONSTRAINT fk_users_primary_store FOREIGN KEY (primaryStoreId) REFERENCES storeLocation(storeId) ON DELETE SET NULL + CONSTRAINT fk_users_primary_store FOREIGN KEY (primaryStoreId) REFERENCES storeLocation(storeId) ON DELETE SET NULL, + INDEX idx_users_primary_store (primaryStoreId), + INDEX idx_users_role (role), + INDEX idx_users_name (lastName, firstName) ); CREATE TABLE IF NOT EXISTS supplier ( @@ -65,7 +68,8 @@ CREATE TABLE IF NOT EXISTS service_species ( species VARCHAR(50) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (serviceId, species), - CONSTRAINT fk_service_species_service FOREIGN KEY (serviceId) REFERENCES service(serviceId) ON DELETE CASCADE + CONSTRAINT fk_service_species_service FOREIGN KEY (serviceId) REFERENCES service(serviceId) ON DELETE CASCADE, + INDEX idx_service_species_species (species) ); CREATE TABLE IF NOT EXISTS product ( @@ -89,7 +93,9 @@ CREATE TABLE IF NOT EXISTS inventory ( updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT uq_inventory_store_product UNIQUE (storeId, prodId), CONSTRAINT fk_inventory_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId), - CONSTRAINT fk_inventory_product FOREIGN KEY (prodId) REFERENCES product(prodId) + CONSTRAINT fk_inventory_product FOREIGN KEY (prodId) REFERENCES product(prodId), + INDEX idx_inventory_store (storeId), + INDEX idx_inventory_product (prodId) ); CREATE TABLE IF NOT EXISTS productSupplier ( @@ -111,7 +117,8 @@ CREATE TABLE IF NOT EXISTS purchaseOrder ( created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT fk_purchase_order_supplier FOREIGN KEY (supId) REFERENCES supplier(supId), - CONSTRAINT fk_purchase_order_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId) + CONSTRAINT fk_purchase_order_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId), + INDEX idx_purchase_order_store (storeId) ); CREATE TABLE IF NOT EXISTS coupon ( @@ -143,7 +150,11 @@ CREATE TABLE IF NOT EXISTS pet ( created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT fk_pet_owner_user FOREIGN KEY (ownerUserId) REFERENCES users(id) ON DELETE SET NULL, - CONSTRAINT fk_pet_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId) ON DELETE SET NULL + CONSTRAINT fk_pet_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId) ON DELETE SET NULL, + INDEX idx_pet_owner_user (ownerUserId), + INDEX idx_pet_store (storeId), + INDEX idx_pet_species (petSpecies), + INDEX idx_pet_name (petName) ); CREATE TABLE IF NOT EXISTS appointment ( @@ -162,7 +173,12 @@ CREATE TABLE IF NOT EXISTS appointment ( CONSTRAINT fk_appointment_pet FOREIGN KEY (petId) REFERENCES pet(petId) ON DELETE SET NULL, CONSTRAINT fk_appointment_customer FOREIGN KEY (customerId) REFERENCES users(id), CONSTRAINT fk_appointment_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId), - CONSTRAINT fk_appointment_employee FOREIGN KEY (employeeId) REFERENCES users(id) + CONSTRAINT fk_appointment_employee FOREIGN KEY (employeeId) REFERENCES users(id), + INDEX idx_appointment_store (storeId), + INDEX idx_appointment_employee (employeeId), + INDEX idx_appointment_customer (customerId), + INDEX idx_appointment_pet (petId), + INDEX idx_appointment_date_status (appointmentDate, appointmentStatus) ); CREATE TABLE IF NOT EXISTS adoption ( @@ -178,7 +194,9 @@ CREATE TABLE IF NOT EXISTS adoption ( CONSTRAINT fk_adoption_pet FOREIGN KEY (petId) REFERENCES pet(petId), CONSTRAINT fk_adoption_customer FOREIGN KEY (customerId) REFERENCES users(id), CONSTRAINT fk_adoption_employee FOREIGN KEY (employeeId) REFERENCES users(id), - CONSTRAINT fk_adoption_source_store FOREIGN KEY (sourceStoreId) REFERENCES storeLocation(storeId) + CONSTRAINT fk_adoption_source_store FOREIGN KEY (sourceStoreId) REFERENCES storeLocation(storeId), + INDEX idx_adoption_store (sourceStoreId), + INDEX idx_adoption_employee (employeeId) ); CREATE TABLE IF NOT EXISTS cart ( @@ -200,7 +218,8 @@ CREATE TABLE IF NOT EXISTS cart ( updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT fk_cart_user FOREIGN KEY (userId) REFERENCES users(id), CONSTRAINT fk_cart_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId) ON DELETE SET NULL, - CONSTRAINT fk_cart_coupon FOREIGN KEY (couponId) REFERENCES coupon(couponId) ON DELETE SET NULL + CONSTRAINT fk_cart_coupon FOREIGN KEY (couponId) REFERENCES coupon(couponId) ON DELETE SET NULL, + INDEX idx_cart_user (userId) ); CREATE TABLE IF NOT EXISTS cart_item ( @@ -243,7 +262,11 @@ CREATE TABLE IF NOT EXISTS sale ( CONSTRAINT fk_sale_customer FOREIGN KEY (customerId) REFERENCES users(id) ON DELETE SET NULL, CONSTRAINT fk_sale_original_sale FOREIGN KEY (originalSaleId) REFERENCES sale(saleId), CONSTRAINT fk_sale_cart FOREIGN KEY (cartId) REFERENCES cart(cartId) ON DELETE SET NULL, - CONSTRAINT fk_sale_coupon FOREIGN KEY (couponId) REFERENCES coupon(couponId) ON DELETE SET NULL + CONSTRAINT fk_sale_coupon FOREIGN KEY (couponId) REFERENCES coupon(couponId) ON DELETE SET NULL, + INDEX idx_sale_store (storeId), + INDEX idx_sale_employee (employeeId), + INDEX idx_sale_customer (customerId), + INDEX idx_sale_date (saleDate) ); CREATE TABLE IF NOT EXISTS saleItem ( @@ -291,12 +314,11 @@ CREATE TABLE IF NOT EXISTS passwordResetToken ( usedAt DATETIME NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, CONSTRAINT uq_password_reset_token_hash UNIQUE (tokenHash), - CONSTRAINT fk_password_reset_token_user FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE + CONSTRAINT fk_password_reset_token_user FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE, + INDEX idx_password_reset_token_user (userId), + INDEX idx_password_reset_token_expires (expiresAt) ); -CREATE INDEX idx_password_reset_token_user ON passwordResetToken(userId); -CREATE INDEX idx_password_reset_token_expires ON passwordResetToken(expiresAt); - CREATE TABLE IF NOT EXISTS conversation ( id BIGINT AUTO_INCREMENT PRIMARY KEY, customerId BIGINT NOT NULL, @@ -307,7 +329,9 @@ CREATE TABLE IF NOT EXISTS conversation ( created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, CONSTRAINT fk_conversation_customer FOREIGN KEY (customerId) REFERENCES users(id), - CONSTRAINT fk_conversation_staff FOREIGN KEY (staffId) REFERENCES users(id) ON DELETE SET NULL + CONSTRAINT fk_conversation_staff FOREIGN KEY (staffId) REFERENCES users(id) ON DELETE SET NULL, + INDEX idx_conversation_customer (customerId), + INDEX idx_conversation_staff (staffId) ); CREATE TABLE IF NOT EXISTS message ( @@ -336,33 +360,8 @@ CREATE TABLE IF NOT EXISTS activityLog ( activity TEXT NOT NULL, logTimestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT fk_activity_log_user FOREIGN KEY (userId) REFERENCES users(id), - CONSTRAINT fk_activity_log_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId) ON DELETE SET NULL + CONSTRAINT fk_activity_log_store FOREIGN KEY (storeId) REFERENCES storeLocation(storeId) ON DELETE SET NULL, + INDEX idx_activity_log_store (storeId), + INDEX idx_activity_log_timestamp_id (logTimestamp, logId) ); -CREATE INDEX idx_users_primary_store ON users(primaryStoreId); -CREATE INDEX idx_users_role ON users(role); -CREATE INDEX idx_users_name ON users(lastName, firstName); -CREATE INDEX idx_service_species_species ON service_species(species); -CREATE INDEX idx_inventory_store ON inventory(storeId); -CREATE INDEX idx_inventory_product ON inventory(prodId); -CREATE INDEX idx_purchase_order_store ON purchaseOrder(storeId); -CREATE INDEX idx_pet_owner_user ON pet(ownerUserId); -CREATE INDEX idx_pet_store ON pet(storeId); -CREATE INDEX idx_pet_species ON pet(petSpecies); -CREATE INDEX idx_pet_name ON pet(petName); -CREATE INDEX idx_appointment_store ON appointment(storeId); -CREATE INDEX idx_appointment_employee ON appointment(employeeId); -CREATE INDEX idx_appointment_customer ON appointment(customerId); -CREATE INDEX idx_appointment_pet ON appointment(petId); -CREATE INDEX idx_appointment_date_status ON appointment(appointmentDate, appointmentStatus); -CREATE INDEX idx_adoption_store ON adoption(sourceStoreId); -CREATE INDEX idx_adoption_employee ON adoption(employeeId); -CREATE INDEX idx_sale_store ON sale(storeId); -CREATE INDEX idx_sale_employee ON sale(employeeId); -CREATE INDEX idx_sale_customer ON sale(customerId); -CREATE INDEX idx_sale_date ON sale(saleDate); -CREATE INDEX idx_cart_user ON cart(userId); -CREATE INDEX idx_conversation_customer ON conversation(customerId); -CREATE INDEX idx_conversation_staff ON conversation(staffId); -CREATE INDEX idx_activity_log_store ON activityLog(storeId); -CREATE INDEX idx_activity_log_timestamp_id ON activityLog(logTimestamp, logId);