Skip to content

Commit 8ab45e0

Browse files
feat: enhance database migration functionality and Docker configuration
- Update docker-compose.yml to specify the migration SQL file path in the command - Modify Dockerfile to copy the migrations.sql file into the container - Refactor database.go to accept a migration file path as an argument for the RunMigrations function
1 parent bbf0cf1 commit 8ab45e0

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

server/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ COPY . .
66

77
RUN go build -ldflags="-s -w" -o rabbit.go main.go
88

9-
FROM gcr.io/distroless/static-debian12:latest as runner
9+
FROM gcr.io/distroless/static-debian12:latest AS runner
1010

1111
COPY --from=builder /app/rabbit.go /usr/local/bin/rabbit.go
12+
COPY --from=builder /app/internal/database/migrations.sql /usr/local/bin/internal/database/migrations.sql
1213

1314
# 9999 is the tunnel server port
1415
# 3422 is the API port (never expose this port to the internet)

server/cmd/database.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ var databaseCmd = &cobra.Command{
2323
}
2424

2525
var migrateCmd = &cobra.Command{
26-
Use: "migrate",
26+
Use: "migrate <path>",
2727
Short: "Run database migrations",
2828
Long: `Create database tables and run migrations.`,
29+
Args: cobra.ExactArgs(1),
2930
RunE: func(cmd *cobra.Command, args []string) error {
3031
config := database.GetConfigFromEnv()
3132
db, err := database.NewDatabase(config)
@@ -42,7 +43,7 @@ var migrateCmd = &cobra.Command{
4243
// return fmt.Errorf("failed to clean up database: %w", err)
4344
// }
4445

45-
if err := db.RunMigrations(); err != nil {
46+
if err := db.RunMigrations(args[0]); err != nil {
4647
return fmt.Errorf("migration failed: %w", err)
4748
}
4849

server/docker-compose.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
- "9999:9999"
1010
- "3422:3422"
1111
env_file:
12-
- .env
12+
- ./.env
1313
depends_on:
1414
rabbit-migrate:
1515
condition: service_completed_successfully
@@ -21,14 +21,14 @@ services:
2121
build:
2222
context: .
2323
dockerfile: Dockerfile
24-
command: /usr/local/bin/rabbit.go database migrate
24+
command: /usr/local/bin/rabbit.go database migrate /usr/local/bin/internal/database/migrations.sql
2525
env_file:
26-
- .env
26+
- ./.env
2727
networks:
2828
# optional, if you want to use dokploy network
2929
- dokploy-network
3030

3131
networks:
3232
# optional, if you want to use dokploy network
3333
dokploy-network:
34-
external: true
34+
driver: bridge

server/internal/database/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ func (d *Database) CleanUp() error {
105105
}
106106

107107
// RunMigrations runs the database migrations
108-
func (d *Database) RunMigrations() error {
109-
migrationFile := "internal/database/migrations.sql"
108+
func (d *Database) RunMigrations(path string) error {
109+
migrationFile := path
110110

111111
// Read migration file
112112
migrationSQL, err := os.ReadFile(migrationFile)

0 commit comments

Comments
 (0)