# TODO: make this more robust maybe
interp create migrator
migrator eval {
lappend migrations 0 {init plugins} {
-- init plugins
CREATE TABLE plugins (slug TEXT NOT NULL, -- corresponds directly to manifest
namespace TEXT NOT NULL, -- corresponds directly to manifest
version TEXT NOT NULL, -- corresponds directly to manifest
manifest_hash TEXT NOT NULL, -- sha256
trusted_until TEXT NOT NULL, -- ignore manifest changes if loading before timestamp
updated_on TEXT NOT NULL, -- date of last update
priority INTEGER NOT NULL, -- load priority
PRIMARY KEY (slug, namespace));
}
}
migrator alias db core_db
migrator eval {
package require logger
package require Tk
wm withdraw .
set log [logger::init tclircc::db::migrate]
${log}::debug "starting migrations"
if ![db eval {
SELECT count(*) FROM sqlite_master WHERE name="_migrations"}] {
${log}::info "initializing core db!"
db eval {CREATE TABLE _migrations (id INTEGER PRIMARY KEY, date TEXT)}
}
foreach {id name contents} $migrations {
# TODO: debug interpolation?
if ![db eval "SELECT count(*) FROM _migrations WHERE id=$id"] {
${log}::info "performing migration $id: $name"
db eval BEGIN
set migrationdate [clock seconds]
db eval $contents
db eval "INSERT INTO _migrations (id, date) VALUES ($id, $migrationdate)"
set last $id
db eval COMMIT
} elseif [info exists last] {
${log}::warn "completed migrations exist after incomplete migration $last, core database may be corrupted!"
}
}
${log}::info "core db migrated!"
}
interp delete migrator