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 f94af79
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 41 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"]
5 changes: 1 addition & 4 deletions internal/actionsdb/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
5 changes: 1 addition & 4 deletions internal/database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions internal/dnsdb/dnsdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions internal/httpdb/httpdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions internal/modules/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions internal/modules/api/apiclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f94af79

Please sign in to comment.