Skip to content

Commit ddb60ec

Browse files
Do not create migrations table on dump
When running dbmate dump, no changes to the database should be made.
1 parent 3fe5366 commit ddb60ec

12 files changed

Lines changed: 164 additions & 1 deletion

File tree

pkg/dbmate/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (db *DB) DumpSchema() error {
200200
return err
201201
}
202202

203-
sqlDB, err := db.openDatabaseForMigration(drv)
203+
sqlDB, err := drv.Open()
204204
if err != nil {
205205
return err
206206
}

pkg/dbmate/db_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,37 @@ func TestDumpSchema(t *testing.T) {
136136
require.Contains(t, string(schema), "-- Dbmate schema migrations")
137137
}
138138

139+
func TestDumpSchemaDoesNotCreateMigrationTable(t *testing.T) {
140+
db := newTestDB(t, sqliteTestURL(t))
141+
142+
// create custom schema file directory
143+
dir := t.TempDir()
144+
145+
// create schema.sql in subdirectory to test creating directory
146+
db.SchemaFile = filepath.Join(dir, "/schema/schema.sql")
147+
148+
// drop database
149+
err := db.Drop()
150+
require.NoError(t, err)
151+
152+
// create and migrate
153+
err = db.Create()
154+
require.NoError(t, err)
155+
156+
// schema.sql should not exist
157+
_, err = os.Stat(db.SchemaFile)
158+
require.True(t, os.IsNotExist(err))
159+
160+
// dump schema
161+
err = db.DumpSchema()
162+
require.NoError(t, err)
163+
164+
// verify schema
165+
schema, err := os.ReadFile(db.SchemaFile)
166+
require.NoError(t, err)
167+
require.NotContains(t, string(schema), "-- Dbmate schema migrations")
168+
}
169+
139170
func TestAutoDumpSchema(t *testing.T) {
140171
db := newTestDB(t, sqliteTestURL(t))
141172
db.AutoDumpSchema = true

pkg/driver/bigquery/bigquery.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ func (drv *Driver) schemaDump(db *sql.DB) ([]byte, error) {
221221
}
222222

223223
func (drv *Driver) schemaMigrationsDump(db *sql.DB) ([]byte, error) {
224+
exists, err := drv.MigrationsTableExists(db)
225+
if err != nil {
226+
return nil, fmt.Errorf("failed to check if migration table exists: %w", err)
227+
}
228+
if !exists {
229+
return nil, nil
230+
}
231+
224232
migrationsTable := drv.migrationsTableName
225233

226234
// load applied migrations

pkg/driver/bigquery/bigquery_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,22 @@ func TestGoogleBigQueryDumpSchema(t *testing.T) {
373373
" ('abc2');\n")
374374
})
375375
}
376+
377+
func TestGoogleBigQueryDumpSchemaNoMigrations(t *testing.T) {
378+
t.Run("default migrations table", func(t *testing.T) {
379+
drv := testGoogleBigQueryDriver(t)
380+
381+
// prepare database
382+
db := prepTestGoogleBigQueryDB(t)
383+
defer dbutil.MustClose(db)
384+
385+
// DumpSchema should return schema
386+
config, err := drv.getConfig(db)
387+
require.NoError(t, err)
388+
389+
schema, err := drv.DumpSchema(db)
390+
require.NoError(t, err)
391+
require.NotContains(t, string(schema), fmt.Sprintf("CREATE TABLE `%s.%s.schema_migrations`", config.projectID, config.dataSet))
392+
require.NotContains(t, string(schema), "\n--\n-- Dbmate schema migrations\n")
393+
})
394+
}

pkg/driver/clickhouse/clickhouse.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ func (drv *Driver) schemaDump(db *sql.DB, buf *bytes.Buffer, databaseName string
197197
}
198198

