Check there's room for the database's copy before an update

Before dumping the app's database for a rollback, coreapps reads its size
and refuses the update, changing nothing, if the apps' disk can't hold
the copy and keep 1 GB free; big databases say the copy takes a while.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 19:40:03 -07:00
co-authored by Claude Opus 5.5
parent 2d29ca62b3
commit f56641bbe1
4 changed files with 66 additions and 1 deletions
+42
View File
@@ -967,6 +967,31 @@ RESTORE = {
}
# The database's size, to check there's room for its copy first
DB_SIZE = {
"postgres": ["docker", "exec", "postgresql", "psql", "-U", "postgres", "-Atc",
"SELECT pg_database_size('{db}')"],
"mysql": ["docker", "exec", "pdnsmysql", "sh", "-c",
'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb -uroot -N -e "SELECT '
"COALESCE(SUM(data_length + index_length), 0) FROM information_schema.tables "
"WHERE table_schema = '{db}'\""],
}
# Left free on the apps' disk after the database's copy, for the Core
ROLLBACK_RESERVE = 1024 ** 3
# Above this, say that keeping the copy takes a while
BIG_DATABASE = 200 * 1024 ** 2
def size_text(n):
return f"{n / 1024 ** 3:.1f} GB" if n >= 1024 ** 3 else f"{n / 1024 ** 2:.0f} MB"
def database_size(need, db):
out = run([c.replace("{db}", db) for c in DB_SIZE[need]])
text = out.stdout.strip()
return int(text) if out.returncode == 0 and text.isdigit() else None
def keep_for_rollback(m, p, backup, log):
"""Copies of the app's service folder, settings, database and (where the
filesystem makes it cheap) /data, in backup. Refuses the update if the
@@ -980,12 +1005,29 @@ def keep_for_rollback(m, p, backup, log):
for need in marker_needs(name):
if need in DUMP:
file, cmd = DUMP[need]
# The copy is about the database's size at most (pg_dump -Fc
# compresses; mariadb-dump's SQL is close to it)
size = database_size(need, m["dbname"])
free = shutil.disk_usage(backup).free
if size is not None and size + ROLLBACK_RESERVE > free:
raise Refused(
f"Updating {name} keeps a copy of its database ({size_text(size)}) until the "
f"update has worked, to roll back to, and the apps' disk has "
f"{size_text(free)} free (it keeps {size_text(ROLLBACK_RESERVE)} for the "
"Core). Nothing was changed. Free some space first.")
if size is not None and size > BIG_DATABASE:
say(f"Keeping a copy of its database ({size_text(size)}) in case the update "
"fails; this takes a while...")
started = time.monotonic()
with open(os.path.join(backup, file), "wb") as out, open(log, "a") as err:
result = subprocess.run([c.replace("{db}", m["dbname"]) for c in cmd],
stdout=out, stderr=err)
if result.returncode != 0:
raise Refused(f"Couldn't back up {name}'s database before updating, "
f"so nothing was changed (log on the Core: {log}).")
if time.monotonic() - started > 30:
say(f"Kept it ({size_text(os.path.getsize(os.path.join(backup, file)))}, "
f"{time.monotonic() - started:.0f} seconds).")
# A reflink copy shares the files' blocks until one side changes (btrfs,
# XFS); a full copy of /data could be too big, so there's none
data = os.path.join(APPS, name, "data")