diff --git a/internal/actions/local/migrate/command/ensure_db.go b/internal/actions/local/migrate/command/ensure_db.go new file mode 100644 index 0000000..3aacd1a --- /dev/null +++ b/internal/actions/local/migrate/command/ensure_db.go @@ -0,0 +1,11 @@ +package command + +import ( + "github.com/Drafteame/draft/internal/pkg/exec" +) + +func runInitSQL() error { + cmd := `docker exec -i api-local-postgres psql -U root -d postgres -f /docker-entrypoint-initdb.d/init.sql` + _, err := exec.Command(cmd) + return err +} diff --git a/internal/actions/local/migrate/command/exec.go b/internal/actions/local/migrate/command/exec.go index 5885fc1..5c6bd40 100644 --- a/internal/actions/local/migrate/command/exec.go +++ b/internal/actions/local/migrate/command/exec.go @@ -2,6 +2,8 @@ package command import ( "fmt" + + "github.com/Drafteame/draft/internal/pkg/log" ) func (a *Action) Exec() error { @@ -10,6 +12,12 @@ func (a *Action) Exec() error { return fmt.Errorf("failed to load local migrations config: %w", err) } + if a.Input.Command == "up" { + if err := runInitSQL(); err != nil { + log.Warnf("failed to run init sql: %v", err) + } + } + if a.Input.All { if errAll := a.migrateAll(config); errAll != nil { return fmt.Errorf("failed to execute '%s' migration on all databases: %w", a.Input.Command, errAll)