Skip to content

Commit 1b3bea5

Browse files
ViktorT-11ziggie1984
authored andcommitted
sqldb/v2: limit MigrationExecutor interface
This commit limits the MigrationExecutor interface due to the following reasoning: 1. SkipMigrations() and DefaultTarget() should not be on the interface Both are only used by ApplyAllMigrations, which immediately passes the results back into the same executor. They are internal implementation details and should be folded into ExecuteMigrations itself. 2. SetSchemaVersion and GetSchemaVersion are test-only but on the production interface Every caller of these in sqldb/v2 is in test files. The SetSchemaVersion comment even says "USE WITH CAUTION" — dangerous test utilities should not be on an interface that every real consumer must implement. They should be accessible on the concrete types only and used directly in tests without going through the interface. 3. ExecuteMigrations should not take a MigrationTarget parameter for the normal path On the normal startup path, callers just do executor.ExecuteMigrations(executor.DefaultTarget(), stream) — asking the executor for its default and handing it straight back. The method should run to latest by default; a version override for tests can live on the concrete type instead.
1 parent 9946d52 commit 1b3bea5

5 files changed

Lines changed: 33 additions & 52 deletions

File tree

sqldb/v2/migrations.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,11 @@ type MigrationTarget func(mig *migrate.Migrate,
9696

9797
// MigrationExecutor is an interface that abstracts the migration functionality.
9898
type MigrationExecutor interface {
99-
// ExecuteMigrations runs database migrations up to the specified target
100-
// version or all migrations if no target is specified. A migration may
101-
// include a schema change, a custom migration function, or both.
102-
// Developers must ensure that migrations are defined in the correct
103-
// order. Migration details are stored in the global variable
104-
// migrationConfig.
105-
ExecuteMigrations(target MigrationTarget, set MigrationSet) error
106-
107-
// GetSchemaVersion returns the current schema version of the database.
108-
GetSchemaVersion() (int, bool, error)
109-
110-
// SetSchemaVersion sets the schema version of the database.
111-
//
112-
// NOTE: This alters the internal database schema tracker. USE WITH
113-
// CAUTION!!!
114-
SetSchemaVersion(version int, dirty bool) error
115-
116-
// DefaultTarget returns the default migration target.
117-
DefaultTarget() MigrationTarget
118-
119-
// SkipMigrations indicates if the SQL and corresponding code migrations
120-
// will be skipped.
121-
SkipMigrations() bool
99+
// ExecuteMigrations runs database migrations for the given migration
100+
// set using the executor's default production migration target. A
101+
// migration may include a schema change, a custom migration function,
102+
// or both.
103+
ExecuteMigrations(set MigrationSet) error
122104
}
123105

124106
var (
@@ -403,13 +385,8 @@ func (t *replacerFile) Close() error {
403385
// ApplyAllMigrations applies both the SQLC and custom in-code migrations to the
404386
// SQLite database.
405387
func ApplyAllMigrations(executor MigrationExecutor, sets []MigrationSet) error {
406-
// Execute migrations unless configured to skip them.
407-
if executor.SkipMigrations() {
408-
return nil
409-
}
410-
411388
for _, set := range sets {
412-
err := executor.ExecuteMigrations(executor.DefaultTarget(), set)
389+
err := executor.ExecuteMigrations(set)
413390
if err != nil {
414391
return fmt.Errorf("error applying migrations: %w", err)
415392
}

sqldb/v2/postgres.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,19 @@ func errPostgresMigration(err error) error {
167167
return fmt.Errorf("error creating postgres migration: %w", err)
168168
}
169169

170-
// ExecuteMigrations runs migrations for the Postgres database, depending on the
171-
// target given, either all migrations or up to a given version.
172-
func (s *PostgresStore) ExecuteMigrations(target MigrationTarget,
170+
// ExecuteMigrations runs migrations for the Postgres database using the
171+
// default production migration target.
172+
func (s *PostgresStore) ExecuteMigrations(set MigrationSet) error {
173+
if s.cfg.SkipMigrations {
174+
return nil
175+
}
176+
177+
return s.executeMigrations(TargetLatest, set)
178+
}
179+
180+
// executeMigrations runs migrations for the Postgres database, depending on
181+
// the target given, either all migrations or up to a given version.
182+
func (s *PostgresStore) executeMigrations(target MigrationTarget,
173183
set MigrationSet) error {
174184

175185
dbName, err := getDatabaseNameFromDSN(s.cfg.Dsn)
@@ -231,11 +241,3 @@ func (s *PostgresStore) SetSchemaVersion(version int, dirty bool) error {
231241

232242
return driver.SetVersion(version, dirty)
233243
}
234-
235-
func (s *PostgresStore) DefaultTarget() MigrationTarget {
236-
return TargetLatest
237-
}
238-
239-
func (s *PostgresStore) SkipMigrations() bool {
240-
return s.cfg.SkipMigrations
241-
}

sqldb/v2/postgres_fixture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func NewTestPostgresDBWithVersion(t testing.TB, fixture *TestPgFixture,
197197
store, err := NewPostgresStore(storeCfg)
198198
require.NoError(t, err)
199199

200-
err = store.ExecuteMigrations(TargetVersion(version), sets)
200+
err = store.executeMigrations(TargetVersion(version), sets)
201201
require.NoError(t, err)
202202

203203
t.Cleanup(func() {

sqldb/v2/sqlite.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,19 @@ func (s *SqliteStore) backupAndMigrate(mig *migrate.Migrate,
251251
return mig.Up()
252252
}
253253

254-
// ExecuteMigrations runs migrations for the sqlite database, depending on the
254+
// ExecuteMigrations runs migrations for the sqlite database using the default
255+
// production migration target.
256+
func (s *SqliteStore) ExecuteMigrations(set MigrationSet) error {
257+
if s.Config.SkipMigrations {
258+
return nil
259+
}
260+
261+
return s.executeMigrations(s.backupAndMigrate, set)
262+
}
263+
264+
// executeMigrations runs migrations for the sqlite database, depending on the
255265
// target given, either all migrations or up to a given version.
256-
func (s *SqliteStore) ExecuteMigrations(target MigrationTarget,
266+
func (s *SqliteStore) executeMigrations(target MigrationTarget,
257267
set MigrationSet) error {
258268

259269
driver, err := sqlite_migrate.WithInstance(
@@ -285,14 +295,6 @@ func (s *SqliteStore) ExecuteMigrations(target MigrationTarget,
285295
)
286296
}
287297

288-
func (s *SqliteStore) DefaultTarget() MigrationTarget {
289-
return s.backupAndMigrate
290-
}
291-
292-
func (s *SqliteStore) SkipMigrations() bool {
293-
return s.Config.SkipMigrations
294-
}
295-
296298
// GetSchemaVersion returns the current schema version of the SQLite database.
297299
func (s *SqliteStore) GetSchemaVersion() (int, bool, error) {
298300
driver, err := sqlite_migrate.WithInstance(

sqldb/v2/sqlite_test_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func NewTestSqliteDBWithVersion(t *testing.T, set MigrationSet,
7575
}, dbFileName)
7676
require.NoError(t, err)
7777

78-
err = sqlDB.ExecuteMigrations(TargetVersion(version), set)
78+
err = sqlDB.executeMigrations(TargetVersion(version), set)
7979
require.NoError(t, err)
8080

8181
t.Cleanup(func() {

0 commit comments

Comments
 (0)