From cb1df12b7e9714f28028c8e3995cfb49b1b597c7 Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Mon, 17 Nov 2025 11:03:15 +0700 Subject: [PATCH] Feat[BE-260]: create BOP migration --- ...51117034511_create_expenses_table.down.sql | 1 + ...0251117034511_create_expenses_table.up.sql | 44 ++++++++++++++++ ...29_create_expense_nonstocks_table.down.sql | 1 + ...4529_create_expense_nonstocks_table.up.sql | 50 +++++++++++++++++++ ...create_expense_realizations_table.down.sql | 1 + ...8_create_expense_realizations_table.up.sql | 40 +++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 internal/database/migrations/20251117034511_create_expenses_table.down.sql create mode 100644 internal/database/migrations/20251117034511_create_expenses_table.up.sql create mode 100644 internal/database/migrations/20251117034529_create_expense_nonstocks_table.down.sql create mode 100644 internal/database/migrations/20251117034529_create_expense_nonstocks_table.up.sql create mode 100644 internal/database/migrations/20251117034538_create_expense_realizations_table.down.sql create mode 100644 internal/database/migrations/20251117034538_create_expense_realizations_table.up.sql diff --git a/internal/database/migrations/20251117034511_create_expenses_table.down.sql b/internal/database/migrations/20251117034511_create_expenses_table.down.sql new file mode 100644 index 00000000..bf0ea945 --- /dev/null +++ b/internal/database/migrations/20251117034511_create_expenses_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS expenses; \ No newline at end of file diff --git a/internal/database/migrations/20251117034511_create_expenses_table.up.sql b/internal/database/migrations/20251117034511_create_expenses_table.up.sql new file mode 100644 index 00000000..054084e4 --- /dev/null +++ b/internal/database/migrations/20251117034511_create_expenses_table.up.sql @@ -0,0 +1,44 @@ +CREATE TABLE expenses ( + id BIGSERIAL PRIMARY KEY, + reference_number VARCHAR, -- format => BOP-LTI-0001 = 0001 is increment + supplier_id BIGINT NULL, + category VARCHAR(50) NOT NULL CHECK ( + category IN ('BOP', 'NON-BOP') + ), + po_number VARCHAR(50) UNIQUE NOT NULL, + document_path JSON, + expense_date DATE NOT NULL, + grand_total NUMERIC(15, 3) DEFAULT 0, + note TEXT, + created_by BIGINT, + created_at TIMESTAMPTZ DEFAULT now(), + updated_at TIMESTAMPTZ DEFAULT now(), + deleted_at TIMESTAMPTZ +); + +-- Tambahkan Foreign Key ke suppliers +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'suppliers') THEN + ALTER TABLE expenses + ADD CONSTRAINT fk_expenses_supplier_id + FOREIGN KEY (supplier_id) REFERENCES suppliers(id); + END IF; +END $$; + +-- Tambahkan Foreign Key ke users (created_by) +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN + ALTER TABLE expenses + ADD CONSTRAINT fk_expenses_created_by + FOREIGN KEY (created_by) REFERENCES users(id); + END IF; +END $$; + +-- Index +CREATE INDEX idx_expenses_supplier_id ON expenses (supplier_id); + +CREATE INDEX idx_expenses_expense_date ON expenses (expense_date); + +CREATE INDEX idx_expenses_deleted_at ON expenses (deleted_at); \ No newline at end of file diff --git a/internal/database/migrations/20251117034529_create_expense_nonstocks_table.down.sql b/internal/database/migrations/20251117034529_create_expense_nonstocks_table.down.sql new file mode 100644 index 00000000..70fcf148 --- /dev/null +++ b/internal/database/migrations/20251117034529_create_expense_nonstocks_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS expense_nonstocks; \ No newline at end of file diff --git a/internal/database/migrations/20251117034529_create_expense_nonstocks_table.up.sql b/internal/database/migrations/20251117034529_create_expense_nonstocks_table.up.sql new file mode 100644 index 00000000..5b0c2c16 --- /dev/null +++ b/internal/database/migrations/20251117034529_create_expense_nonstocks_table.up.sql @@ -0,0 +1,50 @@ +CREATE TABLE expense_nonstocks ( + id BIGSERIAL PRIMARY KEY, + expense_id BIGINT, + project_flock_kandang_id BIGINT, + nonstock_id BIGINT, + qty NUMERIC(15, 3) NOT NULL, + unit_price NUMERIC(15, 3) NOT NULL, + total_price NUMERIC(15, 3) NOT NULL, + note TEXT NULL, + created_at TIMESTAMPTZ DEFAULT now(), + updated_at TIMESTAMPTZ DEFAULT now(), + deleted_at TIMESTAMPTZ +); + +-- Tambahkan Foreign Key ke expenses +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'expenses') THEN + ALTER TABLE expense_nonstocks + ADD CONSTRAINT fk_expense_nonstocks_expense_id + FOREIGN KEY (expense_id) REFERENCES expenses(id); + END IF; +END $$; + +-- Tambahkan Foreign Key ke project_flock_kandangs +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN + ALTER TABLE expense_nonstocks + ADD CONSTRAINT fk_expense_nonstocks_kandang_id + FOREIGN KEY (project_flock_kandang_id) REFERENCES project_flock_kandangs(id); + END IF; +END $$; + +-- Tambahkan Foreign Key ke nonstocks +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'nonstocks') THEN + ALTER TABLE expense_nonstocks + ADD CONSTRAINT fk_expense_nonstocks_nonstock_id + FOREIGN KEY (nonstock_id) REFERENCES nonstocks(id); + END IF; +END $$; + +-- Index +CREATE INDEX idx_expense_nonstocks_expense_id ON expense_nonstocks (expense_id); + +CREATE INDEX idx_expense_nonstocks_nonstock_id ON expense_nonstocks (nonstock_id); + +CREATE INDEX idx_expense_nonstocks_deleted_at ON expense_nonstocks (deleted_at); \ No newline at end of file diff --git a/internal/database/migrations/20251117034538_create_expense_realizations_table.down.sql b/internal/database/migrations/20251117034538_create_expense_realizations_table.down.sql new file mode 100644 index 00000000..5f70a0e6 --- /dev/null +++ b/internal/database/migrations/20251117034538_create_expense_realizations_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS expense_realizations; \ No newline at end of file diff --git a/internal/database/migrations/20251117034538_create_expense_realizations_table.up.sql b/internal/database/migrations/20251117034538_create_expense_realizations_table.up.sql new file mode 100644 index 00000000..4a8dc148 --- /dev/null +++ b/internal/database/migrations/20251117034538_create_expense_realizations_table.up.sql @@ -0,0 +1,40 @@ +CREATE TABLE expense_realizations ( + id BIGSERIAL PRIMARY KEY, + expense_nonstock_id BIGINT, + realization_qty NUMERIC(15, 3) NOT NULL, + realization_unit_price NUMERIC(15, 3) NOT NULL, + realization_total_price NUMERIC(15, 3) NOT NULL, + realization_date DATE NOT NULL, + note TEXT, + created_by BIGINT, + created_at TIMESTAMPTZ DEFAULT now(), + updated_at TIMESTAMPTZ DEFAULT now(), + deleted_at TIMESTAMPTZ +); + +-- Tambahkan Foreign Key ke expense_nonstocks +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'expense_nonstocks') THEN + ALTER TABLE expense_realizations + ADD CONSTRAINT fk_expense_realizations_nonstock_id + FOREIGN KEY (expense_nonstock_id) REFERENCES expense_nonstocks(id); + END IF; +END $$; + +-- Tambahkan Foreign Key ke users (created_by) +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN + ALTER TABLE expense_realizations + ADD CONSTRAINT fk_expense_realizations_created_by + FOREIGN KEY (created_by) REFERENCES users(id); + END IF; +END $$; + +-- Index +CREATE INDEX idx_expense_realizations_nonstock_id ON expense_realizations (expense_nonstock_id); + +CREATE INDEX idx_expense_realizations_date ON expense_realizations (realization_date); + +CREATE INDEX idx_expense_realizations_deleted_at ON expense_realizations (deleted_at); \ No newline at end of file