Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Add the strict accepted-change partition head to projects.

Revision ID: s2p3e4c5w6k7
Revises: bcdbd5a942ca
Create Date: 2026-08-29 20:15:00.000000

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "s2p3e4c5w6k7"
down_revision: Union[str, None] = "bcdbd5a942ca"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Add the project partition head and its durable accepted evidence."""
with op.batch_alter_table("project", schema=None) as batch_op:
batch_op.add_column(
sa.Column(
"partition_position",
sa.Integer(),
server_default=sa.text("0"),
nullable=False,
)
)

op.create_table(
"accepted_project_note_change",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("project_id", sa.Integer(), nullable=False),
sa.Column("project_external_id", sa.String(), nullable=False),
sa.Column("partition_position", sa.Integer(), nullable=False),
sa.Column("entity_id", sa.Integer(), nullable=False),
sa.Column("note_external_id", sa.String(), nullable=False),
sa.Column("title", sa.Text(), nullable=False),
sa.Column("operation", sa.String(), nullable=False),
sa.Column("file_path", sa.Text(), nullable=False),
sa.Column("previous_file_path", sa.Text(), nullable=True),
sa.Column("accepted_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("source", sa.String(), nullable=False),
sa.Column("db_version", sa.Integer(), nullable=True),
sa.Column("db_checksum", sa.String(), nullable=True),
sa.Column("actor_user_profile_id", sa.String(), nullable=True),
sa.Column("actor_kind", sa.String(), nullable=True),
sa.Column("actor_name", sa.String(), nullable=True),
sa.Column("materialized_at", sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"project_id",
"partition_position",
name="uq_accepted_project_note_change_project_position",
),
)
op.create_index(
"ix_accepted_project_note_change_project_materialized",
"accepted_project_note_change",
["project_id", "materialized_at"],
unique=False,
)
op.create_index(
"ix_accepted_project_note_change_note_external_id",
"accepted_project_note_change",
["note_external_id"],
unique=False,
)


def downgrade() -> None:
"""Remove accepted evidence and the project partition head."""
op.drop_index(
"ix_accepted_project_note_change_note_external_id",
table_name="accepted_project_note_change",
)
op.drop_index(
"ix_accepted_project_note_change_project_materialized",
table_name="accepted_project_note_change",
)
op.drop_table("accepted_project_note_change")
with op.batch_alter_table("project", schema=None) as batch_op:
batch_op.drop_column("partition_position")
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""Reconcile accepted project change storage for pre-release tenants.

Revision ID: t3q4r5s6x7y8
Revises: s2p3e4c5w6k7
Create Date: 2026-08-30 02:15:00.000000

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect


revision: str = "t3q4r5s6x7y8"
down_revision: Union[str, None] = "s2p3e4c5w6k7"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def _create_accepted_project_note_change() -> None:
op.create_table(
"accepted_project_note_change",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("project_id", sa.Integer(), nullable=False),
sa.Column("project_external_id", sa.String(), nullable=False),
sa.Column("partition_position", sa.Integer(), nullable=False),
sa.Column("entity_id", sa.Integer(), nullable=False),
sa.Column("note_external_id", sa.String(), nullable=False),
sa.Column("title", sa.Text(), nullable=False),
sa.Column("operation", sa.String(), nullable=False),
sa.Column("file_path", sa.Text(), nullable=False),
sa.Column("previous_file_path", sa.Text(), nullable=True),
sa.Column("accepted_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("source", sa.String(), nullable=False),
sa.Column("db_version", sa.Integer(), nullable=True),
sa.Column("db_checksum", sa.String(), nullable=True),
sa.Column("actor_user_profile_id", sa.String(), nullable=True),
sa.Column("actor_kind", sa.String(), nullable=True),
sa.Column("actor_name", sa.String(), nullable=True),
sa.Column("materialized_at", sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(["project_id"], ["project.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint(
"project_id",
"partition_position",
name="uq_accepted_project_note_change_project_position",
),
)


def _create_missing_indexes(existing_indexes: set[str]) -> None:
indexes = {
"ix_accepted_project_note_change_project_materialized": [
"project_id",
"materialized_at",
],
"ix_accepted_project_note_change_note_external_id": ["note_external_id"],
}
for index_name, columns in indexes.items():
if index_name not in existing_indexes:
op.create_index(
index_name,
"accepted_project_note_change",
columns,
unique=False,
)


def upgrade() -> None:
"""Repair tenants stamped while the preceding revision was still pre-release."""
connection = op.get_bind()
inspector = inspect(connection)

project_columns = {column["name"] for column in inspector.get_columns("project")}
if "partition_position" not in project_columns:
with op.batch_alter_table("project", schema=None) as batch_op:
batch_op.add_column(
sa.Column(
"partition_position",
sa.Integer(),
server_default=sa.text("0"),
nullable=False,
)
)

table_names = set(inspector.get_table_names())
if "accepted_project_note_change" not in table_names:
_create_accepted_project_note_change()
_create_missing_indexes(set())
return

existing_indexes = {
index["name"]
for index in inspector.get_indexes("accepted_project_note_change")
if index["name"] is not None
}
_create_missing_indexes(existing_indexes)


def downgrade() -> None:
"""Keep the schema promised by the preceding revision."""
Loading
Loading