Skip to content

Commit

Permalink
Embed migrations into binary
Browse files Browse the repository at this point in the history
  • Loading branch information
nt0xa committed Jul 17, 2024
1 parent 925fa07 commit 823ea02
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ FROM alpine:latest
RUN apk add --no-cache ca-certificates
WORKDIR /opt/app
COPY --from=builder /opt/app/server .
COPY ./internal/database/migrations ./migrations
EXPOSE 53/udp 21 25 80 443
CMD ["./server"]
1 change: 0 additions & 1 deletion Dockerfile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ FROM alpine:latest
RUN apk add --no-cache ca-certificates
WORKDIR /opt/app
COPY server /opt/app/
COPY ./internal/database/migrations ./migrations
EXPOSE 53/udp 25 80 443
CMD ["./server"]
4 changes: 1 addition & 3 deletions internal/database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
)

type Config struct {
DSN string `json:"dsn"`
Migrations string `json:"migrations" default:"/opt/app/migrations"`
DSN string `json:"dsn"`
}

func (c Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.DSN, validation.Required),
validation.Field(&c.Migrations, validation.Required),
)
}
29 changes: 17 additions & 12 deletions internal/database/db.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package database

import (
"embed"
"fmt"

_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"

"github.com/nt0xa/sonar/internal/utils/logger"
)

//go:embed migrations/*.sql
var migrationsFS embed.FS

type DB struct {
*sqlx.DB
log logger.StdLogger
migrations string
obserers []Observer
log logger.StdLogger
obserers []Observer
}

func New(cfg *Config, log logger.StdLogger) (*DB, error) {
Expand All @@ -29,24 +32,26 @@ func New(cfg *Config, log logger.StdLogger) (*DB, error) {
}

return &DB{
DB: db,
log: log,
migrations: cfg.Migrations,
obserers: make([]Observer, 0),
DB: db,
log: log,
obserers: make([]Observer, 0),
}, nil
}

func (db *DB) Migrate() error {
fs, err := iofs.New(migrationsFS, "migrations")
if err != nil {
return fmt.Errorf("migrate: fail to create source: %w", err)
}

driver, err := postgres.WithInstance(db.DB.DB, &postgres.Config{})
if err != nil {
return fmt.Errorf("migrate: fail to create driver: %w", err)
}

migrations, err := migrate.NewWithDatabaseInstance(
fmt.Sprintf("file://%s", db.migrations),
"postgres", driver)
migrations, err := migrate.NewWithInstance("iofs", fs, "postgres", driver)
if err != nil {
return fmt.Errorf("migrate: fail to create source: %w", err)
return fmt.Errorf("migrate: fail to init: %w", err)
}

if err := migrations.Up(); err != nil && err != migrate.ErrNoChange {
Expand Down

0 comments on commit 823ea02

Please sign in to comment.