Skip to content
Snippets Groups Projects

Add sqlutil package

Merged ale requested to merge sql into master
2 files
+ 152
5
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 9
5
@@ -11,7 +11,7 @@ import (
)
// DebugMigrations can be set to true to dump statements to stderr.
var DebugMigrations = true
var DebugMigrations bool
const defaultOptions = "?cache=shared&_busy_timeout=10000&_journal=WAL&_sync=OFF"
@@ -67,6 +67,7 @@ func migrate(db *sql.DB, migrations []func(*sql.Tx) error) error {
if idx == len(migrations) {
// Already fully migrated, nothing needed.
return nil
} else if idx > len(migrations) {
return fmt.Errorf("database is at version %d, which is more recent than this binary understands", idx)
}
@@ -77,10 +78,13 @@ func migrate(db *sql.DB, migrations []func(*sql.Tx) error) error {
}
}
// For some reason, ? substitution doesn't work in PRAGMA
// statements, sqlite reports a parse error.
if _, err := tx.Exec(fmt.Sprintf("PRAGMA user_version=%d", len(migrations))); err != nil {
return fmt.Errorf("recording new DB version: %w", err)
if n := len(migrations); n > 0 {
// For some reason, ? substitution doesn't work in PRAGMA
// statements, sqlite reports a parse error.
if _, err := tx.Exec(fmt.Sprintf("PRAGMA user_version=%d", n)); err != nil {
return fmt.Errorf("recording new DB version: %w", err)
}
log.Printf("db migration: upgraded schema version %d -> %d", idx, n)
}
return tx.Commit()
Loading