Compare commits

..
2 Commits
3 changed files with 11 additions and 7 deletions
+2 -1
View File
@@ -11,6 +11,7 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/robindittmar/dttmr-api/internal/config" "github.com/robindittmar/dttmr-api/internal/config"
"github.com/robindittmar/dttmr-api/internal/database" "github.com/robindittmar/dttmr-api/internal/database"
"github.com/robindittmar/dttmr-api/internal/database/migrations"
"github.com/robindittmar/dttmr-api/internal/domain" "github.com/robindittmar/dttmr-api/internal/domain"
"github.com/robindittmar/dttmr-api/internal/repository" "github.com/robindittmar/dttmr-api/internal/repository"
) )
@@ -51,7 +52,7 @@ func main() {
} }
}() }()
if err := database.RunMigrations(db); err != nil { if err := database.RunMigrations(db, migrations.MigrationFS); err != nil {
slog.Error("failed to run migrations", slog.Any("error", err)) slog.Error("failed to run migrations", slog.Any("error", err))
os.Exit(1) os.Exit(1)
} }
+3 -6
View File
@@ -2,9 +2,9 @@ package database
import ( import (
"database/sql" "database/sql"
"embed"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"log/slog" "log/slog"
"github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4"
@@ -12,11 +12,8 @@ import (
"github.com/golang-migrate/migrate/v4/source/iofs" "github.com/golang-migrate/migrate/v4/source/iofs"
) )
//go:embed migrations/*.sql func RunMigrations(db *sql.DB, migrationFS fs.FS) error {
var migrationFS embed.FS sourceDriver, err := iofs.New(migrationFS, ".")
func RunMigrations(db *sql.DB) error {
sourceDriver, err := iofs.New(migrationFS, "migrations")
if err != nil { if err != nil {
return fmt.Errorf("failed to load embedded migrations: %w", err) return fmt.Errorf("failed to load embedded migrations: %w", err)
} }
+6
View File
@@ -0,0 +1,6 @@
package migrations
import "embed"
//go:embed *.sql
var MigrationFS embed.FS