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/actionsdb/actions_test.go b/internal/actionsdb/actions_test.go index bad783a..a6bb0d7 100644 --- a/internal/actionsdb/actions_test.go +++ b/internal/actionsdb/actions_test.go @@ -33,10 +33,7 @@ func TestMain(m *testing.M) { log := logrus.New() - db, err = database.New(&database.Config{ - DSN: dsn, - Migrations: "../database/migrations", - }, log) + db, err = database.New(&database.Config{DSN: dsn}, log) if err != nil { fmt.Fprintf(os.Stderr, "fail to init database: %v\n", err) os.Exit(1) 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 { diff --git a/internal/database/db_test.go b/internal/database/db_test.go index 9faa365..7fd7191 100644 --- a/internal/database/db_test.go +++ b/internal/database/db_test.go @@ -29,10 +29,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - db, err = database.New(&database.Config{ - DSN: dsn, - Migrations: "migrations", - }, log) + db, err = database.New(&database.Config{DSN: dsn}, log) if err != nil { fmt.Fprintf(os.Stderr, "fail to init database: %v\n", err) os.Exit(1) diff --git a/internal/dnsdb/dnsdb_test.go b/internal/dnsdb/dnsdb_test.go index 814a03c..4786c1d 100644 --- a/internal/dnsdb/dnsdb_test.go +++ b/internal/dnsdb/dnsdb_test.go @@ -32,10 +32,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - db, err = database.New(&database.Config{ - DSN: dsn, - Migrations: "../database/migrations", - }, logrus.New()) + db, err = database.New(&database.Config{DSN: dsn}, logrus.New()) if err != nil { fmt.Fprintf(os.Stderr, "fail to init database: %v\n", err) os.Exit(1) diff --git a/internal/httpdb/httpdb_test.go b/internal/httpdb/httpdb_test.go index b8e5f0f..0766cec 100644 --- a/internal/httpdb/httpdb_test.go +++ b/internal/httpdb/httpdb_test.go @@ -39,10 +39,7 @@ func TestMain(m *testing.M) { os.Exit(1) } - db, err = database.New(&database.Config{ - DSN: dsn, - Migrations: "../database/migrations", - }, logrus.New()) + db, err = database.New(&database.Config{DSN: dsn}, logrus.New()) if err != nil { fmt.Fprintf(os.Stderr, "fail to init database: %v\n", err) os.Exit(1) diff --git a/internal/modules/api/api_test.go b/internal/modules/api/api_test.go index 3756653..bc9610f 100644 --- a/internal/modules/api/api_test.go +++ b/internal/modules/api/api_test.go @@ -68,10 +68,7 @@ func TestMain(m *testing.M) { log := logrus.New() - db, err = database.New(&database.Config{ - DSN: dsn, - Migrations: "../../database/migrations", - }, log) + db, err = database.New(&database.Config{DSN: dsn}, log) if err != nil { fmt.Fprintf(os.Stderr, "fail to init database: %v\n", err) os.Exit(1) diff --git a/internal/modules/api/apiclient/client_test.go b/internal/modules/api/apiclient/client_test.go index cff4720..7d52e04 100644 --- a/internal/modules/api/apiclient/client_test.go +++ b/internal/modules/api/apiclient/client_test.go @@ -67,10 +67,7 @@ func TestMain(m *testing.M) { log := logrus.New() - db, err = database.New(&database.Config{ - DSN: dsn, - Migrations: "../../../database/migrations", - }, log) + db, err = database.New(&database.Config{DSN: dsn}, log) if err != nil { fmt.Fprintf(os.Stderr, "fail to init database: %v\n", err) os.Exit(1)