Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ To set up your environment variables:

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

### Running Migrations

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.

```bash
go run main.go migrate:up
```
Or if you want to run with seeder you can run this command

```bash
go run main.go migrate:fresh
```
But it will be down your migration first

### Running the Service

To run `lms-be` on your local machine, you have two options:
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Execute() {
config.LoadConfig()

// Adding child commands
rootCmd.AddCommand(createMigration, serveHttpCmd, migrateUp, migrateDown, createSeeder, seedUp)
rootCmd.AddCommand(createMigration, serveHttpCmd, migrateUp, migrateDown, migrateFresh, createSeeder, seedUp)

// cmd execute
if err := rootCmd.Execute(); err != nil {
Expand Down
52 changes: 52 additions & 0 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,55 @@ var migrateDown = &cobra.Command{

},
}

var migrateFresh = &cobra.Command{
Use: "migrate:fresh",
Short: "migrate fresh",
Long: "migrate fresh",
Run: func(cmd *cobra.Command, args []string) {
cfg := config.GetConfig()

// 1. Reset goose_db_version table first
resetCmd := exec.Command("psql", cfg.DB_POSTGRES_DSN, "-c", "DROP TABLE IF EXISTS goose_db_version;")
resetCmd.Stdout = os.Stdout
resetCmd.Stderr = os.Stderr

if err := resetCmd.Run(); err != nil {
logrus.Error("Error resetting goose_db_version: ", err)
// Continue anyway, as the table might not exist
}

// 2. Run migrations
upCmd := exec.Command("goose", "-dir", migrationDir, "postgres", cfg.DB_POSTGRES_DSN, "up")
upCmd.Stdout = os.Stdout
upCmd.Stderr = os.Stderr

if err := upCmd.Run(); err != nil {
logrus.Error("goose up migration error: ", err)
return
}

// 3. Reset seeders version table
resetSeedCmd := exec.Command("psql", cfg.DB_POSTGRES_DSN, "-c", "DROP TABLE IF EXISTS goose_db_version_seeder;")
resetSeedCmd.Stdout = os.Stdout
resetSeedCmd.Stderr = os.Stderr

if err := resetSeedCmd.Run(); err != nil {
logrus.Error("Error resetting goose_db_version_seeder: ", err)
// Continue anyway
}

// 4. Run seeders
seedDir := "database/seeder"
seedCmd := exec.Command("goose", "-dir", seedDir, "postgres", cfg.DB_POSTGRES_DSN, "up")
seedCmd.Stdout = os.Stdout
seedCmd.Stderr = os.Stderr

if err := seedCmd.Run(); err != nil {
logrus.Error("goose seed error: ", err)
return
}

logrus.Info("Database migration and seeding completed successfully")
},
}
1 change: 1 addition & 0 deletions database/migration/20250512101251_table_logout.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS "public"."logout" (
"created_at" timestamptz,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX IF NOT EXISTS uni_logout_token ON public.logout USING btree (token);

DO $$
BEGIN
Expand Down
2 changes: 1 addition & 1 deletion database/migration/20250512101543_table_images.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ CREATE TABLE IF NOT EXISTS "public"."images" (
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS "public"."images";
DROP SEQUENCE IF EXISTS images_id_seq;- +goose StatementEnd
DROP SEQUENCE IF EXISTS images_id_seq;
-- +goose StatementEnd
18 changes: 18 additions & 0 deletions database/seeder/20250522114625_seed_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- +goose Up
-- +goose StatementBegin
INSERT INTO "public"."users" (
"username", "email", "password", "role", "fullname",
"date_of_birth", "gender", "phone_number", "address",
"github", "linkedin", "personal_web", "created_at", "updated_at"
) VALUES
-- password : passowrd
('admin', 'admin@example.com', '$2a$10$zzJJ6MKKBgJT0CfT7rjnWeCAfSRIG6VhBdoqSIWi1VjwBfsp6XcT.', 'admin', 'Admin User',
'1990-01-01', 'Male', '123456789', '123 Admin St',
'github.com/admin', 'linkedin.com/in/admin', 'admin.com', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DELETE FROM "public"."users" WHERE "username" IN ('admin', 'johndoe', 'janedoe');
-- +goose StatementEnd
31 changes: 31 additions & 0 deletions database/seeder/20250522114645_seed_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- +goose Up
-- +goose StatementBegin
INSERT INTO "public"."events" (
"id", "title", "description", "author", "image", "date",
"reservation_start_date", "reservation_end_date", "type",
"location", "duration", "status", "price", "capacity",
"registration_link", "created_at", "updated_at"
) VALUES
(1, 'Web Development Workshop', 'Learn modern web development techniques',
'Tech Team', 'workshop_banner.jpg', '2025-06-15 09:00:00',
'2025-05-15 00:00:00', '2025-06-14 23:59:59', 'workshop',
'Tech Hub, Floor 3', '8 hours', 'upcoming', 150.00, 30,
'https://example.com/register/web-dev', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),

(2, 'Data Science Conference', 'Annual conference for data scientists',
'Data Science Society', 'conference_logo.png', '2025-07-20 08:30:00',
'2025-06-01 00:00:00', '2025-07-15 23:59:59', 'conference',
'Convention Center', '3 days', 'upcoming', 299.99, 200,
'https://example.com/register/data-conf', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),

(3, 'Mobile App Hackathon', '48-hour app building competition',
'Mobile Developers Group', 'hackathon_poster.jpg', '2025-08-10 10:00:00',
'2025-07-01 00:00:00', '2025-08-05 23:59:59', 'hackathon',
'Innovation Labs', '48 hours', 'upcoming', 50.00, 100,
'https://example.com/register/app-hackathon', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DELETE FROM "public"."events" WHERE "id" IN (1, 2, 3);
-- +goose StatementEnd
16 changes: 16 additions & 0 deletions database/seeder/20250522114656_seed_images.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
INSERT INTO "public"."images" (
"file_name", "file_path", "format", "content_type", "is_used", "file_size"
) VALUES
('workshop_banner.jpg', '/uploads/events/workshop_banner.jpg', 'jpg', 'image/jpeg', true, 256000),
('conference_logo.png', '/uploads/events/conference_logo.png', 'png', 'image/png', true, 128000),
('hackathon_poster.jpg', '/uploads/events/hackathon_poster.jpg', 'jpg', 'image/jpeg', true, 512000),
('payment_proof1.jpg', '/uploads/payments/payment_proof1.jpg', 'jpg', 'image/jpeg', true, 150000),
('payment_proof2.jpg', '/uploads/payments/payment_proof2.jpg', 'jpg', 'image/jpeg', true, 148000);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DELETE FROM "public"."images" WHERE "file_name" IN ('workshop_banner.jpg', 'conference_logo.png', 'hackathon_poster.jpg', 'payment_proof1.jpg', 'payment_proof2.jpg');
-- +goose StatementEnd
20 changes: 20 additions & 0 deletions database/seeder/20250522114706_seed_tags.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- +goose Up
-- +goose StatementBegin
INSERT INTO "public"."event_tags" (
"event_id", "tag"
) VALUES
(1, 'programming'),
(1, 'web-development'),
(1, 'javascript'),
(2, 'data-science'),
(2, 'machine-learning'),
(2, 'big-data'),
(3, 'mobile'),
(3, 'hackathon'),
(3, 'app-development');
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DELETE FROM "public"."event_tags" WHERE "event_id" IN (1, 2, 3);
-- +goose StatementEnd
20 changes: 20 additions & 0 deletions database/seeder/20250522114720_seed_registration_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- +goose Up
-- +goose StatementBegin
INSERT INTO "public"."registration_events" (
"order_no", "event_id", "user_id", "name", "email", "phone_number",
"image_proof_payment", "payment_date", "status", "up_to_you", "created_by_user_id"
) VALUES
('ORD-001', 1, '2', 'John Doe', 'john@example.com', '987654321',
'payment_proof1.jpg', '2025-06-01 14:30:00', 'confirmed', 'Looking forward to it!', 2),

('ORD-002', 2, '3', 'Jane Doe', 'jane@example.com', '555123456',
'payment_proof2.jpg', '2025-07-05 10:15:00', 'confirmed', 'Excited to learn!', 3),

('ORD-003', 3, '2', 'John Doe', 'john@example.com', '987654321',
NULL, NULL, 'pending', 'Will bring my team', 2);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DELETE FROM "public"."registration_events" WHERE "order_no" IN ('ORD-001', 'ORD-002', 'ORD-003');
-- +goose StatementEnd
15 changes: 15 additions & 0 deletions database/seeder/20250522114740_seed_event_pays.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +goose Up
-- +goose StatementBegin
INSERT INTO "public"."event_pays" (
"order_no", "status", "registration_event_id", "event_id",
"image_proof_payment", "net_amount"
) VALUES
('PAY-001', 'paid', 1, 1, 'payment_proof1.jpg', 150.00),
('PAY-002', 'paid', 2, 2, 'payment_proof2.jpg', 299.99),
('PAY-003', 'pending', 3, 3, NULL, 50.00);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DELETE FROM "public"."event_pays" WHERE "order_no" IN ('PAY-001', 'PAY-002', 'PAY-003');
-- +goose StatementEnd