~aleteoryx/tclircc

ref: bcf1accce7bc7023aeac374e70380e3dbea98733 tclircc/src/migrate_core.tcl -rw-r--r-- 1.8 KiB
bcf1acccAleteoryx docgen cleaning 22 days 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
51
52
53
54
55
56
57
58
59
#***
# [manpage_begin migrate_core tclircc 0.0.1]
# [titledesc {Script migrate_core.tcl}]
# [description]

# 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

#***
# [manpage_end]