package main import ( "context" "errors" "log" "os" "os/signal" "path/filepath" "time" "code.icb4dc0.de/prskr/searcherside/internal/db/dbtest" "code.icb4dc0.de/prskr/searcherside/internal/ent/migrate" atlas "ariga.io/atlas/sql/migrate" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql/schema" _ "code.icb4dc0.de/prskr/searcherside/internal/db" ) var ( dir = filepath.Join("scripts", "migrations") targets = []string{dialect.SQLite, dialect.Postgres} ) func main() { if len(os.Args) != 2 { log.Fatalln("migration name is required. Use: 'go run -mod=mod code.icb4dc0.de/prskr/searcherside/internal/migrations '") } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) for _, d := range targets { if err := generateMigrations(ctx, d, os.Args[1]); err != nil { cancel() log.Fatalf("failed to generate migrations: %v", err) } } cancel() } func generateMigrations(ctx context.Context, dbDialect, name string) (err error) { migrationsDir := filepath.Join(dir, dbDialect) if err := os.MkdirAll(migrationsDir, 0o755); err != nil { return err } dir, err := atlas.NewLocalDir(migrationsDir) if err != nil { return err } opts := []schema.MigrateOption{ schema.WithDir(dir), schema.WithMigrationMode(schema.ModeReplay), schema.WithDialect(dbDialect), schema.WithFormatter(atlas.DefaultFormatter), schema.WithDropColumn(true), schema.WithDropIndex(true), } connectionString, closer, err := dbtest.TestDatabaseForDialect(ctx, dbDialect) if err != nil { return err } defer func() { shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) err = errors.Join(err, closer.Close(shutdownCtx)) cancel() }() return migrate.NamedDiff(ctx, connectionString, name, opts...) }