During a GoGraz meetup somewhen in 2019 Matthias mentioned that SQLite had a pragma (basically an attribute of the database) called user_version
which might be used for storing the βversionβ of a database. Could this perhaps be useful for a migration library to store at what version the database scheme was at?
Sadly, I couldn’t find any migration library for Go that used that feature and so I just wrote my own as part of the Geotrace project and eventually moved that into it’s own project on GitHub called SQLiteMigrate.
Usage is pretty simple:
# You create a registry where your migrations should live.
reg := mig.NewRegistry()
# New, create a migration which consists of SQL statements
# for upgrading and downgrading a scheme.
reg.RegisterMigration([]string{
`CREATE TABLE users (id integer primary key autoincrement)`,
}, []string{})
db, _ := sql.Open("sqlite3", "test.sqlite")
defer db.Close()
# Finally, you apply the registry to a SQLite database:
if err := reg.Apply(ctx, db); err != nil {
log.Fatal("Failed to apply migration: %s", err.Error())
}
So far I’ve only used that within Geotrace but I want to also have it in webmentiond before that project hits 1.0.0!
Anyway, I haven’t had a new to touch the code in quite a while so I just wanted to finally put a v1.0.0 on it. That’s what I’ve now finally done yesterday afternoon! I don’t anticipate any large API changes on that tiny project as it works for my own use-cases for now. Perhaps this is also something that might be useful to you, too π
Do you want to give me feedback about this article in private? Please send it to comments@zerokspot.com.
Alternatively, this website also supports Webmentions. If you write a post on a blog that supports this technique, I should get notified about your link π