From b156e06cee6ecaebe617e53afdc9686d23a67afe Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Fri, 26 Dec 2025 23:36:53 +0700 Subject: [PATCH] Feat[BE]: add migration scripts for product warehouse ID management and create production standards tables with constraints and indexes --- ...d_to_marketing_delivery_products.down.sql} | 0 ..._id_to_marketing_delivery_products.up.sql} | 0 ...reate_production_standards_tables.down.sql | 10 ++++ ..._create_production_standards_tables.up.sql | 54 +++++++++++++++++++ 4 files changed, 64 insertions(+) rename internal/database/migrations/{20251226114218_add.down.sql => 20251226114218_add_product_warehouse_id_to_marketing_delivery_products.down.sql} (100%) rename internal/database/migrations/{20251226114218_add.up.sql => 20251226114218_add_product_warehouse_id_to_marketing_delivery_products.up.sql} (100%) create mode 100644 internal/database/migrations/20251226161036_create_production_standards_tables.down.sql create mode 100644 internal/database/migrations/20251226161036_create_production_standards_tables.up.sql diff --git a/internal/database/migrations/20251226114218_add.down.sql b/internal/database/migrations/20251226114218_add_product_warehouse_id_to_marketing_delivery_products.down.sql similarity index 100% rename from internal/database/migrations/20251226114218_add.down.sql rename to internal/database/migrations/20251226114218_add_product_warehouse_id_to_marketing_delivery_products.down.sql diff --git a/internal/database/migrations/20251226114218_add.up.sql b/internal/database/migrations/20251226114218_add_product_warehouse_id_to_marketing_delivery_products.up.sql similarity index 100% rename from internal/database/migrations/20251226114218_add.up.sql rename to internal/database/migrations/20251226114218_add_product_warehouse_id_to_marketing_delivery_products.up.sql diff --git a/internal/database/migrations/20251226161036_create_production_standards_tables.down.sql b/internal/database/migrations/20251226161036_create_production_standards_tables.down.sql new file mode 100644 index 00000000..f5cc2237 --- /dev/null +++ b/internal/database/migrations/20251226161036_create_production_standards_tables.down.sql @@ -0,0 +1,10 @@ +-- Drop indexes +DROP INDEX IF EXISTS idx_standard_growth_details_standard_week; +DROP INDEX IF EXISTS idx_production_standard_details_standard_week; +DROP INDEX IF EXISTS idx_production_standards_project_category; +DROP INDEX IF EXISTS idx_production_standards_deleted_at; + +-- Drop tables (in reverse order due to foreign keys) +DROP TABLE IF EXISTS standard_growth_details; +DROP TABLE IF EXISTS production_standard_details; +DROP TABLE IF EXISTS production_standards; diff --git a/internal/database/migrations/20251226161036_create_production_standards_tables.up.sql b/internal/database/migrations/20251226161036_create_production_standards_tables.up.sql new file mode 100644 index 00000000..61aa3071 --- /dev/null +++ b/internal/database/migrations/20251226161036_create_production_standards_tables.up.sql @@ -0,0 +1,54 @@ +-- Create production_standards table +CREATE TABLE IF NOT EXISTS production_standards ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(100) UNIQUE NOT NULL, + project_category VARCHAR(20) NOT NULL CHECK (project_category IN ('GROWING', 'LAYING')), + created_at TIMESTAMPTZ DEFAULT NOW(), + updated_at TIMESTAMPTZ DEFAULT NOW(), + deleted_at TIMESTAMPTZ, + created_by BIGINT NOT NULL +); + +-- Create index for deleted_at (soft delete) +CREATE INDEX idx_production_standards_deleted_at ON production_standards(deleted_at); + +-- Create production_standard_details table +CREATE TABLE IF NOT EXISTS production_standard_details ( + id BIGSERIAL PRIMARY KEY, + production_standard_id BIGINT NOT NULL, + week INT NOT NULL, + target_hen_day_production NUMERIC(15, 3), + target_hen_house_production NUMERIC(15, 3), + target_egg_weight NUMERIC(15, 3), + target_egg_mass NUMERIC(15, 3), + created_at TIMESTAMPTZ DEFAULT NOW(), + updated_at TIMESTAMPTZ DEFAULT NOW(), + CONSTRAINT fk_production_standard_details_standard FOREIGN KEY (production_standard_id) + REFERENCES production_standards(id) ON DELETE CASCADE +); + +-- Create unique constraint for standard_id + week +CREATE UNIQUE INDEX idx_production_standard_details_standard_week + ON production_standard_details(production_standard_id, week); + +-- Create standard_growth_details table +CREATE TABLE IF NOT EXISTS standard_growth_details ( + id BIGSERIAL PRIMARY KEY, + production_standard_id BIGINT NOT NULL, + target_mean_bw INT, + max_depletion NUMERIC(15, 3), + min_uniformity NUMERIC(15, 3) NOT NULL, + week INT NOT NULL, + feed_intake INT, + created_at TIMESTAMPTZ DEFAULT NOW(), + created_by BIGINT NOT NULL, + CONSTRAINT fk_standard_growth_details_standard FOREIGN KEY (production_standard_id) + REFERENCES production_standards(id) ON DELETE CASCADE +); + +-- Create unique constraint for standard_id + week +CREATE UNIQUE INDEX idx_standard_growth_details_standard_week + ON standard_growth_details(production_standard_id, week); + +-- Create index for project_category +CREATE INDEX idx_production_standards_project_category ON production_standards(project_category);