3030 // fully executed yet. So this time needs to be chosen correctly to be
3131 // longer than the longest expected individual test run time.
3232 DefaultPostgresFixtureLifetime = 60 * time .Minute
33+
34+ // postgresSchemaReplacements is a map of schema strings that need to be
35+ // replaced for postgres. This is needed because we write the schemas
36+ // to work with sqlite primarily, and postgres has some differences.
37+ postgresSchemaReplacements = map [string ]string {
38+ "BLOB" : "BYTEA" ,
39+ "INTEGER PRIMARY KEY" : "SERIAL PRIMARY KEY" ,
40+ "BIGINT PRIMARY KEY" : "BIGSERIAL PRIMARY KEY" ,
41+ "TIMESTAMP" : "TIMESTAMP WITHOUT TIME ZONE" ,
42+ }
3343)
3444
3545// PostgresConfig holds the postgres database configuration.
@@ -107,44 +117,41 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) {
107117 rawDb .SetConnMaxLifetime (connMaxLifetime )
108118 rawDb .SetConnMaxIdleTime (connMaxIdleTime )
109119
110- if ! cfg .SkipMigrations {
111- // Now that the database is open, populate the database with
112- // our set of schemas based on our embedded in-memory file
113- // system.
114- //
115- // First, we'll need to open up a new migration instance for
116- // our current target database: sqlite.
117- driver , err := postgres_migrate .WithInstance (
118- rawDb , & postgres_migrate.Config {},
119- )
120- if err != nil {
121- return nil , err
122- }
123-
124- postgresFS := newReplacerFS (sqlSchemas , map [string ]string {
125- "BLOB" : "BYTEA" ,
126- "INTEGER PRIMARY KEY" : "SERIAL PRIMARY KEY" ,
127- "BIGINT PRIMARY KEY" : "BIGSERIAL PRIMARY KEY" ,
128- "TIMESTAMP" : "TIMESTAMP WITHOUT TIME ZONE" ,
129- })
130-
131- err = applyMigrations (
132- postgresFS , driver , "sqlc/migrations" , cfg .DBName ,
133- )
134- if err != nil {
135- return nil , err
136- }
137- }
138-
139120 queries := sqlc .NewPostgres (rawDb )
140-
141- return & PostgresStore {
121+ s := & PostgresStore {
142122 cfg : cfg ,
143123 BaseDB : & BaseDB {
144124 DB : rawDb ,
145125 Queries : queries ,
146126 },
147- }, nil
127+ }
128+
129+ // Now that the database is open, populate the database with our set of
130+ // schemas based on our embedded in-memory file system.
131+ if ! cfg .SkipMigrations {
132+ if err := s .ExecuteMigrations (TargetLatest ); err != nil {
133+ return nil , fmt .Errorf ("error executing migrations: " +
134+ "%w" , err )
135+ }
136+ }
137+
138+ return s , nil
139+ }
140+
141+ // ExecuteMigrations runs migrations for the Postgres database, depending on the
142+ // target given, either all migrations or up to a given version.
143+ func (s * PostgresStore ) ExecuteMigrations (target MigrationTarget ) error {
144+ driver , err := postgres_migrate .WithInstance (
145+ s .DB , & postgres_migrate.Config {},
146+ )
147+ if err != nil {
148+ return fmt .Errorf ("error creating postgres migration: %w" , err )
149+ }
150+
151+ postgresFS := newReplacerFS (sqlSchemas , postgresSchemaReplacements )
152+ return applyMigrations (
153+ postgresFS , driver , "sqlc/migrations" , s .cfg .DBName , target ,
154+ )
148155}
149156
150157// NewTestPostgresDB is a helper function that creates a Postgres database for
@@ -164,3 +171,27 @@ func NewTestPostgresDB(t *testing.T) *PostgresStore {
164171
165172 return store
166173}
174+
175+ // NewTestPostgresDBWithVersion is a helper function that creates a Postgres
176+ // database for testing and migrates it to the given version.
177+ func NewTestPostgresDBWithVersion (t * testing.T , version uint ) * PostgresStore {
178+ t .Helper ()
179+
180+ t .Logf ("Creating new Postgres DB for testing, migrating to version %d" ,
181+ version )
182+
183+ sqlFixture := NewTestPgFixture (t , DefaultPostgresFixtureLifetime , true )
184+ storeCfg := sqlFixture .GetConfig ()
185+ storeCfg .SkipMigrations = true
186+ store , err := NewPostgresStore (storeCfg )
187+ require .NoError (t , err )
188+
189+ err = store .ExecuteMigrations (TargetVersion (version ))
190+ require .NoError (t , err )
191+
192+ t .Cleanup (func () {
193+ sqlFixture .TearDown (t )
194+ })
195+
196+ return store
197+ }
0 commit comments