diff --git a/backend/app/api/main.go b/backend/app/api/main.go index bd833b3..f25d20f 100644 --- a/backend/app/api/main.go +++ b/backend/app/api/main.go @@ -5,6 +5,7 @@ import ( "os" "time" + "entgo.io/ent/dialect/sql/schema" "github.com/hay-kot/homebox/backend/app/api/docs" "github.com/hay-kot/homebox/backend/ent" "github.com/hay-kot/homebox/backend/internal/config" @@ -72,7 +73,7 @@ func run(cfg *config.Config) error { defer func(c *ent.Client) { _ = c.Close() }(c) - if err := c.Schema.Create(context.Background()); err != nil { + if err := c.Schema.Create(context.Background(), schema.WithAtlas(true)); err != nil { log.Fatal(). Err(err). Str("driver", "sqlite"). diff --git a/backend/ent/generate.go b/backend/ent/generate.go index 8d3fdfd..eb03ded 100644 --- a/backend/ent/generate.go +++ b/backend/ent/generate.go @@ -1,3 +1,3 @@ package ent -//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate ./schema +//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/versioned-migration ./schema diff --git a/backend/ent/migrate/migrate.go b/backend/ent/migrate/migrate.go index 1956a6b..d8d3bcb 100644 --- a/backend/ent/migrate/migrate.go +++ b/backend/ent/migrate/migrate.go @@ -54,6 +54,38 @@ func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...sche return migrate.Create(ctx, tables...) } +// Diff compares the state read from a database connection or migration directory with +// the state defined by the Ent schema. Changes will be written to new migration files. +func Diff(ctx context.Context, url string, opts ...schema.MigrateOption) error { + return NamedDiff(ctx, url, "changes", opts...) +} + +// NamedDiff compares the state read from a database connection or migration directory with +// the state defined by the Ent schema. Changes will be written to new named migration files. +func NamedDiff(ctx context.Context, url, name string, opts ...schema.MigrateOption) error { + return schema.Diff(ctx, url, name, Tables, opts...) +} + +// Diff creates a migration file containing the statements to resolve the diff +// between the Ent schema and the connected database. +func (s *Schema) Diff(ctx context.Context, opts ...schema.MigrateOption) error { + migrate, err := schema.NewMigrate(s.drv, opts...) + if err != nil { + return fmt.Errorf("ent/migrate: %w", err) + } + return migrate.Diff(ctx, Tables...) +} + +// NamedDiff creates a named migration file containing the statements to resolve the diff +// between the Ent schema and the connected database. +func (s *Schema) NamedDiff(ctx context.Context, name string, opts ...schema.MigrateOption) error { + migrate, err := schema.NewMigrate(s.drv, opts...) + if err != nil { + return fmt.Errorf("ent/migrate: %w", err) + } + return migrate.NamedDiff(ctx, name, Tables...) +} + // WriteTo writes the schema changes to w instead of running them against the database. // // if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {