Skip to content

Commit db2ea4d

Browse files
author
Chris Hundt
committed
[CMCSMACD-6648] Allow for a setup function in SchemaTest
This allows callers to set up preerquisite data or tables that are needed for the migrations without running afoul of the empty- database check at the beginning of SchemaTest.
1 parent a326a3e commit db2ea4d

3 files changed

Lines changed: 82 additions & 18 deletions

File tree

cmd/schema-test/main.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"os"
78

89
"github.com/corbaltcode/go-libraries/migrations"
10+
"github.com/corbaltcode/go-libraries/pgutils"
911
_ "github.com/lib/pq"
1012
)
1113

@@ -22,11 +24,37 @@ func main() {
2224
os.Exit(1)
2325
}
2426

25-
err := migrations.SchemaTest(&cfg, allMigrations)
27+
commonSchema := "common"
28+
29+
postgresConnectionString := fmt.Sprintf(
30+
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
31+
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database)
32+
connectionStringProvider, err := pgutils.NewConnectionStringProviderFromURLString(context.Background(), postgresConnectionString)
33+
if err != nil {
34+
log.Fatalf("NewConnectionStringProviderFromURLString: %v", err)
35+
}
36+
dbWithSearchPath, err := pgutils.ConnectDB(pgutils.ToConnector(pgutils.WithSchemaSearchPath(connectionStringProvider, commonSchema)))
37+
if err != nil {
38+
log.Fatalf("ConnectDB: %v", err)
39+
}
40+
41+
commonSetup := func() error {
42+
err = migrations.EnsureSchema(dbWithSearchPath, commonSchema)
43+
if err != nil {
44+
return fmt.Errorf("EnsureSchema: %v", err)
45+
}
46+
return migrations.Migrate(dbWithSearchPath, commonMigrations)
47+
}
48+
49+
err = migrations.SchemaTestWithSetup(&cfg, allMigrations, commonSetup)
2650
if err != nil {
2751
log.Fatalf("First schema test failed: %s", err)
2852
}
29-
err = migrations.SchemaTest(&cfg, allMigrations)
53+
54+
// Database must be empty before calling SchemaTest a second time.
55+
dbWithSearchPath.MustExec(fmt.Sprintf("DROP SCHEMA %s CASCADE", commonSchema))
56+
57+
err = migrations.SchemaTestWithSetup(&cfg, allMigrations, commonSetup)
3058
if err != nil {
3159
log.Fatalf("Second schema test failed: %s", err)
3260
}

cmd/schema-test/migrations.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ package main
22

33
import "github.com/corbaltcode/go-libraries/migrations"
44

5+
var commonMigrations = []migrations.NamedMigration{
6+
{
7+
Name: "Create a common table",
8+
Migration: migrations.StaticMigration([]string{
9+
`CREATE TABLE common (
10+
id serial PRIMARY KEY
11+
)`,
12+
}),
13+
Reverse: migrations.StaticMigration([]string{
14+
`DROP TABLE common`,
15+
}),
16+
},
17+
}
18+
519
var allMigrations = []migrations.NamedMigration{
620
{
721
Name: "Create a table",
@@ -77,4 +91,16 @@ var allMigrations = []migrations.NamedMigration{
7791
`DROP VIEW v`,
7892
}),
7993
},
94+
{
95+
Name: "Create a table referencing a common table",
96+
Migration: migrations.StaticMigration([]string{
97+
`CREATE TABLE dependent (
98+
id serial PRIMARY KEY,
99+
common_id integer REFERENCES common.common(id)
100+
)`,
101+
}),
102+
Reverse: migrations.StaticMigration([]string{
103+
`DROP TABLE dependent`,
104+
}),
105+
},
80106
}

migrations/verify.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,9 @@ func migrateAndRollback(emptyDBConfig *PostgresConfig, db *sqlx.DB, allMigration
107107
return err
108108
}
109109

110-
// Schema test expects a new *empty* postgres database.
111-
// It will:
112-
// 1. Apply all migrations
113-
// 2. Reverse all migrations
114-
// 3. For each migration:
115-
// a. Apply the migration
116-
// b. Reverse the migration
117-
// c. Apply the migration again
118-
//
119-
// Before and after each step it will use pg_dump to dump the database schema.
120-
// It will verify that:
121-
// A. The schema is the same after reversing as before applying.
122-
// B. (If re-applying) The schema is the same after applying as after re-applying.
123-
//
124-
// You must have `pg_dump` in your `PATH` to run this.
125-
func SchemaTest(emptyDBConfig *PostgresConfig, allMigrations []NamedMigration) error {
110+
// Does a SchemaTest but calls the provided setup function after verifying that the
111+
// database is empty.
112+
func SchemaTestWithSetup(emptyDBConfig *PostgresConfig, allMigrations []NamedMigration, setup func() error) error {
126113
for _, v := range []string{
127114
emptyDBConfig.Host,
128115
emptyDBConfig.Port,
@@ -145,6 +132,10 @@ func SchemaTest(emptyDBConfig *PostgresConfig, allMigrations []NamedMigration) e
145132
if err != nil {
146133
return err
147134
}
135+
err = setup()
136+
if err != nil {
137+
return fmt.Errorf("setup: %s", err)
138+
}
148139
err = Migrate(db, []NamedMigration{})
149140
if err != nil {
150141
return fmt.Errorf("Setting up migrations table failed: %s", err)
@@ -170,3 +161,22 @@ func SchemaTest(emptyDBConfig *PostgresConfig, allMigrations []NamedMigration) e
170161
}
171162
return nil
172163
}
164+
165+
// Schema test expects a new *empty* postgres database.
166+
// It will:
167+
// 1. Apply all migrations
168+
// 2. Reverse all migrations
169+
// 3. For each migration:
170+
// a. Apply the migration
171+
// b. Reverse the migration
172+
// c. Apply the migration again
173+
//
174+
// Before and after each step it will use pg_dump to dump the database schema.
175+
// It will verify that:
176+
// A. The schema is the same after reversing as before applying.
177+
// B. (If re-applying) The schema is the same after applying as after re-applying.
178+
//
179+
// You must have `pg_dump` in your `PATH` to run this.
180+
func SchemaTest(emptyDBConfig *PostgresConfig, allMigrations []NamedMigration) error {
181+
return SchemaTestWithSetup(emptyDBConfig, allMigrations, func() error { return nil })
182+
}

0 commit comments

Comments
 (0)