# TODO: make this more robust maybe interp create migrator migrator eval { lappend migrations 0 {init plugins} { -- init plugins CREATE TABLE plugins (slug TEXT PRIMARY KEY, version INTEGER, hashes TEXT, trusted BOOL, priority INTEGER); } } 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