Files
FabledGrimoire/src/grimoire/services/instance.py
T
bvandeusenandClaude Opus 5.5 6d8db0bb65
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 1m37s
CI & Build / integration (push) Successful in 2m7s
CI & Build / Build & push image (push) Successful in 22s
feat(6845): every instance has an id lineage, and every full backup carries it
instance_identity is one row made by migration 0127 with a random id, so an
instance has it before its first account. A full backup carries it in its
header as instance_id (the tar manifest header and the JSON top level);
BACKUP_VERSION is 28. The table rides in _NOT_INCLUDED: a restore decides from
the header value rather than copying a row.

Two stores with the same id number their rows the same way. That is what lets
a later step roll a live instance back by original id, refuse a backup whose
ids would land on unrelated rows, and have a fresh-instance restore adopt the
backup's id (adopt_instance_id).

Milestone 582, step 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 19:53:45 -05:00

47 lines
1.7 KiB
Python

"""This instance's id lineage (models/instance.py says what it means)."""
from __future__ import annotations
import uuid
from sqlalchemy import select
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.ext.asyncio import AsyncSession
from grimoire.models import async_session
from grimoire.models.instance import InstanceIdentity
async def _read_or_make(session: AsyncSession) -> str:
current = (await session.execute(select(InstanceIdentity.instance_id))).scalar_one_or_none()
if current:
return current
# The migration makes the row; this covers a schema built some other way.
await session.execute(
insert(InstanceIdentity)
.values(id=1, instance_id=str(uuid.uuid4()))
.on_conflict_do_nothing(index_elements=["id"])
)
return (await session.execute(select(InstanceIdentity.instance_id))).scalar_one()
async def get_instance_id(session: AsyncSession | None = None) -> str:
"""This instance's id. Inside `session` when given, so a caller's
transaction sees the same value it may be about to change."""
if session is not None:
return await _read_or_make(session)
async with async_session() as own:
value = await _read_or_make(own)
await own.commit()
return value
async def adopt_instance_id(session: AsyncSession, value: str) -> None:
"""Take a backup's id as this instance's own, inside the caller's
transaction: after a restore that kept the backup's original ids, this
store and the one the backup came from number their rows the same way."""
await session.execute(
insert(InstanceIdentity)
.values(id=1, instance_id=value)
.on_conflict_do_update(index_elements=["id"], set_={"instance_id": value})
)