~aleteoryx/tclircc

ref: 63922942ea1eb76534810686b711a550e3745c57 tclircc/migrate_core.tcl -rw-r--r-- 1.6 KiB
63922942aleteoryx plugin version comparison, improved manifest reading 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
49
50
# 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
                      hashes TEXT NOT NULL, -- dict of path -> hash
                      trusted BOOL NOT NULL, -- bypass security checks for plugins with this name
                      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