enable atlas migrations

This commit is contained in:
Hayden 2022-09-24 11:39:49 -08:00
parent 343290a55a
commit 24084726fb
3 changed files with 35 additions and 2 deletions

View file

@ -5,6 +5,7 @@ import (
"os" "os"
"time" "time"
"entgo.io/ent/dialect/sql/schema"
"github.com/hay-kot/homebox/backend/app/api/docs" "github.com/hay-kot/homebox/backend/app/api/docs"
"github.com/hay-kot/homebox/backend/ent" "github.com/hay-kot/homebox/backend/ent"
"github.com/hay-kot/homebox/backend/internal/config" "github.com/hay-kot/homebox/backend/internal/config"
@ -72,7 +73,7 @@ func run(cfg *config.Config) error {
defer func(c *ent.Client) { defer func(c *ent.Client) {
_ = c.Close() _ = c.Close()
}(c) }(c)
if err := c.Schema.Create(context.Background()); err != nil { if err := c.Schema.Create(context.Background(), schema.WithAtlas(true)); err != nil {
log.Fatal(). log.Fatal().
Err(err). Err(err).
Str("driver", "sqlite"). Str("driver", "sqlite").

View file

@ -1,3 +1,3 @@
package ent 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

View file

@ -54,6 +54,38 @@ func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...sche
return migrate.Create(ctx, tables...) 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. // 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 { // if err := client.Schema.WriteTo(context.Background(), os.Stdout); err != nil {