Skip to content

Commit a8a6b44

Browse files
author
Miguel Molina
committed
run migrations in a transaction
2 parents f52d19b + be44523 commit a8a6b44

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

generator/generator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ func TestMigrationGeneratorGenerate(t *testing.T) {
7070

7171
content, err := ioutil.ReadFile(g.migrationFile(migrationUp, g.now()))
7272
require.NoError(t, err)
73-
require.Equal(t, expectedTable2+"\n\n", string(content))
73+
require.Equal(t, "BEGIN;\n\n"+expectedTable2+"\n\nCOMMIT;\n", string(content))
7474

7575
content, err = ioutil.ReadFile(g.migrationFile(migrationDown, g.now()))
7676
require.NoError(t, err)
77-
require.Equal(t, "DROP TABLE table2;\n\n", string(content))
77+
require.Equal(t, "BEGIN;\n\nDROP TABLE table2;\n\nCOMMIT;\n", string(content))
7878

7979
expected, err := migration.Lock.MarshalText()
8080
require.NoError(t, err)

generator/migration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ func (cs ChangeSet) sorted(dropIndex, createIndex map[string]*TableSchema) (Chan
323323

324324
func (cs ChangeSet) MarshalText() ([]byte, error) {
325325
var buf bytes.Buffer
326+
buf.WriteString("BEGIN;\n\n")
326327
for _, c := range cs {
327328
bytes, err := c.MarshalText()
328329
if err != nil {
@@ -331,6 +332,7 @@ func (cs ChangeSet) MarshalText() ([]byte, error) {
331332
buf.Write(bytes)
332333
buf.WriteRune('\n')
333334
}
335+
buf.WriteString("COMMIT;\n")
334336
return buf.Bytes(), nil
335337
}
336338

generator/migration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestChangeSet(t *testing.T) {
6262
&DropTable{"foo"},
6363
&DropColumn{"col", "table"},
6464
},
65-
"DROP TABLE foo;\n\nALTER TABLE table DROP COLUMN col;\n\n",
65+
"BEGIN;\n\nDROP TABLE foo;\n\nALTER TABLE table DROP COLUMN col;\n\nCOMMIT;\n",
6666
)
6767
}
6868

0 commit comments

Comments
 (0)