AtOM/lib/database/checkVersion
2026-03-13 05:15:02 +01:00

54 lines
1.8 KiB
Bash

#!/usr/bin/env bash
# Copyright © 2012-2026 ScriptFanix
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# A copy of the GNU General Public License v3 is includded in the LICENSE file
# at the root of the project.
# Current schema version this AtOM binary understands
currentdbversion=8
checkDatabaseVersion() {
local dbversion
# Try to read the stored version from the 'atom' metadata table
if dbversion=$(Select atom version <<<"\"1\" = 1")
then
if (( dbversion == currentdbversion ))
then
return 0 # Already up to date
elif (( dbversion < currentdbversion ))
then
# Run sequential upgrade functions until we reach
# `$currentdbversion`
until (( dbversion == currentdbversion ))
do
# Dynamically calls e.g. upgradedatabase_3_4
upgradedatabase_${dbversion}_$((dbversion+1))
# After each upgrade, re-read the version from
# the database to ensure it was updated
# correctly
dbversion=$(Select atom version <<<"\"1\" = 1")
done
else
# DB was created by a newer AtOM; we can't run and
# ensure consistency
echo "Database schema version $dbversion is" \
"higher thanthat of this version of" \
"AtOM ($currentdbversion). Bailing out." >&2
exit $EDBVERSION
fi
else
# No version row found: this is a database from very early
# drafts
# This is stupid but nobody is running with DB schema v0 anyway
Insert atom 1 <<<"version $currentdbversion"
fi
}