Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

feat(postgres-checkpointer): add exceptions + alembic helpers (D1.2)

xf gong8d8757218a57
diff --git a/cubepi/checkpointer/postgres/alembic_helpers.py b/cubepi/checkpointer/postgres/alembic_helpers.py
new file mode 100644
index 0000000..ad5c818
--- /dev/null
+++ b/cubepi/checkpointer/postgres/alembic_helpers.py
@@ -0,0 +1,32 @@
+"""SQL helpers for host application alembic migrations."""
+
+from cubepi.checkpointer.postgres.models import (
+ EXPECTED_SCHEMA_VERSION,
+ PARTITION_COUNT,
+)
+
+
+def create_message_partitions_op() -> str:
+ """Return SQL DDL creating all 64 child partitions of cubepi_messages.
+
+ Call inside an alembic upgrade() via op.execute(), AFTER the parent
+ cubepi_messages table has been created.
+ """
+ return "\n".join(
+ f"CREATE TABLE cubepi_messages_p{i:02d} "
+ f"PARTITION OF cubepi_messages "
+ f"FOR VALUES WITH (modulus {PARTITION_COUNT}, remainder {i});"
+ for i in range(PARTITION_COUNT)
+ )
+
+
+def write_schema_version_op() -> str:
+ """Return SQL inserting the current schema version.
+
+ Call inside alembic upgrade() after CREATE TABLE cubepi_schema_version.
+ Idempotent via ON CONFLICT DO NOTHING.
+ """
+ return (
+ f"INSERT INTO cubepi_schema_version (version) "
+ f"VALUES ({EXPECTED_SCHEMA_VERSION}) ON CONFLICT DO NOTHING;"
+ )
diff --git a/cubepi/checkpointer/postgres/exceptions.py b/cubepi/checkpointer/postgres/exceptions.py
new file mode 100644
index 0000000..5a59cf2
--- /dev/null
+++ b/cubepi/checkpointer/postgres/exceptions.py
@@ -0,0 +1,29 @@
+"""Exceptions raised by PostgresCheckpointer schema verification."""
+
+
+class CubepiSchemaError(Exception):
+ """Base class for cubepi Postgres schema errors."""
+
+
+class CubepiSchemaUninitialized(CubepiSchemaError):
+ """The cubepi_schema_version table is empty or missing.
+
+ Typically means the host application's alembic upgrade hasn't been
+ run yet against this database.
+ """
+
+
+class CubepiSchemaMismatch(CubepiSchemaError):
+ """The DB schema version doesn't match cubepi's expected version.
+
+ Typically means the cubepi library was upgraded but the host
+ application's alembic is behind. Run a new alembic revision.
+ """
+
+ def __init__(self, *, expected: int, actual: int, hint: str = "") -> None:
+ msg = f"cubepi schema mismatch: expected={expected} actual={actual}."
+ if hint:
+ msg += f" {hint}"
+ super().__init__(msg)
+ self.expected = expected
+ self.actual = actual
diff --git a/tests/checkpointer/test_postgres.py b/tests/checkpointer/test_postgres.py
index 75a646c..116ce95 100644
--- a/tests/checkpointer/test_postgres.py
+++ b/tests/checkpointer/test_postgres.py
@@ -38,3 +38,54 @@ def test_cubepi_message_has_gin_index() -> None:
msgs = cubepi_metadata.tables["cubepi_messages"]
idx_names = [i.name for i in msgs.indexes]
assert "ix_cubepi_messages_metadata_gin" in idx_names
+
+
+def test_create_message_partitions_op_yields_64_statements() -> None:
+ from cubepi.checkpointer.postgres.alembic_helpers import create_message_partitions_op
+ sql = create_message_partitions_op()
+ assert sql.count("CREATE TABLE cubepi_messages_p") == 64
+ assert "modulus 64, remainder 0" in sql
+ assert "modulus 64, remainder 63" in sql
+
+
+def test_create_message_partitions_op_partitions_are_zero_padded() -> None:
+ from cubepi.checkpointer.postgres.alembic_helpers import create_message_partitions_op
+ sql = create_message_partitions_op()
+ # Partition names use 2-digit padding so they sort lexicographically
+ assert "cubepi_messages_p00 " in sql
+ assert "cubepi_messages_p63 " in sql
+
+
+def test_write_schema_version_op_includes_expected_version() -> None:
+ from cubepi.checkpointer.postgres.alembic_helpers import write_schema_version_op
+ sql = write_schema_version_op()
+ assert "INSERT INTO cubepi_schema_version" in sql
+ assert "VALUES (1)" in sql
+ assert "ON CONFLICT" in sql
+
+
+def test_schema_uninitialized_is_schema_error() -> None:
+ from cubepi.checkpointer.postgres.exceptions import (
+ CubepiSchemaError,
+ CubepiSchemaUninitialized,
+ )
+ err = CubepiSchemaUninitialized("tables missing")
+ assert isinstance(err, CubepiSchemaError)
+
+
+def test_schema_mismatch_carries_expected_actual() -> None:
+ from cubepi.checkpointer.postgres.exceptions import CubepiSchemaMismatch
+ err = CubepiSchemaMismatch(expected=2, actual=1, hint="run alembic")
+ assert err.expected == 2
+ assert err.actual == 1
+ assert "expected=2" in str(err)
+ assert "actual=1" in str(err)
+ assert "run alembic" in str(err)
+
+
+def test_schema_mismatch_without_hint() -> None:
+ from cubepi.checkpointer.postgres.exceptions import CubepiSchemaMismatch
+ err = CubepiSchemaMismatch(expected=2, actual=1)
+ # No hint suffix
+ assert "expected=2" in str(err)
+ assert "actual=1" in str(err)