While reading through SQLite's PRAGMA statements page I stumbled upon the user_version
pragma. The description says this:
The user-version is an integer that is available to applications to use however they want. SQLite makes no use of the user-version itself.
I immediately thought about using it to store the database's current schema version and wasn't surprised to discover I'm not alone!
// get current schema version
var currentVersion int
db.QueryRow(`PRAGMA user_version;`).Scan(¤tVersion)
// apply your migrations logic ...
// set your schema version
cmd := fmt.Sprintf(`PRAGMA user_version = %d;`, newVersion)
tx.Exec(cmd)
You may have noticed that I'm doing string formatting instead of passing the newVersion
as a parameter and the reason is that this is not an expression, it's a statement.