#!/bin/sh
# Journals written by earlier pic versions open with the current one: the
# schema-3 fixture is migrated in place, passes pic check, keeps its history,
# and takes new work. Needs the sqlite3 command to build the fixture.
set -eu
PIC=${PIC:?set PIC to the pic binary}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-migrate.XXXXXX")
# pic keeps user state under HOME: never the real one.
export HOME="$T/home"
mkdir -p "$HOME"
trap 'rm -rf -- "$T"' EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
DB="$T/journal.sqlite"
mkdir "$T/ws"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic() { "$PIC" --db "$DB" --workspace "$T/ws" "$@"; }
pic check > /dev/null || fail "migrated journal fails pic check"
out=$(pic dump)
printf '%s\n' "$out" | grep -qF 'entry 2 1 assistant | [{"type":"thinking","thinking":"Write it.","signature":"SIG-OLD"},{"type":"toolCall","id":"toolu_old","name":"write","arguments":{"path":"notes.txt","content":"hi"}}]' ||
fail "reply not converted to the neutral form"
printf '%s\n' "$out" | grep -q '^entry 4 1 answer | \[{"type":"text","text":"done"}\]$' || fail "answer"
printf '%s\n' "$out" | grep -q '^generation 1 1.0.1 done stop=toolUse' || fail "stop reasons not renamed"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "version not 19"
# Version 8 adds the queue of steering and follow-up messages, empty.
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_queued")" = 0 ] || fail "no empty pic_queued"
# Version 9 adds images and their attachments, empty.
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_image") $(sqlite3 "$DB" "SELECT count(*) FROM pic_attachment")" = "0 0" ] ||
fail "no empty pic_image and pic_attachment"
# Every journal has the table of commands the user ran (pic bash), empty.
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_bash")" = 0 ] || fail "no empty pic_bash"
# Times older journals never wrote come from the ask key (054 ms, leading zero kept).
[ "$(sqlite3 "$DB" "SELECT group_concat(DISTINCT created_ms) FROM pic_entry WHERE submission_id = 1")" = 1795035064054 ] ||
fail "entry times: $(sqlite3 "$DB" "SELECT group_concat(coalesce(created_ms, 'null')) FROM pic_entry")"
[ "$(sqlite3 "$DB" "SELECT created_ms FROM pic_session")" = 1795035064054 ] || fail "session time: $(sqlite3 "$DB" "SELECT created_ms FROM pic_session")"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_session")" = 1 ] || fail "submission not given a session"
[ "$(sqlite3 "$DB" "SELECT group_concat(coalesce(parent_seq, 0), ' ') FROM (SELECT parent_seq FROM pic_entry WHERE submission_id = 1 ORDER BY seq)")" = "0 1 2 3" ] ||
fail "entries not chained"
[ "$(sqlite3 "$DB" "SELECT provider || '/' || model || ' ' || (native LIKE '%tool_use%') FROM pic_entry WHERE seq=2")" = \
"anthropic/unknown 1" ] || fail "origin or native form"
printf 'tool write new.txt again\ntext done again\n' > "$T/script"
pic submit job-2 'more' > /dev/null
pic run --script "$T/script"
pic check > /dev/null || fail "journal broken after new work"
[ "$(cat "$T/ws/new.txt")" = again ] || fail "new work did not run"
printf 'PASS: a schema-3 journal migrates and keeps working\n'
# A version-8 journal (the last without images) steps up alone, keeps its
# rows, and takes new work.
DB="$T/v8.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v8: setup"
sqlite3 "$DB" "DROP TABLE pic_attachment; DROP TABLE pic_image; UPDATE pic_meta SET value='8' WHERE key='schema_version'"
pic check > /dev/null || fail "v8: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v8: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_entry")" = 4 ] || fail "v8: entries lost"
printf 'text done\n' > "$T/script"
pic submit job-3 'more' > /dev/null
pic run --script "$T/script" > /dev/null
pic check > /dev/null || fail "v8: journal broken after new work"
printf 'PASS: a schema-8 journal migrates and keeps working\n'
# A version-9 journal (images without sizes) is rebuilt: each image's size is
# read from its header and its type sniffed; one that no longer passes is
# pruned, its row kept.
DB="$T/v9.sqlite"
"$PIC" settings imageRetentionDays 0 > /dev/null # the fixture's dates must not age its image out
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v9: setup"
PNG=89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000d49444154789c63f8cfc0f01f00050001ff89993d1d0000000049454e44ae426082
sqlite3 "$DB" "DROP TABLE pic_image; CREATE TABLE pic_image(hash TEXT PRIMARY KEY, mime TEXT NOT NULL, sz INTEGER NOT NULL, data BLOB NOT NULL);
INSERT INTO pic_image VALUES('a', 'image/jpeg', 70, X'$PNG'), ('b', 'image/png', 3, X'000102');
INSERT INTO pic_attachment(entry_seq, ordinal, image) VALUES(1, 0, 'a'), (1, 1, 'b');
UPDATE pic_meta SET value='9' WHERE key='schema_version'"
pic dump > /dev/null || fail "v9: migrated journal does not open"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v9: version not 19"
[ "$(sqlite3 "$DB" "SELECT group_concat(hash || ' ' || mime || ' ' || width || 'x' || height || ' ' || (data IS NULL), ', ') FROM pic_image")" = \
'a image/png 1x1 0, b image/png 0x0 1' ] || fail "v9: images: $(sqlite3 "$DB" 'SELECT * FROM pic_image')"
printf 'PASS: a schema-9 journal gets image sizes and sniffed types\n'
# A version-10 journal (no hooks) with queued messages and images: pic_entry
# is rebuilt for the hook kind with every row, the queue and the images kept.
DB="$T/v10.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v10: setup"
HASH=$(sqlite3 :memory: "SELECT lower(hex(sha3(X'$PNG', 256)))")
sqlite3 "$DB" "INSERT INTO pic_image VALUES('$HASH', 'image/png', 1, 1, 70, X'$PNG');
INSERT INTO pic_attachment(entry_seq, ordinal, image) VALUES(1, 0, '$HASH');
INSERT INTO pic_queued(session_id, submission_id, kind, text, state, created_ms)
SELECT session_id, id, 'follow_up', 'later, please', 'cleared', 0 FROM pic_submission WHERE id = 1;
INSERT INTO pic_attachment(queued_id, ordinal, image) VALUES(last_insert_rowid(), 0, '$HASH');
CREATE TABLE pic_entry_v10(seq INTEGER PRIMARY KEY, submission_id INTEGER NOT NULL,
kind TEXT NOT NULL CHECK(kind IN ('user','assistant','tool_result','answer','instructions','compaction')),
body TEXT NOT NULL, effect_id INTEGER, native TEXT, provider TEXT, api TEXT, model TEXT,
parent_seq INTEGER, created_ms INTEGER, first_kept_seq INTEGER, tokens_before INTEGER);
INSERT INTO pic_entry_v10 SELECT seq, submission_id, kind, body, effect_id, native, provider, api, model,
parent_seq, created_ms, first_kept_seq, tokens_before FROM pic_entry;
DROP TABLE pic_entry; ALTER TABLE pic_entry_v10 RENAME TO pic_entry;
DROP TABLE pic_hook_run;
UPDATE pic_meta SET value='10' WHERE key='schema_version'"
before=$(sqlite3 "$DB" "SELECT group_concat(seq || kind || parent_seq || body, '|') FROM pic_entry")
pic check > /dev/null || fail "v10: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v10: version not 19"
[ "$(sqlite3 "$DB" "SELECT group_concat(seq || kind || parent_seq || body, '|') FROM pic_entry")" = "$before" ] || fail "v10: entries changed"
[ "$(sqlite3 "$DB" "SELECT text || ' ' || state FROM pic_queued")" = 'later, please cleared' ] || fail "v10: queue lost"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_attachment a JOIN pic_image i ON i.hash = a.image")" = 2 ] || fail "v10: images lost"
sqlite3 "$DB" "SELECT sql FROM sqlite_master WHERE name = 'pic_entry'" | grep -q "'hook'" || fail "v10: no hook kind"
sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE name = 'pic_entry_link'" | grep -q 1 || fail "v10: link trigger lost"
pic submit job-10 'after the migration' > /dev/null
pic run --script "$T/script" > /dev/null
pic check > /dev/null || fail "v10: journal broken after new work"
printf 'PASS: a schema-10 journal with queued messages and images migrates to hooks and keeps working\n'
# A version-11 journal (no stopped status, trial labels, request caps or
# compaction reasons) with answered and failed requests: pic_submission is
# rebuilt with every row kept, the new columns NULL, and new work takes them.
DB="$T/v11.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v11: setup"
sqlite3 "$DB" "PRAGMA legacy_alter_table=ON;
INSERT INTO pic_submission(request_key, content, status, error, session_id)
SELECT 'job-broke', 'broke', 'failed', 'provider failed', session_id FROM pic_submission WHERE id = 1;
INSERT INTO pic_entry(submission_id, kind, body) VALUES(last_insert_rowid(), 'user', 'broke');
CREATE TABLE pic_submission_v11(id INTEGER PRIMARY KEY, request_key TEXT NOT NULL UNIQUE, content TEXT NOT NULL,
status TEXT NOT NULL CHECK(status IN ('admitted','answered','failed')), error TEXT, session_id TEXT);
INSERT INTO pic_submission_v11 SELECT id, request_key, content, status, error, session_id FROM pic_submission;
DROP TABLE pic_submission; ALTER TABLE pic_submission_v11 RENAME TO pic_submission;
ALTER TABLE pic_generation DROP COLUMN reason;
ALTER TABLE pic_generation DROP COLUMN idle_seconds;
ALTER TABLE pic_generation DROP COLUMN context_tokens;
UPDATE pic_meta SET value='11' WHERE key='schema_version'"
before=$(sqlite3 "$DB" "SELECT group_concat(id || request_key || status || coalesce(error, '-') || session_id, '|') FROM pic_submission")
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_submission")" = 2 ] || fail "v11: setup rows"
pic check > /dev/null || fail "v11: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v11: version not 19"
[ "$(sqlite3 "$DB" "SELECT group_concat(id || request_key || status || coalesce(error, '-') || session_id, '|') FROM pic_submission")" = "$before" ] ||
fail "v11: requests changed"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_submission WHERE trial IS NULL AND max_requests IS NULL AND stop_reason IS NULL")" = 2 ] ||
fail "v11: new columns not NULL"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_generation WHERE reason IS NULL AND idle_seconds IS NULL AND context_tokens IS NULL")" = \
"$(sqlite3 "$DB" "SELECT count(*) FROM pic_generation")" ] || fail "v11: generations"
sqlite3 "$DB" "SELECT sql FROM sqlite_master WHERE name = 'pic_submission'" | grep -q "'stopped'" || fail "v11: no stopped status"
"$PIC" --db "$DB" --workspace "$T/ws" --trial arm --max-requests 5 submit job-11 'after the migration' > /dev/null
pic run --script "$T/script" > /dev/null
[ "$(sqlite3 "$DB" "SELECT trial || ' ' || max_requests || ' ' || status FROM pic_submission WHERE request_key = 'job-11'")" = 'arm 5 answered' ] ||
fail "v11: new work: $(sqlite3 "$DB" 'SELECT * FROM pic_submission')"
pic check > /dev/null || fail "v11: journal broken after new work"
printf 'PASS: a schema-11 journal migrates to trials, caps and compaction reasons and keeps working\n'
# A version-12 journal (no branch summaries): pic_entry and pic_generation are
# rebuilt with every row kept and the new kind, column and purpose allowed.
DB="$T/v12.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v12: setup"
sqlite3 "$DB" "PRAGMA legacy_alter_table=ON; DROP TRIGGER pic_entry_link;
CREATE TABLE pic_entry_v12(seq INTEGER PRIMARY KEY, submission_id INTEGER NOT NULL,
kind TEXT NOT NULL CHECK(kind IN ('user','assistant','tool_result','answer','instructions','compaction','hook')),
body TEXT NOT NULL, effect_id INTEGER, native TEXT, provider TEXT, api TEXT, model TEXT,
parent_seq INTEGER, created_ms INTEGER, first_kept_seq INTEGER, tokens_before INTEGER);
INSERT INTO pic_entry_v12 SELECT seq, submission_id, kind, body, effect_id, native, provider, api, model,
parent_seq, created_ms, first_kept_seq, tokens_before FROM pic_entry;
DROP TABLE pic_entry; ALTER TABLE pic_entry_v12 RENAME TO pic_entry;
CREATE TABLE pic_generation_v12(id INTEGER PRIMARY KEY, submission_id INTEGER NOT NULL, turn INTEGER NOT NULL,
attempt INTEGER NOT NULL, state TEXT NOT NULL CHECK(state IN ('started','done','failed')),
entry_seq INTEGER, stop_reason TEXT, usage TEXT, request_id TEXT, error TEXT,
purpose TEXT NOT NULL DEFAULT 'reply' CHECK(purpose IN ('reply','compaction')),
reason TEXT, idle_seconds INTEGER, context_tokens INTEGER, UNIQUE(submission_id, purpose, turn, attempt));
INSERT INTO pic_generation_v12 SELECT id, submission_id, turn, attempt, state, entry_seq, stop_reason, usage,
request_id, error, purpose, reason, idle_seconds, context_tokens FROM pic_generation;
DROP TABLE pic_generation; ALTER TABLE pic_generation_v12 RENAME TO pic_generation;
UPDATE pic_meta SET value='12' WHERE key='schema_version'"
before=$(sqlite3 "$DB" "SELECT group_concat(seq || kind || coalesce(parent_seq, '-') || body, '|') FROM pic_entry")
gens=$(sqlite3 "$DB" "SELECT group_concat(id || state || purpose || coalesce(entry_seq, '-'), '|') FROM pic_generation")
pic check > /dev/null || fail "v12: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v12: version not 19"
[ "$(sqlite3 "$DB" "SELECT group_concat(seq || kind || coalesce(parent_seq, '-') || body, '|') FROM pic_entry")" = "$before" ] ||
fail "v12: entries changed"
[ "$(sqlite3 "$DB" "SELECT group_concat(id || state || purpose || coalesce(entry_seq, '-'), '|') FROM pic_generation")" = "$gens" ] ||
fail "v12: generations changed"
sqlite3 "$DB" "SELECT sql FROM sqlite_master WHERE name = 'pic_entry'" | grep -q "'branch_summary'" || fail "v12: no branch_summary kind"
sqlite3 "$DB" "SELECT sql FROM sqlite_master WHERE name = 'pic_generation'" | grep -q "'branch_summary'" ||
fail "v12: no branch_summary purpose"
sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE name = 'pic_entry_link'" | grep -q 1 || fail "v12: link trigger lost"
sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE name = 'pic_entry_effect'" | grep -q 1 || fail "v12: pic_entry rebuilt without its index"
pic submit job-12 'after the migration' > /dev/null
pic run --script "$T/script" > /dev/null
pic check > /dev/null || fail "v12: journal broken after new work"
printf 'PASS: a schema-12 journal migrates to branch summaries and keeps working\n'
# A version-13 journal gains the index of result entries by effect and the
# model and thinking level each request is admitted with; a version-14 one
# (with the index, or without it) gains what it lacks. Old requests have no
# model (whatever runs them).
for from in 13 14 14-no-index; do
DB="$T/v$from.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v$from: setup"
case $from in
13) undo="DROP INDEX pic_entry_effect;" v=13 ;;
14) undo="" v=14 ;;
*) undo="DROP INDEX pic_entry_effect;" v=14 ;;
esac
sqlite3 "$DB" "$undo ALTER TABLE pic_submission DROP COLUMN model; ALTER TABLE pic_submission DROP COLUMN thinking;
UPDATE pic_meta SET value='$v' WHERE key='schema_version'"
pic check > /dev/null || fail "v$from: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v$from: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_submission WHERE model IS NULL AND thinking IS NULL")" = 1 ] ||
fail "v$from: old requests given a model"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE type = 'index' AND name = 'pic_entry_effect'")" = 1 ] ||
fail "v$from: no pic_entry_effect index"
done
printf 'PASS: schema-13 and schema-14 journals migrate to admitted models and the effect index\n'
# A version-13 journal (no index of tool results by call): migrated to 19
# with pic_entry_effect, which settling a call and pic check search by.
DB="$T/v13-index.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v13: setup"
sqlite3 "$DB" "DROP INDEX pic_entry_effect; UPDATE pic_meta SET value='13' WHERE key='schema_version'"
pic check > /dev/null || fail "v13: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v13: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE type = 'index' AND name = 'pic_entry_effect' AND tbl_name = 'pic_entry'")" = 1 ] ||
fail "v13: no pic_entry_effect index"
sqlite3 "$DB" "EXPLAIN QUERY PLAN SELECT 1 FROM pic_entry WHERE effect_id = 1 AND kind = 'tool_result'" | grep -q pic_entry_effect ||
fail "v13: results by call not searched by the index"
pic submit job-13 'after the migration' > /dev/null
pic run --script "$T/script" > /dev/null
pic check > /dev/null || fail "v13: journal broken after new work"
printf 'PASS: a schema-13 journal gets the index of tool results by call\n'
# A version-15 journal (no file history): migrated to 19 with its tables,
# empty; a write afterwards records the file as it was.
DB="$T/v15.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v15: setup"
sqlite3 "$DB" "DROP TABLE pic_file_change; DROP TABLE pic_blob; DROP TABLE pic_restore;
UPDATE pic_meta SET value='15' WHERE key='schema_version'"
pic check > /dev/null || fail "v15: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v15: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name IN ('pic_file_change','pic_blob','pic_restore')")" = 3 ] ||
fail "v15: file history tables missing"
pic submit job-15 'after the migration' > /dev/null
printf 'tool write v15.txt hello\ntext done\n' > "$T/script15"
pic run --script "$T/script15" > /dev/null
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_file_change")" -ge 1 ] || fail "v15: a write after the migration recorded nothing"
pic check > /dev/null || fail "v15: journal broken after new work"
printf 'PASS: a schema-15 journal gets the file history tables\n'
# A version-16 journal (no inbox): migrated to 19 with pic_inbox, empty;
# pic send then takes a message for it.
DB="$T/v16.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v16: setup"
sqlite3 "$DB" "DROP TABLE pic_inbox; UPDATE pic_meta SET value='16' WHERE key='schema_version'"
if pic send steer 'too soon' 2> "$T/v16.err"; then fail "v16: pic send wrote to a journal of another schema"; fi
pic check > /dev/null || fail "v16: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v16: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_inbox")" = 0 ] || fail "v16: inbox not empty"
pic send follow_up 'now' > /dev/null || fail "v16: pic send after the migration"
pic check > /dev/null || fail "v16: journal broken after a send"
printf 'PASS: a schema-16 journal gets the inbox\n'
# A version-17 journal (no credential per request): migrated to 19 with the
# column, its old rows NULL; a request after it records the key it used.
DB="$T/v17.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v17: setup"
sqlite3 "$DB" "CREATE TABLE g17 AS SELECT id, submission_id, turn, attempt, state, entry_seq, stop_reason, usage, request_id,
error, purpose, reason, idle_seconds, context_tokens FROM pic_generation; DROP TABLE pic_generation;
CREATE TABLE pic_generation(id INTEGER PRIMARY KEY, submission_id INTEGER NOT NULL, turn INTEGER NOT NULL,
attempt INTEGER NOT NULL, state TEXT NOT NULL CHECK(state IN ('started','done','failed')), entry_seq INTEGER,
stop_reason TEXT, usage TEXT, request_id TEXT, error TEXT,
purpose TEXT NOT NULL DEFAULT 'reply' CHECK(purpose IN ('reply','compaction','branch_summary')), reason TEXT,
idle_seconds INTEGER, context_tokens INTEGER, UNIQUE(submission_id, purpose, turn, attempt));
INSERT INTO pic_generation SELECT * FROM g17; DROP TABLE g17;
UPDATE pic_meta SET value='17' WHERE key='schema_version'"
before=$(sqlite3 "$DB" "SELECT count(*) FROM pic_generation")
pic check > /dev/null || fail "v17: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v17: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_generation WHERE credential IS NULL")" = "$before" ] || fail "v17: old rows changed"
pic check > /dev/null || fail "v17: journal broken"
printf 'PASS: a schema-17 journal gets the credential column\n'
# A version-18 journal (no verdicts): migrated to 19 with pic_verdict, empty.
DB="$T/v18.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
pic check > /dev/null || fail "v18: setup"
sqlite3 "$DB" "DROP TABLE pic_verdict; UPDATE pic_meta SET value='18' WHERE key='schema_version'"
pic check > /dev/null || fail "v18: migrated journal fails pic check"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 19 ] || fail "v18: version not 19"
[ "$(sqlite3 "$DB" "SELECT count(*) FROM pic_verdict")" = 0 ] || fail "v18: verdicts not empty"
printf 'PASS: a schema-18 journal gets the verdict table\n'
# A migration needs every other pic out of the journal (each holds the journal
# lock shared while it has it open): refused while one is there, done once it
# has gone.
DB="$T/held.sqlite"
sqlite3 "$DB" < "$(dirname "$0")/journal-v3.sql"
perl -e 'use Fcntl qw(:flock); open(my $f, ">>", $ARGV[0]) or die; flock($f, LOCK_SH) or die; open(my $g, ">", $ARGV[1]); close $g; sleep 1 while !-e $ARGV[2]' \
"$DB.pic-lock" "$T/held" "$T/release" &
other=$!
i=0
while [ ! -e "$T/held" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "held: the other holder did not start"; sleep 0.1; done
if pic check > /dev/null 2> "$T/held.err"; then touch "$T/release"; fail "held: migrated under another pic"; fi
touch "$T/release"
wait "$other"
grep -q 'needs migrating to schema .* and another pic process has it open' "$T/held.err" || fail "held: $(cat "$T/held.err")"
[ "$(sqlite3 "$DB" "SELECT value FROM pic_meta WHERE key='schema_version'")" = 3 ] || fail "held: the journal was changed"
pic check > /dev/null || fail "held: not migrated once the other pic had gone"
printf 'PASS: a journal is not migrated under another pic, and is once it has gone\n'