~aleteoryx/tclircc

ref: 1c9eee37d59cadc5728509594b0fc666f3fa510c tclircc/migrate_core.tcl -rw-r--r-- 1.3 KiB
1c9eee37aleteoryx manifest loading basics a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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