Skip to content

Commit b28a02f

Browse files
authored
Merge pull request #16 from hammercode-dev/BE-10/Database-migrate-and-seeder
Be 10/database migrate and seeder
2 parents 5ad026c + c08c8b3 commit b28a02f

11 files changed

+189
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ To set up your environment variables:
3030

3131
Make sure to replace placeholder values with actual data where necessary.
3232

33+
### Running Migrations
34+
35+
Before starting the service, you need to run the database migrations to set up the necessary tables and schema. The project uses `migrate` for managing database migrations.
36+
37+
```bash
38+
go run main.go migrate:up
39+
```
40+
Or if you want to run with seeder you can run this command
41+
42+
```bash
43+
go run main.go migrate:fresh
44+
```
45+
But it will be down your migration first
46+
3347
### Running the Service
3448

3549
To run `lms-be` on your local machine, you have two options:

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, createSeeder, seedUp)
26+
rootCmd.AddCommand(createMigration, serveHttpCmd, migrateUp, migrateDown, migrateFresh, createSeeder, seedUp)
2727

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

cmd/migrate.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,55 @@ var migrateDown = &cobra.Command{
9393

9494
},
9595
}
96+
97+
var migrateFresh = &cobra.Command{
98+
Use: "migrate:fresh",
99+
Short: "migrate fresh",
100+
Long: "migrate fresh",
101+
Run: func(cmd *cobra.Command, args []string) {
102+
cfg := config.GetConfig()
103+
104+
// 1. Reset goose_db_version table first
105+
resetCmd := exec.Command("psql", cfg.DB_POSTGRES_DSN, "-c", "DROP TABLE IF EXISTS goose_db_version;")
106+
resetCmd.Stdout = os.Stdout
107+
resetCmd.Stderr = os.Stderr
108+
109+
if err := resetCmd.Run(); err != nil {
110+
logrus.Error("Error resetting goose_db_version: ", err)
111+
// Continue anyway, as the table might not exist
112+
}
113+
114+
// 2. Run migrations
115+
upCmd := exec.Command("goose", "-dir", migrationDir, "postgres", cfg.DB_POSTGRES_DSN, "up")
116+
upCmd.Stdout = os.Stdout
117+
upCmd.Stderr = os.Stderr
118+
119+
if err := upCmd.Run(); err != nil {
120+
logrus.Error("goose up migration error: ", err)
121+
return
122+
}
123+
124+
// 3. Reset seeders version table
125+
resetSeedCmd := exec.Command("psql", cfg.DB_POSTGRES_DSN, "-c", "DROP TABLE IF EXISTS goose_db_version_seeder;")
126+
resetSeedCmd.Stdout = os.Stdout
127+
resetSeedCmd.Stderr = os.Stderr
128+
129+
if err := resetSeedCmd.Run(); err != nil {
130+
logrus.Error("Error resetting goose_db_version_seeder: ", err)
131+
// Continue anyway
132+
}
133+
134+
// 4. Run seeders
135+
seedDir := "database/seeder"
136+
seedCmd := exec.Command("goose", "-dir", seedDir, "postgres", cfg.DB_POSTGRES_DSN, "up")
137+
seedCmd.Stdout = os.Stdout
138+
seedCmd.Stderr = os.Stderr
139+
140+
if err := seedCmd.Run(); err != nil {
141+
logrus.Error("goose seed error: ", err)
142+
return
143+
}
144+
145+
logrus.Info("Database migration and seeding completed successfully")
146+
},
147+
}

database/migration/20250512101251_table_logout.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS "public"."logout" (
99
"created_at" timestamptz,
1010
PRIMARY KEY ("id")
1111
);
12+
CREATE UNIQUE INDEX IF NOT EXISTS uni_logout_token ON public.logout USING btree (token);
1213

1314
DO $$
1415
BEGIN

