Skip to content

Commit 62d7113

Browse files
committed
feat(seeder): add seed status and reset commands
- Add `seed:status` command to show the status of goose seed migrations. - Add `seed:reset` command to reset all seeders (down + up). - Update `migrateFresh` to use goose seeder table for seed migrations. - Improve error handling and argument validation in seeder commands.
1 parent 647d347 commit 62d7113

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Execute() {
2323
config.LoadConfig()
2424

2525
// Adding child commands
26-
rootCmd.AddCommand(createMigration, serveHttpCmd, migrateUp, migrateDown, migrateFresh, createSeeder, seedUp)
26+
rootCmd.AddCommand(createMigration, serveHttpCmd, migrateUp, migrateDown, migrateFresh, createSeeder, seedUp, seedStatus, seedReset)
2727

2828
// cmd execute
2929
if err := rootCmd.Execute(); err != nil {

cmd/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var migrateFresh = &cobra.Command{
133133

134134
// 4. Run seeders
135135
seedDir := "database/seeder"
136-
seedCmd := exec.Command("goose", "-dir", seedDir, "postgres", cfg.DB_POSTGRES_DSN, "up")
136+
seedCmd := exec.Command("goose", "-dir", seedDir, "-table", "goose_db_version_seeder", "postgres", cfg.DB_POSTGRES_DSN, "up")
137137
seedCmd.Stdout = os.Stdout
138138
seedCmd.Stderr = os.Stderr
139139

cmd/seeder.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ var createSeeder = &cobra.Command{
1919
Long: "seed create",
2020
Run: func(cmd *cobra.Command, args []string) {
2121

22+
if len(args) < 1 {
23+
logrus.Error("Seeder name is required, e.g. `go run main.go seed:create users`")
24+
return
25+
}
2226
gooseCmd := exec.Command("goose", "create", "seed_"+args[0], "-dir", seederDir, "sql")
2327
gooseCmd.Stdout = os.Stdout
2428
gooseCmd.Stderr = os.Stderr
@@ -49,3 +53,46 @@ var seedUp = &cobra.Command{
4953

5054
},
5155
}
56+
57+
var seedStatus = &cobra.Command{
58+
Use: "seed:status",
59+
Short: "show seed status",
60+
Long: "show status of goose seed migrations",
61+
Run: func(cmd *cobra.Command, args []string) {
62+
cfg := config.GetConfig()
63+
gooseCmd := exec.Command("goose", "postgres", cfg.DB_POSTGRES_DSN, "status", "-dir", seederDir)
64+
gooseCmd.Stdout = os.Stdout
65+
gooseCmd.Stderr = os.Stderr
66+
67+
if err := gooseCmd.Run(); err != nil {
68+
logrus.Error("goose seed status error: ", err)
69+
return
70+
}
71+
},
72+
}
73+
var seedReset = &cobra.Command{
74+
Use: "seed:reset",
75+
Short: "seed reset (down + up)",
76+
Long: "reset all seeders",
77+
Run: func(cmd *cobra.Command, args []string) {
78+
cfg := config.GetConfig()
79+
80+
// Down
81+
down := exec.Command("goose", "postgres", cfg.DB_POSTGRES_DSN, "down", "-dir", seederDir)
82+
down.Stdout = os.Stdout
83+
down.Stderr = os.Stderr
84+
if err := down.Run(); err != nil {
85+
logrus.Error("goose seed down error: ", err)
86+
return
87+
}
88+
89+
// Up
90+
up := exec.Command("goose", "postgres", cfg.DB_POSTGRES_DSN, "up", "-dir", seederDir)
91+
up.Stdout = os.Stdout
92+
up.Stderr = os.Stderr
93+
if err := up.Run(); err != nil {
94+
logrus.Error("goose seed up error: ", err)
95+
return
96+
}
97+
},
98+
}

database/migration/20250522034141_table_add_status_and_userid_to_logout.sql

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
-- +goose Up
22
-- +goose StatementBegin
3-
ALTER TABLE public.logout
4-
ADD COLUMN status smallint DEFAULT 1,
5-
ADD COLUMN user_id int8;
3+
DO $$
4+
BEGIN
5+
-- Kolom status
6+
IF NOT EXISTS (
7+
SELECT 1 FROM information_schema.columns
8+
WHERE table_name = 'logout' AND column_name = 'status'
9+
) THEN
10+
ALTER TABLE public.logout ADD COLUMN status smallint DEFAULT 1;
11+
END IF;
612

7-
ALTER TABLE public.logout
8-
ADD CONSTRAINT fk_logout_user FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE SET NULL;
13+
-- Kolom user_id
14+
IF NOT EXISTS (
15+
SELECT 1 FROM information_schema.columns
16+
WHERE table_name = 'logout' AND column_name = 'user_id'
17+
) THEN
18+
ALTER TABLE public.logout ADD COLUMN user_id int8;
19+
END IF;
920

10-
ALTER TABLE public.logout
11-
ADD CONSTRAINT chk_logout_status CHECK (status IN (0, 1));
21+
-- Constraint foreign key
22+
BEGIN
23+
ALTER TABLE public.logout ADD CONSTRAINT fk_logout_user
24+
FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE SET NULL;
25+
EXCEPTION WHEN duplicate_object THEN NULL;
26+
END;
27+
28+
-- Constraint CHECK status
29+
BEGIN
30+
ALTER TABLE public.logout ADD CONSTRAINT chk_logout_status
31+
CHECK (status IN (0, 1));
32+
EXCEPTION WHEN duplicate_object THEN NULL;
33+
END;
34+
END $$;
1235
-- +goose StatementEnd
1336

1437
-- +goose Down

0 commit comments

Comments
 (0)