199199
func (drv *Driver) schemaMigrationsDump(db *sql.DB, buf *bytes.Buffer) error {
200+
exists, err := drv.MigrationsTableExists(db)
201+
if err != nil {
202+
return fmt.Errorf("failed to check if migration table exists: %w", err)
203+
}
204+
if !exists {
205+
return nil
206+
}
207+
200208
migrationsTable := drv.quotedMigrationsTableName()
201209

202210
// load applied migrations

pkg/driver/clickhouse/clickhouse_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ func TestClickHouseDumpSchema(t *testing.T) {
137137
require.EqualError(t, err, "code: 81, message: Database fakedb doesn't exist")
138138
}
139139

140+
func TestClickHouseDumpSchemaNoMigrations(t *testing.T) {
141+
drv := testClickHouseDriver(t)
142+
drv.migrationsTableName = "test_migrations"
143+
144+
// prepare database
145+
db := prepTestClickHouseDB(t, drv)
146+
defer dbutil.MustClose(db)
147+
148+
// DumpSchema should return schema
149+
schema, err := drv.DumpSchema(db)
150+
require.NoError(t, err)
151+
require.NotContains(t, string(schema), "CREATE TABLE "+drv.databaseName()+".test_migrations")
152+
require.NotContains(t, string(schema), "--\n-- Dbmate schema migrations\n")
153+
}
154+
140155
func TestClickHouseDatabaseExists(t *testing.T) {
141156
drv := testClickHouseDriver(t)
142157

pkg/driver/mysql/mysql.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ func (drv *Driver) mysqldumpArgs() []string {
158158
}
159159

160160
func (drv *Driver) schemaMigrationsDump(db *sql.DB) ([]byte, error) {
161+
exists, err := drv.MigrationsTableExists(db)
162+
if err != nil {
163+
return nil, fmt.Errorf("failed to check if migration table exists: %w", err)
164+
}
165+
if !exists {
166+
return nil, nil
167+
}
168+
161169
migrationsTable := drv.quotedMigrationsTableName()
162170

163171
// load applied migrations

pkg/driver/mysql/mysql_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ func TestMySQLDumpSchemaContainsNoAutoIncrement(t *testing.T) {
247247
require.NotContains(t, string(schema), "AUTO_INCREMENT=")
248248
}
249249

250+
func TestMySQLDumpSchemaDoesNotCreateMigrationsTable(t *testing.T) {
251+
drv := testMySQLDriver(t)
252+
drv.migrationsTableName = "test_migrations"
253+
254+
// prepare database
255+
db := prepTestMySQLDB(t)
256+
defer dbutil.MustClose(db)
257+
_, err := db.Exec(`create table foo_table (id int not null primary key)`)
258+
require.NoError(t, err)
259+
260+
// DumpSchema should return schema
261+
schema, err := drv.DumpSchema(db)
262+
require.NoError(t, err)
263+
require.NotContains(t, string(schema), "CREATE TABLE `test_migrations`")
264+
require.Contains(t, string(schema), "CREATE TABLE `foo_table`")
265+
}
266+
250267
func TestMySQLDatabaseExists(t *testing.T) {
251268
drv := testMySQLDriver(t)
252269

pkg/driver/postgres/postgres.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ func (drv *Driver) DropDatabase() error {
171171
}
172172

173173
func (drv *Driver) schemaMigrationsDump(db *sql.DB) ([]byte, error) {
174+
exists, err := drv.MigrationsTableExists(db)
175+
if err != nil {
176+
return nil, fmt.Errorf("failed to check if migration table exists: %w", err)
177+
}
178+
if !exists {
179+
return nil, nil
180+
}
181+
174182
migrationsTable, err := drv.quotedMigrationsTableName(db)
175183
if err != nil {
176184
return nil, err

pkg/driver/postgres/postgres_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ func TestPostgresDumpSchema(t *testing.T) {
274274
" ('abc1'),\n"+
275275
" ('abc2');\n")
276276
})
277+
278+
t.Run("no migrations table on dump", func(t *testing.T) {
279+
drv := testPostgresDriver(t)
280+
281+
// prepare database
282+
db := prepTestPostgresDB(t)
283+
defer dbutil.MustClose(db)
284+
285+
// DumpSchema should return schema
286+
schema, err := drv.DumpSchema(db)
287+
require.NoError(t, err)
288+
require.NotContains(t, string(schema), "CREATE TABLE public.schema_migrations")
289+
require.Contains(t, string(schema), "\n--\n"+
290+
"-- PostgreSQL database dump complete\n"+
291+
"--\n\n")
292+
require.NotContains(t, string(schema), "-- Dbmate schema migrations\n"+
293+
"--\n\n"+
294+
"INSERT INTO public.schema_migrations (version) VALUES\n"+
295+
" ('abc1'),\n"+
296+
" ('abc2');\n")
297+
})
277298
}
278299

279300
func TestPostgresDatabaseExists(t *testing.T) {

0 commit comments

Comments
 (0)