US01-02: Establish Application and Database Foundation (#47)
This commit was merged in pull request #47.
This commit is contained in:
50
migrations/env.py
Normal file
50
migrations/env.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Alembic environment.
|
||||
|
||||
The database URL is injected by photo_pipeline.db.run_migrations. Importing the
|
||||
models package registers every table on Base.metadata for autogenerate. Logging
|
||||
config is owned by the application, so fileConfig is intentionally not called.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
import photo_pipeline.models # noqa: F401 (registers models on Base.metadata)
|
||||
from photo_pipeline.db import Base
|
||||
|
||||
config = context.config
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=config.get_main_option("sqlalchemy.url"),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
render_as_batch=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
render_as_batch=True,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
22
migrations/script.py.mako
Normal file
22
migrations/script.py.mako
Normal file
@@ -0,0 +1,22 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
70
migrations/versions/0001_initial_identity.py
Normal file
70
migrations/versions/0001_initial_identity.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""Initial stable-identity schema: assets and asset_paths.
|
||||
|
||||
Revision ID: 0001_initial_identity
|
||||
Revises:
|
||||
Create Date: 2026-07-15
|
||||
"""
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = "0001_initial_identity"
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"assets",
|
||||
sa.Column("id", sa.String(), primary_key=True),
|
||||
sa.Column("original_path", sa.String(), nullable=False),
|
||||
sa.Column("current_path", sa.String(), nullable=True, unique=True),
|
||||
sa.Column("current_sha256", sa.String(), nullable=True),
|
||||
sa.Column("pixel_sha256", sa.String(), nullable=True),
|
||||
sa.Column("phash", sa.String(), nullable=True),
|
||||
sa.Column("hash_version", sa.Integer(), nullable=False, server_default="1"),
|
||||
sa.Column("byte_size", sa.Integer(), nullable=True),
|
||||
sa.Column("discovered_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("missing_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column(
|
||||
"availability_state", sa.String(), nullable=False, server_default="active"
|
||||
),
|
||||
sa.Column("state_version", sa.Integer(), nullable=False, server_default="1"),
|
||||
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_assets_current_sha256", "assets", ["current_sha256"])
|
||||
op.create_index("ix_assets_pixel_sha256", "assets", ["pixel_sha256"])
|
||||
op.create_index("ix_assets_phash", "assets", ["phash"])
|
||||
op.create_index("ix_assets_availability_state", "assets", ["availability_state"])
|
||||
|
||||
op.create_table(
|
||||
"asset_paths",
|
||||
sa.Column(
|
||||
"asset_id",
|
||||
sa.String(),
|
||||
sa.ForeignKey("assets.id"),
|
||||
primary_key=True,
|
||||
),
|
||||
sa.Column("path", sa.String(), primary_key=True),
|
||||
sa.Column("valid_from", sa.DateTime(timezone=True), primary_key=True),
|
||||
sa.Column("valid_until", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("reason", sa.String(), nullable=True),
|
||||
)
|
||||
op.create_index("ix_asset_paths_path", "asset_paths", ["path"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("asset_paths")
|
||||
op.drop_table("assets")
|
||||
Reference in New Issue
Block a user