2022-09-28 04:26:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-10 05:57:57 +00:00
|
|
|
"fmt"
|
2022-09-28 04:26:44 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/migrate"
|
2022-09-28 04:26:44 +00:00
|
|
|
|
|
|
|
atlas "ariga.io/atlas/sql/migrate"
|
|
|
|
_ "ariga.io/atlas/sql/sqlite"
|
|
|
|
"entgo.io/ent/dialect"
|
|
|
|
"entgo.io/ent/dialect/sql/schema"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
// Create a local migration directory able to understand Atlas migration file format for replay.
|
2022-10-30 04:05:38 +00:00
|
|
|
dir, err := atlas.NewLocalDir("internal/data/migrations/migrations")
|
2022-09-28 04:26:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed creating atlas migration directory: %v", err)
|
|
|
|
}
|
|
|
|
// Migrate diff options.
|
|
|
|
opts := []schema.MigrateOption{
|
|
|
|
schema.WithDir(dir), // provide migration directory
|
|
|
|
schema.WithMigrationMode(schema.ModeReplay), // provide migration mode
|
|
|
|
schema.WithDialect(dialect.SQLite), // Ent dialect to use
|
|
|
|
schema.WithFormatter(atlas.DefaultFormatter),
|
|
|
|
schema.WithDropIndex(true),
|
|
|
|
schema.WithDropColumn(true),
|
|
|
|
}
|
|
|
|
if len(os.Args) != 2 {
|
|
|
|
log.Fatalln("migration name is required. Use: 'go run -mod=mod ent/migrate/main.go <name>'")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate migrations using Atlas support for MySQL (note the Ent dialect option passed above).
|
|
|
|
err = migrate.NamedDiff(ctx, "sqlite://.data/homebox.migration.db?_fk=1", os.Args[1], opts...)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed generating migration file: %v", err)
|
|
|
|
}
|
2022-12-10 05:57:57 +00:00
|
|
|
|
|
|
|
fmt.Println("Migration file generated successfully.")
|
2022-09-28 04:26:44 +00:00
|
|
|
}
|