101 lines
4.7 KiB
Python
101 lines
4.7 KiB
Python
"""Rename plans and their per-operation journal (US04-01, US04-02).
|
|
|
|
Revision ID: 0007_rename_plans
|
|
Revises: 0006_album_proposals
|
|
Create Date: 2026-08-15
|
|
|
|
The journal columns (state machine, attempts, fencing token, verification evidence)
|
|
are created here alongside the plan itself so the crash-safe journal of US04-02 does
|
|
not have to re-migrate the very rows US04-01 creates. US04-01 fills the planning and
|
|
validation columns; the journal columns stay at their initial values until apply.
|
|
"""
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision = "0007_rename_plans"
|
|
down_revision = "0006_album_proposals"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.create_table(
|
|
"rename_plans",
|
|
sa.Column("id", sa.String(), primary_key=True),
|
|
# planned | validated | invalid | applying | applied | failed | rolled_back
|
|
sa.Column("state", sa.String(), nullable=False, server_default="planned"),
|
|
sa.Column("schema_version", sa.Integer(), nullable=False, server_default="1"),
|
|
# Content checksum over the plan's operations; a portable export carries it
|
|
# so a stale confirmation can be detected without trusting the browser.
|
|
sa.Column("checksum", sa.String(), nullable=True),
|
|
sa.Column("blockers", sa.String(), nullable=True), # JSON array of issues
|
|
sa.Column("operation_count", sa.Integer(), nullable=False, server_default="0"),
|
|
# Optimistic concurrency for confirmation (apply must present this).
|
|
sa.Column("version", sa.Integer(), nullable=False, server_default="1"),
|
|
sa.Column("validated_at", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("applied_at", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column(
|
|
"created_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
|
),
|
|
sa.Column(
|
|
"updated_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
|
),
|
|
)
|
|
op.create_index("ix_rename_plans_state", "rename_plans", ["state"])
|
|
|
|
op.create_table(
|
|
"rename_operations",
|
|
sa.Column("id", sa.String(), primary_key=True),
|
|
sa.Column(
|
|
"plan_id",
|
|
sa.String(),
|
|
sa.ForeignKey("rename_plans.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
),
|
|
# Deterministic order within the plan (parents before children).
|
|
sa.Column("sequence", sa.Integer(), nullable=False),
|
|
# move_folder | move_file — folder moves are the default (album renames).
|
|
sa.Column("operation", sa.String(), nullable=False),
|
|
sa.Column("album", sa.String(), nullable=True),
|
|
sa.Column("source_path", sa.String(), nullable=False),
|
|
sa.Column("destination_path", sa.String(), nullable=False),
|
|
# Preconditions captured at planning time and rechecked before mutation.
|
|
sa.Column("asset_ids", sa.String(), nullable=True), # JSON array
|
|
sa.Column("expected_sha256", sa.String(), nullable=True), # JSON {asset_id: sha}
|
|
sa.Column("asset_count", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("same_filesystem", sa.Boolean(), nullable=True),
|
|
sa.Column("case_only", sa.Boolean(), nullable=False, server_default="0"),
|
|
sa.Column("issues", sa.String(), nullable=True), # JSON array of issues
|
|
# ── journal (US04-02): untouched until apply ─────────────────────────
|
|
# planned → validated → moving → moved → database_updated → verified
|
|
# → complete, or failed / rollback_required
|
|
sa.Column("journal_state", sa.String(), nullable=False, server_default="planned"),
|
|
sa.Column("attempt_count", sa.Integer(), nullable=False, server_default="0"),
|
|
sa.Column("fencing_token", sa.Integer(), nullable=True),
|
|
sa.Column("worker_id", sa.String(), nullable=True),
|
|
sa.Column("verified_at", sa.DateTime(timezone=True), nullable=True),
|
|
sa.Column("error_code", sa.String(), nullable=True),
|
|
sa.Column("error_message", sa.String(), nullable=True),
|
|
sa.Column(
|
|
"updated_at",
|
|
sa.DateTime(timezone=True),
|
|
nullable=False,
|
|
server_default=sa.text("CURRENT_TIMESTAMP"),
|
|
),
|
|
sa.UniqueConstraint("plan_id", "sequence", name="uq_rename_operations_plan_sequence"),
|
|
)
|
|
op.create_index("ix_rename_operations_plan_id", "rename_operations", ["plan_id"])
|
|
op.create_index("ix_rename_operations_journal_state", "rename_operations", ["journal_state"])
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_table("rename_operations")
|
|
op.drop_table("rename_plans")
|