Skip to content

Commit 9ce1541

Browse files
committed
Add option to generate a reversed diff
1 parent 34273e5 commit 9ce1541

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pkg/diff/plan_generator.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type (
3333
logger log.Logger
3434
validatePlan bool
3535
getSchemaOpts []schema.GetSchemaOpt
36+
reverse bool
3637
}
3738

3839
PlanOpt func(opts *planOptions)
@@ -93,6 +94,13 @@ func WithGetSchemaOpts(getSchemaOpts ...externalschema.GetSchemaOpt) PlanOpt {
9394
}
9495
}
9596

97+
// WithReverse return a diff to generate statements to migrate the schema to the current database
98+
func WithReverse() PlanOpt {
99+
return func(opts *planOptions) {
100+
opts.reverse = true
101+
}
102+
}
103+
96104
// deprecated: GeneratePlan generates a migration plan to migrate the database to the target schema. This function only
97105
// diffs the public schemas.
98106
//
@@ -127,6 +135,7 @@ func Generate(
127135
validatePlan: true,
128136
ignoreChangesToColOrder: true,
129137
logger: log.SimpleLogger(),
138+
reverse: false,
130139
}
131140
for _, opt := range opts {
132141
opt(planOptions)
@@ -145,6 +154,10 @@ func Generate(
145154
return Plan{}, fmt.Errorf("getting new schema: %w", err)
146155
}
147156

157+
if planOptions.reverse {
158+
currentSchema, newSchema = newSchema, currentSchema
159+
}
160+
148161
statements, err := generateMigrationStatements(currentSchema, newSchema, planOptions)
149162
if err != nil {
150163
return Plan{}, fmt.Errorf("generating plan statements: %w", err)

0 commit comments

Comments
 (0)