This commit is contained in:
parent
9d5c0bcbb1
commit
82c7f57078
26
migrator.go
26
migrator.go
@ -50,7 +50,7 @@ func (d *Db) MigrateDb(filesystem ReadDirFileFS, directory string) error {
|
|||||||
return fmt.Errorf("error opening migration script %s: %v", migrationScript, err)
|
return fmt.Errorf("error opening migration script %s: %v", migrationScript, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = performSingleMigration(err, d, migrationScript, targetVersion)
|
err = performSingleMigration(d, migrationScript, targetVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -61,21 +61,29 @@ func (d *Db) MigrateDb(filesystem ReadDirFileFS, directory string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func performSingleMigration(err error, d *Db, migrationScript []byte, targetVersion int) error {
|
func performSingleMigration(d *Db, migrationScript []byte, targetVersion int) error {
|
||||||
|
script := string(migrationScript)
|
||||||
|
// Split script based on semicolon
|
||||||
|
statements := strings.Split(script, ";")
|
||||||
|
|
||||||
tx, err := d.Begin()
|
tx, err := d.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error beginning transaction: %v", err)
|
return fmt.Errorf("error beginning transaction: %v", err)
|
||||||
}
|
}
|
||||||
defer tx.MustRollback()
|
defer tx.MustRollback()
|
||||||
|
|
||||||
err = tx.Query(string(migrationScript)).Exec()
|
for _, statement := range statements {
|
||||||
if err != nil {
|
statement = strings.TrimSpace(statement)
|
||||||
return fmt.Errorf("error performing migration: %v", err)
|
err = tx.Query(statement).Exec()
|
||||||
}
|
if err != nil {
|
||||||
|
return fmt.Errorf("error performing migration: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Query(fmt.Sprintf("PRAGMA user_version = %d", targetVersion)).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error updating version: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
err = tx.Query(fmt.Sprintf("PRAGMA user_version = %d", targetVersion)).Exec()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error updating version: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tx.Commit()
|
err = tx.Commit()
|
||||||
|
3
testMigrations/3_multicomment.sql
Normal file
3
testMigrations/3_multicomment.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
create table testTable(value text);
|
||||||
|
|
||||||
|
insert into testTable(value) values ('testValue');
|
Loading…
x
Reference in New Issue
Block a user