From f16451eab831cc90b0a681184334c76947b01b5e Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:06:53 +0200 Subject: [PATCH] dialect/sql/schema: allow planning with indentation (#3363) --- dialect/sql/schema/atlas.go | 16 +++++++++++----- dialect/sql/schema/migrate.go | 8 ++++++++ dialect/sql/schema/migrate_test.go | 5 ++++- examples/go.mod | 2 +- examples/go.sum | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- 7 files changed, 29 insertions(+), 12 deletions(-) diff --git a/dialect/sql/schema/atlas.go b/dialect/sql/schema/atlas.go index 11725a335..dd89f3b39 100644 --- a/dialect/sql/schema/atlas.go +++ b/dialect/sql/schema/atlas.go @@ -33,11 +33,12 @@ type Atlas struct { withFixture bool // deprecated: with fks rename fixture sum bool // deprecated: sum file generation will be required - errNoPlan bool // no plan error enabled - universalID bool // global unique ids - dropColumns bool // drop deleted columns - dropIndexes bool // drop deleted indexes - withForeignKeys bool // with foreign keys + indent string // plan indentation + errNoPlan bool // no plan error enabled + universalID bool // global unique ids + dropColumns bool // drop deleted columns + dropIndexes bool // drop deleted indexes + withForeignKeys bool // with foreign keys mode Mode hooks []Hook // hooks to apply before creation diffHooks []DiffHook // diff hooks to run when diffing current and desired @@ -804,6 +805,11 @@ func (a *Atlas) diff(ctx context.Context, name string, current, desired *schema. filtered = append(filtered, c) } } + if a.indent != "" { + opts = append(opts, func(opts *migrate.PlanOptions) { + opts.Indent = a.indent + }) + } plan, err := a.atDriver.PlanChanges(ctx, name, filtered, opts...) if err != nil { return nil, err diff --git a/dialect/sql/schema/migrate.go b/dialect/sql/schema/migrate.go index 035d391e9..014eb98b3 100644 --- a/dialect/sql/schema/migrate.go +++ b/dialect/sql/schema/migrate.go @@ -41,6 +41,14 @@ func WithGlobalUniqueID(b bool) MigrateOption { } } +// WithIndent sets Atlas to generate SQL statements with indentation. +// An empty string indicates no indentation. +func WithIndent(indent string) MigrateOption { + return func(a *Atlas) { + a.indent = indent + } +} + // WithErrNoPlan sets Atlas to returns a migrate.ErrNoPlan in case // the migration plan is empty. Defaults to false. func WithErrNoPlan(b bool) MigrateOption { diff --git a/dialect/sql/schema/migrate_test.go b/dialect/sql/schema/migrate_test.go index 9d7bfd000..e1b64c0d6 100644 --- a/dialect/sql/schema/migrate_test.go +++ b/dialect/sql/schema/migrate_test.go @@ -292,13 +292,16 @@ func TestMigrate_Diff(t *testing.T) { }, "\n") requireFileEqual(t, filepath.Join(p, "changes.sql"), changesSQL) + // Enable indentations. + m, err = NewMigrate(db, WithFormatter(f), WithDir(d), WithGlobalUniqueID(true), WithIndent(" ")) + require.NoError(t, err) // Adding another node will result in a new entry to the TypeTable (without actually creating it). _, err = db.ExecContext(ctx, changesSQL, nil, nil) require.NoError(t, err) require.NoError(t, m.NamedDiff(ctx, "changes_2", petsTable)) requireFileEqual(t, filepath.Join(p, "changes_2.sql"), strings.Join([]string{ - "CREATE TABLE `pets` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT);", + "CREATE TABLE `pets` (\n `id` integer NOT NULL PRIMARY KEY AUTOINCREMENT\n);", fmt.Sprintf("INSERT INTO sqlite_sequence (name, seq) VALUES (\"pets\", %d);", 2<<32), "INSERT INTO `ent_types` (`type`) VALUES ('pets');", "", }, "\n")) diff --git a/examples/go.mod b/examples/go.mod index 962eed9a3..02b9bc92d 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -5,7 +5,7 @@ go 1.19 replace entgo.io/ent => ../ require ( - ariga.io/atlas v0.9.1 + ariga.io/atlas v0.9.2-0.20230303073438-03a4779a6338 entgo.io/ent v0.0.0-00010101000000-000000000000 github.com/google/uuid v1.3.0 github.com/mattn/go-sqlite3 v1.14.16 diff --git a/examples/go.sum b/examples/go.sum index 26f99f17e..90e2a70f8 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,5 +1,5 @@ -ariga.io/atlas v0.9.1 h1:EpoPMnwsQG0vn9c0sYExpwSYtr7bvuSUXzQclU2pMjc= -ariga.io/atlas v0.9.1/go.mod h1:T230JFcENj4ZZzMkZrXFDSkv+2kXkUgpJ5FQQ5hMcKU= +ariga.io/atlas v0.9.2-0.20230303073438-03a4779a6338 h1:8kmSV3mbQKn0niZ/EdE11uhFvFKiW1VlaqVBIYOyahM= +ariga.io/atlas v0.9.2-0.20230303073438-03a4779a6338/go.mod h1:T230JFcENj4ZZzMkZrXFDSkv+2kXkUgpJ5FQQ5hMcKU= bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8= bazil.org/fuse v0.0.0-20200407214033-5883e5a4b512/go.mod h1:FbcW6z/2VytnFDhZfumh8Ss8zxHE6qpMP5sHTRe0EaM= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= diff --git a/go.mod b/go.mod index 624510d52..319ca5798 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module entgo.io/ent go 1.19 require ( - ariga.io/atlas v0.9.1 + ariga.io/atlas v0.9.2-0.20230303073438-03a4779a6338 github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/go-openapi/inflect v0.19.0 github.com/go-sql-driver/mysql v1.7.0 diff --git a/go.sum b/go.sum index 6da855ca1..285302067 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -ariga.io/atlas v0.9.1 h1:EpoPMnwsQG0vn9c0sYExpwSYtr7bvuSUXzQclU2pMjc= -ariga.io/atlas v0.9.1/go.mod h1:T230JFcENj4ZZzMkZrXFDSkv+2kXkUgpJ5FQQ5hMcKU= +ariga.io/atlas v0.9.2-0.20230303073438-03a4779a6338 h1:8kmSV3mbQKn0niZ/EdE11uhFvFKiW1VlaqVBIYOyahM= +ariga.io/atlas v0.9.2-0.20230303073438-03a4779a6338/go.mod h1:T230JFcENj4ZZzMkZrXFDSkv+2kXkUgpJ5FQQ5hMcKU= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=