Skip to content

Commit 7e225f6

Browse files
authored
Change Generate() function to a SchemaSource instead of a db for the from parameter (#189)
1 parent 34273e5 commit 7e225f6

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ if err != nil {
141141
}
142142
defer tempDbFactory.Close()
143143
// Generate the migration plan
144-
plan, err := diff.Generate(ctx, connPool, diff.DDLSchemaSource(ddl),
144+
plan, err := diff.Generate(ctx, diff.DBSchemaSource(connPool), diff.DDLSchemaSource(ddl),
145145
diff.WithTempDbFactory(tempDbFactory),
146146
diff.WithDataPackNewTables(),
147147
)

cmd/pg-schema-diff/plan_cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ func generatePlan(ctx context.Context, logger log.Logger, connConfig *pgx.ConnCo
410410
defer schemaSourceCloser.Close()
411411
}
412412

413-
plan, err := diff.Generate(ctx, connPool, schemaSource,
413+
connSource := diff.DBSchemaSource(connPool)
414+
415+
plan, err := diff.Generate(ctx, connSource, schemaSource,
414416
append(
415417
planConfig.opts,
416418
diff.WithTempDbFactory(tempDbFactory),

internal/migration_acceptance_tests/acceptance_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ func (suite *acceptanceTestSuite) runTest(tc acceptanceTestCase) {
9696
}
9797
if tc.planFactory == nil {
9898
tc.planFactory = func(ctx context.Context, connPool sqldb.Queryable, tempDbFactory tempdb.Factory, newSchemaDDL []string, opts ...diff.PlanOpt) (diff.Plan, error) {
99-
return diff.Generate(ctx, connPool, diff.DDLSchemaSource(newSchemaDDL),
99+
100+
connSource := diff.DBSchemaSource(connPool)
101+
102+
return diff.Generate(ctx, connSource, diff.DDLSchemaSource(newSchemaDDL),
100103
append(tc.planOpts,
101104
diff.WithTempDbFactory(tempDbFactory),
102105
)...)

internal/migration_acceptance_tests/database_schema_source_cases_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func databaseSchemaSourcePlan(ctx context.Context, connPool sqldb.Queryable, tem
3535
opts = append(opts, diff.WithGetSchemaOpts(o))
3636
}
3737

38-
return diff.Generate(ctx, connPool, diff.DBSchemaSource(newSchemaDb.ConnPool), opts...)
38+
return diff.Generate(ctx, diff.DBSchemaSource(connPool), diff.DBSchemaSource(newSchemaDb.ConnPool), opts...)
3939
}
4040

4141
func dirSchemaSourcePlanFactory(schemaDirs []string) planFactory {
@@ -53,7 +53,9 @@ func dirSchemaSourcePlanFactory(schemaDirs []string) planFactory {
5353
return diff.Plan{}, fmt.Errorf("creating schema source: %w", err)
5454
}
5555

56-
return diff.Generate(ctx, connPool, schemaSource, opts...)
56+
connSource := diff.DBSchemaSource(connPool)
57+
58+
return diff.Generate(ctx, connSource, schemaSource, opts...)
5759
}
5860
}
5961

@@ -65,7 +67,7 @@ var databaseSchemaSourceTestCases = []acceptanceTestCase{
6567
oldSchemaDDL: []string{
6668
`
6769
CREATE TABLE fizz();
68-
70+
6971
CREATE TABLE foobar(
7072
id INT,
7173
bar SERIAL NOT NULL,
@@ -134,7 +136,7 @@ var databaseSchemaSourceTestCases = []acceptanceTestCase{
134136
CREATE INDEX bar_normal_idx ON bar(bar);
135137
CREATE INDEX bar_another_normal_id ON bar(bar, fizz);
136138
CREATE UNIQUE INDEX bar_unique_idx on bar(fizz, buzz);
137-
139+
138140
`,
139141
},
140142
expectedHazardTypes: []diff.MigrationHazardType{

pkg/diff/plan_generator.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,22 @@ func WithGetSchemaOpts(getSchemaOpts ...externalschema.GetSchemaOpt) PlanOpt {
106106
// newDDL: DDL encoding the new schema
107107
// opts: Additional options to configure the plan generation
108108
func GeneratePlan(ctx context.Context, queryable sqldb.Queryable, tempdbFactory tempdb.Factory, newDDL []string, opts ...PlanOpt) (Plan, error) {
109-
return Generate(ctx, queryable, DDLSchemaSource(newDDL), append(opts, WithTempDbFactory(tempdbFactory), WithIncludeSchemas("public"))...)
109+
110+
schemaSource := DBSchemaSource(queryable)
111+
112+
return Generate(ctx, schemaSource, DDLSchemaSource(newDDL), append(opts, WithTempDbFactory(tempdbFactory), WithIncludeSchemas("public"))...)
110113
}
111114

112115
// Generate generates a migration plan to migrate the database to the target schema
113116
//
114117
// Parameters:
115-
// fromDB: The target database to generate the diff for. It is recommended to pass in *sql.DB of the db you
116-
// wish to migrate. If using a connection pool, it is RECOMMENDED to set a maximum number of connections.
118+
// fromSchema: The target schema to generate the diff for.
117119
// targetSchema: The (source of the) schema you want to migrate the database to. Use DDLSchemaSource if the new
118120
// schema is encoded in DDL.
119121
// opts: Additional options to configure the plan generation
120122
func Generate(
121123
ctx context.Context,
122-
fromDB sqldb.Queryable,
124+
fromSchema SchemaSource,
123125
targetSchema SchemaSource,
124126
opts ...PlanOpt,
125127
) (Plan, error) {
@@ -132,7 +134,11 @@ func Generate(
132134
opt(planOptions)
133135
}
134136

135-
currentSchema, err := schema.GetSchema(ctx, fromDB, planOptions.getSchemaOpts...)
137+
currentSchema, err := fromSchema.GetSchema(ctx, schemaSourcePlanDeps{
138+
tempDBFactory: planOptions.tempDbFactory,
139+
logger: planOptions.logger,
140+
getSchemaOpts: planOptions.getSchemaOpts,
141+
})
136142
if err != nil {
137143
return Plan{}, fmt.Errorf("getting current schema: %w", err)
138144
}

pkg/diff/plan_generator_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (suite *planGeneratorTestSuite) TestGenerate() {
104104
tempDbFactory := suite.mustBuildTempDbFactory(context.Background())
105105
defer tempDbFactory.Close()
106106

107-
plan, err := Generate(context.Background(), connPool, DDLSchemaSource([]string{newSchemaDDL}), WithTempDbFactory(tempDbFactory))
107+
plan, err := Generate(context.Background(), DBSchemaSource(connPool), DDLSchemaSource([]string{newSchemaDDL}), WithTempDbFactory(tempDbFactory))
108108
suite.NoError(err)
109109

110110
suite.mustApplyMigrationPlan(connPool, plan)
@@ -140,7 +140,7 @@ func (suite *planGeneratorTestSuite) TestGeneratePlan_SchemaSourceErr() {
140140
connPool := suite.mustGetTestDBPool()
141141
defer connPool.Close()
142142

143-
_, err := Generate(context.Background(), connPool, fakeSchemaSource,
143+
_, err := Generate(context.Background(), DBSchemaSource(connPool), fakeSchemaSource,
144144
WithTempDbFactory(tempDbFactory),
145145
WithGetSchemaOpts(getSchemaOpts...),
146146
WithLogger(logger),
@@ -163,7 +163,7 @@ func (suite *planGeneratorTestSuite) TestGenerate_CannotPackNewTablesWithoutIgno
163163
connPool := suite.mustGetTestDBPool()
164164
defer connPool.Close()
165165

166-
_, err := Generate(context.Background(), connPool, DDLSchemaSource([]string{``}),
166+
_, err := Generate(context.Background(), DBSchemaSource(connPool), DDLSchemaSource([]string{``}),
167167
WithTempDbFactory(tempDbFactory),
168168
WithDataPackNewTables(),
169169
WithRespectColumnOrder(),
@@ -174,7 +174,7 @@ func (suite *planGeneratorTestSuite) TestGenerate_CannotPackNewTablesWithoutIgno
174174
func (suite *planGeneratorTestSuite) TestGenerate_CannotBuildMigrationFromDDLWithoutTempDbFactory() {
175175
pool := suite.mustGetTestDBPool()
176176
defer pool.Close()
177-
_, err := Generate(context.Background(), pool, DDLSchemaSource([]string{``}),
177+
_, err := Generate(context.Background(), DBSchemaSource(pool), DDLSchemaSource([]string{``}),
178178
WithIncludeSchemas("public"),
179179
WithDoNotValidatePlan(),
180180
)
@@ -184,7 +184,7 @@ func (suite *planGeneratorTestSuite) TestGenerate_CannotBuildMigrationFromDDLWit
184184
func (suite *planGeneratorTestSuite) TestGenerate_CannotValidateWithoutTempDbFactory() {
185185
pool := suite.mustGetTestDBPool()
186186
defer pool.Close()
187-
_, err := Generate(context.Background(), pool, DDLSchemaSource([]string{``}),
187+
_, err := Generate(context.Background(), DBSchemaSource(pool), DDLSchemaSource([]string{``}),
188188
WithIncludeSchemas("public"),
189189
WithDoNotValidatePlan(),
190190
)

0 commit comments

Comments
 (0)