From 823ea02cf7a3ef3cb17cd40019801a205c54c3aa Mon Sep 17 00:00:00 2001 From: Anton Prokhorov Date: Wed, 17 Jul 2024 22:54:58 +0100 Subject: [PATCH] Embed migrations into binary --- Dockerfile | 1 - Dockerfile.ci | 1 - internal/database/config.go | 4 +--- internal/database/db.go | 29 +++++++++++++++++------------ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index edebe76..2b0646f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/Dockerfile.ci b/Dockerfile.ci index 7ae5ae0..b2a2221 100644 --- a/Dockerfile.ci +++ b/Dockerfile.ci @@ -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"] diff --git a/internal/database/config.go b/internal/database/config.go index e6a3617..2318762 100644 --- a/internal/database/config.go +++ b/internal/database/config.go @@ -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), ) } diff --git a/internal/database/db.go b/internal/database/db.go index 820ee0f..53d25db 100644 --- a/internal/database/db.go +++ b/internal/database/db.go @@ -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) { @@ -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 {