From 2a101ed0db09bf4f65c1df8bcad613e8ce475499 Mon Sep 17 00:00:00 2001 From: giovanni Date: Fri, 5 Jun 2026 12:58:08 +0700 Subject: [PATCH] fix fk empty kandang to kandang_group --- ...daily_checklist_empty_kandangs_fk.down.sql | 10 ++++++++ ...x_daily_checklist_empty_kandangs_fk.up.sql | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.down.sql create mode 100644 internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.up.sql diff --git a/internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.down.sql b/internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.down.sql new file mode 100644 index 00000000..b71696d3 --- /dev/null +++ b/internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.down.sql @@ -0,0 +1,10 @@ +BEGIN; + +ALTER TABLE daily_checklist_empty_kandangs + DROP CONSTRAINT IF EXISTS fk_dcek_kandang; + +ALTER TABLE daily_checklist_empty_kandangs + ADD CONSTRAINT fk_dcek_kandang + FOREIGN KEY (kandang_id) REFERENCES kandangs (id) ON DELETE CASCADE; + +COMMIT; diff --git a/internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.up.sql b/internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.up.sql new file mode 100644 index 00000000..e7b755a6 --- /dev/null +++ b/internal/database/migrations/20260605055720_fix_daily_checklist_empty_kandangs_fk.up.sql @@ -0,0 +1,23 @@ +BEGIN; + +ALTER TABLE daily_checklist_empty_kandangs + DROP CONSTRAINT IF EXISTS fk_dcek_kandang; + +DO $$ +BEGIN + IF EXISTS ( + SELECT 1 + FROM daily_checklist_empty_kandangs dcek + LEFT JOIN kandang_groups kg ON kg.id = dcek.kandang_id + WHERE kg.id IS NULL + AND dcek.deleted_at IS NULL + ) THEN + RAISE EXCEPTION 'Cannot fix FK: some kandang_id values do not exist in kandang_groups'; + END IF; +END $$; + +ALTER TABLE daily_checklist_empty_kandangs + ADD CONSTRAINT fk_dcek_kandang + FOREIGN KEY (kandang_id) REFERENCES kandang_groups (id) ON DELETE CASCADE; + +COMMIT;