database/migration/20250512101543_table_images.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ CREATE TABLE IF NOT EXISTS "public"."images" (
2020
-- +goose Down
2121
-- +goose StatementBegin
2222
DROP TABLE IF EXISTS "public"."images";
23-
DROP SEQUENCE IF EXISTS images_id_seq;- +goose StatementEnd
23+
DROP SEQUENCE IF EXISTS images_id_seq;
2424
-- +goose StatementEnd
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
INSERT INTO "public"."users" (
4+
"username", "email", "password", "role", "fullname",
5+
"date_of_birth", "gender", "phone_number", "address",
6+
"github", "linkedin", "personal_web", "created_at", "updated_at"
7+
) VALUES
8+
-- password : passowrd
9+
('admin', '[email protected]', '$2a$10$zzJJ6MKKBgJT0CfT7rjnWeCAfSRIG6VhBdoqSIWi1VjwBfsp6XcT.', 'admin', 'Admin User',
10+
'1990-01-01', 'Male', '123456789', '123 Admin St',
11+
'github.com/admin', 'linkedin.com/in/admin', 'admin.com', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
12+
13+
-- +goose StatementEnd
14+
15+
-- +goose Down
16+
-- +goose StatementBegin
17+
DELETE FROM "public"."users" WHERE "username" IN ('admin', 'johndoe', 'janedoe');
18+
-- +goose StatementEnd
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
INSERT INTO "public"."events" (
4+
"id", "title", "description", "author", "image", "date",
5+
"reservation_start_date", "reservation_end_date", "type",
6+
"location", "duration", "status", "price", "capacity",
7+
"registration_link", "created_at", "updated_at"
8+
) VALUES
9+
(1, 'Web Development Workshop', 'Learn modern web development techniques',
10+
'Tech Team', 'workshop_banner.jpg', '2025-06-15 09:00:00',
11+
'2025-05-15 00:00:00', '2025-06-14 23:59:59', 'workshop',
12+
'Tech Hub, Floor 3', '8 hours', 'upcoming', 150.00, 30,
13+
'https://example.com/register/web-dev', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
14+
15+
(2, 'Data Science Conference', 'Annual conference for data scientists',
16+
'Data Science Society', 'conference_logo.png', '2025-07-20 08:30:00',
17+
'2025-06-01 00:00:00', '2025-07-15 23:59:59', 'conference',
18+
'Convention Center', '3 days', 'upcoming', 299.99, 200,
19+
'https://example.com/register/data-conf', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
20+
21+
(3, 'Mobile App Hackathon', '48-hour app building competition',
22+
'Mobile Developers Group', 'hackathon_poster.jpg', '2025-08-10 10:00:00',
23+
'2025-07-01 00:00:00', '2025-08-05 23:59:59', 'hackathon',
24+
'Innovation Labs', '48 hours', 'upcoming', 50.00, 100,
25+
'https://example.com/register/app-hackathon', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
26+
-- +goose StatementEnd
27+
28+
-- +goose Down
29+
-- +goose StatementBegin
30+
DELETE FROM "public"."events" WHERE "id" IN (1, 2, 3);
31+
-- +goose StatementEnd
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
INSERT INTO "public"."images" (
4+
"file_name", "file_path", "format", "content_type", "is_used", "file_size"
5+
) VALUES
6+
('workshop_banner.jpg', '/uploads/events/workshop_banner.jpg', 'jpg', 'image/jpeg', true, 256000),
7+
('conference_logo.png', '/uploads/events/conference_logo.png', 'png', 'image/png', true, 128000),
8+
('hackathon_poster.jpg', '/uploads/events/hackathon_poster.jpg', 'jpg', 'image/jpeg', true, 512000),
9+
('payment_proof1.jpg', '/uploads/payments/payment_proof1.jpg', 'jpg', 'image/jpeg', true, 150000),
10+
('payment_proof2.jpg', '/uploads/payments/payment_proof2.jpg', 'jpg', 'image/jpeg', true, 148000);
11+
-- +goose StatementEnd
12+
13+
-- +goose Down
14+
-- +goose StatementBegin
15+
DELETE FROM "public"."images" WHERE "file_name" IN ('workshop_banner.jpg', 'conference_logo.png', 'hackathon_poster.jpg', 'payment_proof1.jpg', 'payment_proof2.jpg');
16+
-- +goose StatementEnd
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
INSERT INTO "public"."event_tags" (
4+
"event_id", "tag"
5+
) VALUES
6+
(1, 'programming'),
7+
(1, 'web-development'),
8+
(1, 'javascript'),
9+
(2, 'data-science'),
10+
(2, 'machine-learning'),
11+
(2, 'big-data'),
12+
(3, 'mobile'),
13+
(3, 'hackathon'),
14+
(3, 'app-development');
15+
-- +goose StatementEnd
16+
17+
-- +goose Down
18+
-- +goose StatementBegin
19+
DELETE FROM "public"."event_tags" WHERE "event_id" IN (1, 2, 3);
20+
-- +goose StatementEnd
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
INSERT INTO "public"."registration_events" (
4+
"order_no", "event_id", "user_id", "name", "email", "phone_number",
5+
"image_proof_payment", "payment_date", "status", "up_to_you", "created_by_user_id"
6+
) VALUES
7+
('ORD-001', 1, '2', 'John Doe', '[email protected]', '987654321',
8+
'payment_proof1.jpg', '2025-06-01 14:30:00', 'confirmed', 'Looking forward to it!', 2),
9+
10+
('ORD-002', 2, '3', 'Jane Doe', '[email protected]', '555123456',
11+
'payment_proof2.jpg', '2025-07-05 10:15:00', 'confirmed', 'Excited to learn!', 3),
12+
13+
('ORD-003', 3, '2', 'John Doe', '[email protected]', '987654321',
14+
NULL, NULL, 'pending', 'Will bring my team', 2);
15+
-- +goose StatementEnd
16+
17+
-- +goose Down
18+
-- +goose StatementBegin
19+
DELETE FROM "public"."registration_events" WHERE "order_no" IN ('ORD-001', 'ORD-002', 'ORD-003');
20+
-- +goose StatementEnd

0 commit comments

Comments
 (0)