From 095080320c470d2f641d247d1f958d63bd526223 Mon Sep 17 00:00:00 2001 From: giovanni Date: Tue, 27 Jan 2026 21:20:17 +0700 Subject: [PATCH 1/3] add migration for change type data id tables module daily checklist --- ...pe_data_column_id_daily_checklist.down.sql | 55 ++++++++++++++++++ ...type_data_column_id_daily_checklist.up.sql | 58 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql create mode 100644 internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql diff --git a/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql new file mode 100644 index 00000000..dccb2ae4 --- /dev/null +++ b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql @@ -0,0 +1,55 @@ +BEGIN; + +DO $$ +DECLARE + t text; + seq_name text; +BEGIN + FOREACH t IN ARRAY ARRAY[ + 'daily_checklist_activity_task_assignments', + 'daily_checklist_activity_tasks', + 'daily_checklist_phases', + 'daily_checklist_tasks', + 'employee_kandangs', + 'employees', + 'phase_activities', + 'phases' + ] + LOOP + -- Sequence name convention + seq_name := format('public.%I_id_seq', t); + + -- 1) Drop default nextval (bigserial behavior) + EXECUTE format( + 'ALTER TABLE public.%I ALTER COLUMN id DROP DEFAULT', + t + ); + + -- 2) Add IDENTITY back (BY DEFAULT is safer for rollback) + EXECUTE format( + 'ALTER TABLE public.%I ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY', + t + ); + + -- 3) Detach & optionally drop sequence (safe) + IF EXISTS ( + SELECT 1 FROM pg_class + WHERE relkind = 'S' + AND relname = t || '_id_seq' + ) THEN + EXECUTE format( + 'ALTER SEQUENCE %s OWNED BY NONE', + seq_name + ); + + -- Optional: drop sequence (comment if you want to keep it) + EXECUTE format( + 'DROP SEQUENCE IF EXISTS %s', + seq_name + ); + END IF; + + END LOOP; +END $$; + +COMMIT; diff --git a/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql new file mode 100644 index 00000000..9c7ce8f7 --- /dev/null +++ b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql @@ -0,0 +1,58 @@ +BEGIN; + +DO $$ +DECLARE + t text; + seq_name text; + max_id bigint; +BEGIN + FOREACH t IN ARRAY ARRAY[ + 'daily_checklist_activity_task_assignments', + 'daily_checklist_activity_tasks', + 'daily_checklist_phases', + 'daily_checklist_tasks', + 'employee_kandangs', + 'employees', + 'phase_activities', + 'phases' + ] + LOOP + -- Sequence name convention: public._id_seq + seq_name := format('public.%I_id_seq', t); + + -- Drop IDENTITY only if the column is identity (safe to re-run) + IF EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_schema = 'public' + AND table_name = t + AND column_name = 'id' + AND is_identity = 'YES' + ) THEN + EXECUTE format('ALTER TABLE public.%I ALTER COLUMN id DROP IDENTITY', t); + END IF; + + -- Ensure sequence exists + EXECUTE format('CREATE SEQUENCE IF NOT EXISTS %s', seq_name); + + -- Set default like bigserial + EXECUTE format( + 'ALTER TABLE public.%I ALTER COLUMN id SET DEFAULT nextval(''%s'')', + t, seq_name + ); + + -- Own the sequence by the column + EXECUTE format( + 'ALTER SEQUENCE %s OWNED BY public.%I.id', + seq_name, t + ); + + -- Sync sequence to MAX(id) + 1 to avoid duplicate key + EXECUTE format('SELECT COALESCE(MAX(id), 0) FROM public.%I', t) INTO max_id; + + EXECUTE format('SELECT setval(''%s'', $1, false)', seq_name) + USING (max_id + 1); + END LOOP; +END $$; + +COMMIT; From 3d39d6d31e93fca735ae2adba09423ca50b8b743 Mon Sep 17 00:00:00 2001 From: giovanni Date: Wed, 28 Jan 2026 09:46:34 +0700 Subject: [PATCH 2/3] adjust validation select phase --- ...036_change_type_data_column_id_daily_checklist.down.sql | 1 + ...14036_change_type_data_column_id_daily_checklist.up.sql | 1 + .../modules/closings/repositories/closing.repository.go | 7 ++++--- .../validations/daily-checklist.validation.go | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql index dccb2ae4..ee02950a 100644 --- a/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql +++ b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.down.sql @@ -10,6 +10,7 @@ BEGIN 'daily_checklist_activity_tasks', 'daily_checklist_phases', 'daily_checklist_tasks', + 'daily_checklists', 'employee_kandangs', 'employees', 'phase_activities', diff --git a/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql index 9c7ce8f7..3c1913cb 100644 --- a/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql +++ b/internal/database/migrations/20260127114036_change_type_data_column_id_daily_checklist.up.sql @@ -11,6 +11,7 @@ BEGIN 'daily_checklist_activity_tasks', 'daily_checklist_phases', 'daily_checklist_tasks', + 'daily_checklists', 'employee_kandangs', 'employees', 'phase_activities', diff --git a/internal/modules/closings/repositories/closing.repository.go b/internal/modules/closings/repositories/closing.repository.go index f479306c..3363d404 100644 --- a/internal/modules/closings/repositories/closing.repository.go +++ b/internal/modules/closings/repositories/closing.repository.go @@ -355,9 +355,10 @@ func (r *ClosingRepositoryImpl) SumMarketingWeightAndQtyByProjectFlockKandangIDs Joins("JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id"). Joins("JOIN products prod ON prod.id = pw.product_id"). Joins("JOIN flags f ON f.flagable_id = prod.id AND f.flagable_type = ?", "products"). + Joins("JOIN marketing_delivery_products mdp ON mdp.marketing_product_id = mp.id"). Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs). Where("f.name IN ?", flagNames). - Select("COALESCE(SUM(mp.total_weight), 0) AS total_weight, COALESCE(SUM(mp.qty), 0) AS total_qty, COALESCE(SUM(mp.total_price), 0) AS total_price"). + Select("COALESCE(SUM(mdp.total_weight), 0) AS total_weight, COALESCE(SUM(mdp.usage_qty), 0) AS total_qty, COALESCE(SUM(mp.total_price), 0) AS total_price"). Scan(&agg).Error if err != nil { return 0, 0, 0, err @@ -797,7 +798,7 @@ func (r *ClosingRepositoryImpl) detailQuery( ) *gorm.DB { db := r.withCtx(ctx). Table(table). - Joins("JOIN product_warehouses pw ON "+pwJoinCond). + Joins("JOIN product_warehouses pw ON " + pwJoinCond). Joins("JOIN products p ON p.id = pw.product_id") db = applyJoins(db, joins...) @@ -1034,7 +1035,7 @@ func (r *ClosingRepositoryImpl) fetchStockLogs(ctx context.Context, kandangID ui COALESCE(sl.increase,0) AS increase, COALESCE(sl.decrease,0) AS decrease, COALESCE(p.product_price,0) AS price, - `+movementSelect+` + ` + movementSelect + ` `). Joins("JOIN product_warehouses pw ON pw.id = sl.product_warehouse_id"). Joins("JOIN products p ON p.id = pw.product_id"). diff --git a/internal/modules/daily-checklists/validations/daily-checklist.validation.go b/internal/modules/daily-checklists/validations/daily-checklist.validation.go index 9157c4e2..353aeaa5 100644 --- a/internal/modules/daily-checklists/validations/daily-checklist.validation.go +++ b/internal/modules/daily-checklists/validations/daily-checklist.validation.go @@ -29,7 +29,7 @@ type Query struct { } type AssignPhases struct { - PhaseIDs string `json:"phase_ids" validate:"required"` + PhaseIDs string `json:"phase_ids" validate:"omitempty"` } type AssignTask struct { From 8086d2fb9ea14bf6040495e62700e0ba6e4cd914 Mon Sep 17 00:00:00 2001 From: giovanni Date: Wed, 28 Jan 2026 09:50:03 +0700 Subject: [PATCH 3/3] adjust query --- internal/modules/closings/repositories/closing.repository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/modules/closings/repositories/closing.repository.go b/internal/modules/closings/repositories/closing.repository.go index 3363d404..04391332 100644 --- a/internal/modules/closings/repositories/closing.repository.go +++ b/internal/modules/closings/repositories/closing.repository.go @@ -358,7 +358,7 @@ func (r *ClosingRepositoryImpl) SumMarketingWeightAndQtyByProjectFlockKandangIDs Joins("JOIN marketing_delivery_products mdp ON mdp.marketing_product_id = mp.id"). Where("pw.project_flock_kandang_id IN ?", projectFlockKandangIDs). Where("f.name IN ?", flagNames). - Select("COALESCE(SUM(mdp.total_weight), 0) AS total_weight, COALESCE(SUM(mdp.usage_qty), 0) AS total_qty, COALESCE(SUM(mp.total_price), 0) AS total_price"). + Select("COALESCE(SUM(mdp.total_weight), 0) AS total_weight, COALESCE(SUM(mdp.usage_qty), 0) AS total_qty, COALESCE(SUM(mdp.total_price), 0) AS total_price"). Scan(&agg).Error if err != nil { return 0, 0, 0, err