Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 74 additions & 17 deletions apple/scripts/probes/check-history-downgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# it was not running. Both apps run in one storage sandbox, one at a time. The edits between runs are written straight
# into the stores, in the formats the previous stable writes, since its UI cannot be scripted.
#
# The previous stable never opens the core's `saved_queries.db` or `history/history.db` (core ADR 0069), so after this
# build runs, the probe checks the stores the previous stable does read.
#
# Usage: check-history-downgrade.sh <this build's TablePro.app> <previous stable's TablePro.app>
set -euo pipefail

Expand All @@ -20,7 +23,8 @@ mkdir -p "$SUPPORT"
touch "$SUPPORT/.defaults-suite"
FAVORITES="$SUPPORT/sql_favorites.db"
HISTORY="$SUPPORT/query_history.db"
CORE="$SUPPORT/Core/state/history.db"
SAVED="$SUPPORT/Core/state/saved_queries.db"
CORE_HISTORY="$SUPPORT/Core/state/history/history.db"

# PluginHostStorage.suiteName(forSandboxAt:): FNV-1a over the sandbox path.
SUITE="$(python3 -I -c '
Expand All @@ -43,9 +47,36 @@ fail() {
exit 1
}

core() {
[ -f "$CORE" ] || return 0
sqlite3 -readonly "$CORE" "$1" 2>/dev/null | paste -sd'|' -
# Empty until the file exists, so a wait can poll before this build made it.
query() {
[ -f "$1" ] || return 0
sqlite3 -readonly "$1" "$2" 2>/dev/null | paste -sd'|' -
}
saved() { query "$SAVED" "$1"; }
core_history() { query "$CORE_HISTORY" "$1"; }
favorites() { query "$FAVORITES" "$1"; }
history() { query "$HISTORY" "$1"; }

# Whether a defaults key holds JSON data, as the previous stable decodes it, with every seeded record and field.
# Fields this build adds are allowed: the previous stable's decoder skips them.
stored() {
defaults export "$SUITE" - 2>/dev/null | python3 -I -c '
import json, plistlib, sys
data = plistlib.loads(sys.stdin.buffer.read()).get(sys.argv[1])
expected = json.loads(bytes.fromhex(sys.argv[2]))
def holds(actual, wanted):
if isinstance(wanted, dict):
return isinstance(actual, dict) and all(k in actual and holds(actual[k], v) for k, v in wanted.items())
if isinstance(wanted, list):
return isinstance(actual, list) and len(actual) == len(wanted) and all(map(holds, actual, wanted))
# The previous stable decodes a UUID in either case, and the core keeps ids in lower case.
if isinstance(wanted, str) and isinstance(actual, str) and len(wanted) == 36 and wanted.count("-") == 4:
return actual.upper() == wanted.upper()
return actual == wanted
if not isinstance(data, bytes) or not holds(json.loads(data), expected):
print(data)
sys.exit(1)
' "$1" "$2"
}

# Launches the app until `check` passes or 90 seconds go by, then stops it.
Expand All @@ -69,6 +100,7 @@ OLDER=77777777-7777-4777-8777-777777777777
NEWER=88888888-8888-4888-8888-888888888888
FIRST_RUN=99999999-9999-4999-8999-999999999999
SECOND_RUN=AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA
THIRD_RUN=CCCCCCCC-CCCC-4CCC-8CCC-CCCCCCCCCCCC
TABLE_FOLDER=BBBBBBBB-BBBB-4BBB-8BBB-BBBBBBBBBBBB
# Within the default 90-day history window, so the launch's retention pass keeps the runs.
NOW="$(date +%s)"
Expand Down Expand Up @@ -134,29 +166,54 @@ PY
defaults write "$SUITE" com.TablePro.favoriteTables -data "$(defaults_json tables)"
defaults write "$SUITE" com.TablePro.favoriteDatabases -data "$(defaults_json databases)"
defaults write "$SUITE" "com.TablePro.tableFolders.$CONNECTION" -data "$(defaults_json folders)"
# The schema versions the previous stable stamped; a newer one would be a format it cannot read.
FAVORITES_VERSION="$(favorites "PRAGMA user_version")"
HISTORY_VERSION="$(history "PRAGMA user_version")"

# Everything the previous stable reads, as this build must leave it: the settled name is this build's own write.
previous_stable_reads() {
local when="$1"
[ "$(favorites "PRAGMA user_version")" = "$FAVORITES_VERSION" ] || fail "$when: sql_favorites.db is at another schema version"
[ "$(history "PRAGMA user_version")" = "$HISTORY_VERSION" ] || fail "$when: query_history.db is at another schema version"
[ "$(favorites "PRAGMA quick_check")" = ok ] || fail "$when: sql_favorites.db is damaged"
[ "$(history "PRAGMA quick_check")" = ok ] || fail "$when: query_history.db is damaged"
local rows
rows="$(favorites "SELECT upper(id), name, query, ifnull(keyword, ''), ifnull(upper(folder_id), ''), upper(connection_id), sort_order, printf('%.3f', created_at) FROM favorites ORDER BY created_at")"
[ "$rows" = "$OLDER|Report|SELECT 1|rep|$FOLDER|$CONNECTION|0|1700000001.250|$NEWER|Report (2)|SELECT 2|||$CONNECTION|1|1700000002.750" ] || fail "$when: sql_favorites.db holds $rows"
rows="$(favorites "SELECT upper(id), name, ifnull(parent_id, ''), upper(connection_id), sort_order, printf('%.3f', created_at) FROM folders")"
[ "$rows" = "$FOLDER|Reports||$CONNECTION|0|1700000000.500" ] || fail "$when: sql_favorites.db holds the folders $rows"
[ "$(favorites "SELECT count(*) FROM favorites_fts WHERE favorites_fts MATCH 'Report'")" = 2 ] || fail "$when: the saved queries' search index lost a row"
rows="$(history "SELECT upper(id), query, upper(connection_id), database_name, database_type, printf('%.3f', executed_at), printf('%.3f', execution_time), row_count, was_successful, ifnull(error_message, '') FROM history ORDER BY executed_at")"
[ "$rows" = "$FIRST_RUN|SELECT 1|$CONNECTION|shop|PostgreSQL|$((NOW - 300)).000|0.500|1|1||$SECOND_RUN|SELECT broken|$CONNECTION|shop|PostgreSQL|$((NOW - 200)).000|0.100|0|0|syntax error" ] || fail "$when: query_history.db holds $rows"
stored com.TablePro.favoriteTables "$(defaults_json tables)" >/dev/null || fail "$when: the favourite tables key holds $(stored com.TablePro.favoriteTables "$(defaults_json tables)")"
stored com.TablePro.favoriteDatabases "$(defaults_json databases)" >/dev/null || fail "$when: the favourite databases key holds $(stored com.TablePro.favoriteDatabases "$(defaults_json databases)")"
stored "com.TablePro.tableFolders.$CONNECTION" "$(defaults_json folders)" >/dev/null || fail "$when: the table folders key holds $(stored "com.TablePro.tableFolders.$CONNECTION" "$(defaults_json folders)")"
if find "$SUPPORT" -maxdepth 1 -name '*.damaged-*' | grep -q .; then
fail "$when: a store was set aside as damaged"
fi
}

MOVED='[ "$(core "SELECT count(*) FROM saved_queries")" = 2 ] && [ "$(core "SELECT count(*) FROM history")" = 2 ] && [ "$(core "SELECT count(*) FROM table_folder_items")" = 1 ] && [ "$(core "SELECT count(*) FROM favourite_databases")" = 1 ]'
MOVED='[ "$(saved "SELECT count(*) FROM saved_queries")" = 2 ] && [ "$(core_history "SELECT count(*) FROM history")" = 2 ] && [ "$(saved "SELECT count(*) FROM table_folder_items")" = 1 ] && [ "$(saved "SELECT count(*) FROM favourite_databases")" = 1 ] && [ "$(favorites "SELECT name FROM favorites WHERE upper(id) = '"'"'$NEWER'"'"'")" = "Report (2)" ]'
run "$NEW_APP" "$MOVED"
eval "$MOVED" || fail "this build did not move the stores into the core"
[ "$(core "SELECT upper(record_id) FROM saved_queries ORDER BY created_at")" = "$OLDER|$NEWER" ] || fail "a saved query lost its id"
[ "$(core "SELECT name FROM saved_queries ORDER BY created_at")" = "Report|Report (2)" ] || fail "the core did not settle the two names"
[ "$(core "SELECT upper(folder_record_id) FROM saved_queries WHERE upper(record_id) = '$OLDER'")" = "$FOLDER" ] || fail "a saved query left its folder"
[ "$(core "SELECT upper(record_id) FROM history ORDER BY executed_at")" = "$FIRST_RUN|$SECOND_RUN" ] || fail "a run lost its id"
[ "$(core "SELECT upper(id) FROM table_folders")" = "$TABLE_FOLDER" ] || fail "the table folder lost its id"
[ "$(core "SELECT name FROM favourite_tables")" = "orders" ] || fail "the favourite table did not move in"
[ "$(sqlite3 "$FAVORITES" "SELECT name FROM favorites WHERE id = '$NEWER'")" = "Report (2)" ] || fail "the settled name did not reach the store the previous stable reads"
eval "$MOVED" || fail "this build did not move the stores into the core and write the settled name back"
[ "$(saved "SELECT upper(record_id) FROM saved_queries ORDER BY created_at")" = "$OLDER|$NEWER" ] || fail "a saved query lost its id"
[ "$(saved "SELECT name FROM saved_queries ORDER BY created_at")" = "Report|Report (2)" ] || fail "the core did not settle the two names"
[ "$(saved "SELECT upper(folder_record_id) FROM saved_queries WHERE upper(record_id) = '$OLDER'")" = "$FOLDER" ] || fail "a saved query left its folder"
[ "$(core_history "SELECT upper(record_id) FROM history ORDER BY executed_at")" = "$FIRST_RUN|$SECOND_RUN" ] || fail "a run lost its id"
[ "$(saved "SELECT upper(id) FROM table_folders")" = "$TABLE_FOLDER" ] || fail "the table folder lost its id"
[ "$(saved "SELECT name FROM favourite_tables")" = "orders" ] || fail "the favourite table did not move in"
previous_stable_reads "after this build ran"

run "$OLD_APP" 'false'
[ "$(sqlite3 "$FAVORITES" "SELECT name FROM favorites ORDER BY created_at" | paste -sd'|' -)" = "Report|Report (2)" ] || fail "the previous stable lost what this build wrote"
[ "$(sqlite3 "$HISTORY" "SELECT count(*) FROM history")" = 2 ] || fail "the previous stable lost the history"
previous_stable_reads "after the previous stable ran"

# The previous stable's own edits: a renamed saved query, a deleted run, a cleared favourite table and a new run.
sqlite3 "$FAVORITES" "UPDATE favorites SET name = 'Report, edited on the previous stable', updated_at = 1700000300 WHERE id = '$OLDER';"
sqlite3 "$HISTORY" "DELETE FROM history WHERE id = '$FIRST_RUN';"
sqlite3 "$HISTORY" "INSERT INTO history (id, query, connection_id, database_name, database_type, executed_at, execution_time, row_count, was_successful) VALUES ('CCCCCCCC-CCCC-4CCC-8CCC-CCCCCCCCCCCC', 'SELECT 3', '$CONNECTION', 'shop', 'PostgreSQL', $((NOW - 100)), 0.2, 1, 1);"
sqlite3 "$HISTORY" "INSERT INTO history (id, query, connection_id, database_name, database_type, executed_at, execution_time, row_count, was_successful) VALUES ('$THIRD_RUN', 'SELECT 3', '$CONNECTION', 'shop', 'PostgreSQL', $((NOW - 100)), 0.2, 1, 1);"
defaults write "$SUITE" com.TablePro.favoriteTables -data "$(defaults_json none)"

TAKEN='[ "$(core "SELECT name FROM saved_queries WHERE upper(record_id) = '"'"'$OLDER'"'"'")" = "Report, edited on the previous stable" ] && [ "$(core "SELECT upper(record_id) FROM history ORDER BY executed_at")" = "$SECOND_RUN|CCCCCCCC-CCCC-4CCC-8CCC-CCCCCCCCCCCC" ] && [ "$(core "SELECT count(*) FROM favourite_tables")" = 0 ]'
TAKEN='[ "$(saved "SELECT name FROM saved_queries WHERE upper(record_id) = '"'"'$OLDER'"'"'")" = "Report, edited on the previous stable" ] && [ "$(core_history "SELECT upper(record_id) FROM history ORDER BY executed_at")" = "$SECOND_RUN|$THIRD_RUN" ] && [ "$(saved "SELECT count(*) FROM favourite_tables")" = 0 ]'
run "$NEW_APP" "$TAKEN"
eval "$TAKEN" || fail "this build did not take in the previous stable's edits"

Expand Down
Loading