dialect/sql/schema: allow planning with indentation (#3363)

This commit is contained in:
Ariel Mashraki
2023-03-04 10:06:53 +02:00
committed by GitHub
parent 9517200cb6
commit f16451eab8
7 changed files with 29 additions and 12 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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"))