Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed migrations into binary #174

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}

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)
}

Check warning on line 45 in internal/database/db.go

View check run for this annotation

Codecov / codecov/patch

internal/database/db.go#L44-L45

Added lines #L44 - L45 were not covered by tests

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)

Check warning on line 54 in internal/database/db.go

View check run for this annotation

Codecov / codecov/patch

internal/database/db.go#L54

Added line #L54 was not covered by tests
}

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
Loading