Feat[BE-260]: create BOP migration

This commit is contained in:
aguhh18
2025-11-17 11:03:15 +07:00
parent 60757237c0
commit cb1df12b7e
6 changed files with 137 additions and 0 deletions
@@ -0,0 +1 @@
DROP TABLE IF EXISTS expenses;
@@ -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);
@@ -0,0 +1 @@
DROP TABLE IF EXISTS expense_nonstocks;
@@ -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);
@@ -0,0 +1 @@
DROP TABLE IF EXISTS expense_realizations;
@@ -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);