mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-02 22:01:05 +02:00
118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
"""evolve podcasts: expand status lifecycle and add brief/transcript/storage columns
|
|
|
|
Revision ID: 158
|
|
Revises: 157
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
|
|
from alembic import op
|
|
|
|
revision: str = "158"
|
|
down_revision: str | None = "157"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def _drop_podcasts_from_publication() -> None:
|
|
"""Detach podcasts from zero_publication so status can be retyped.
|
|
|
|
Postgres refuses ``ALTER COLUMN ... TYPE`` on a column a publication
|
|
depends on. Some databases reach this migration with podcasts already
|
|
published (an interim apply_publication ran during 156); drop it here and
|
|
let migration 159 reconcile the publication to the canonical shape.
|
|
"""
|
|
conn = op.get_bind()
|
|
published = conn.execute(
|
|
sa.text(
|
|
"SELECT 1 FROM pg_publication_tables "
|
|
"WHERE pubname = 'zero_publication' "
|
|
"AND schemaname = current_schema() AND tablename = 'podcasts'"
|
|
)
|
|
).fetchone()
|
|
if published:
|
|
op.execute('ALTER PUBLICATION "zero_publication" DROP TABLE "podcasts";')
|
|
|
|
|
|
def upgrade() -> None:
|
|
_drop_podcasts_from_publication()
|
|
|
|
# Retype the status enum by swapping in a fresh type and casting existing
|
|
# rows. The legacy transient value 'generating' maps onto 'rendering'.
|
|
op.execute("ALTER TYPE podcast_status RENAME TO podcast_status_old;")
|
|
op.execute(
|
|
"""
|
|
CREATE TYPE podcast_status AS ENUM (
|
|
'pending', 'awaiting_brief', 'drafting', 'awaiting_review',
|
|
'rendering', 'ready', 'failed', 'cancelled'
|
|
);
|
|
"""
|
|
)
|
|
op.execute("ALTER TABLE podcasts ALTER COLUMN status DROP DEFAULT;")
|
|
op.execute(
|
|
"""
|
|
ALTER TABLE podcasts
|
|
ALTER COLUMN status TYPE podcast_status
|
|
USING (
|
|
CASE status::text
|
|
WHEN 'generating' THEN 'rendering'
|
|
ELSE status::text
|
|
END
|
|
)::podcast_status;
|
|
"""
|
|
)
|
|
op.execute("ALTER TABLE podcasts ALTER COLUMN status SET DEFAULT 'pending';")
|
|
op.execute("DROP TYPE podcast_status_old;")
|
|
|
|
op.execute("ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS source_content TEXT;")
|
|
op.execute("ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS spec JSONB;")
|
|
op.execute(
|
|
"ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS spec_version "
|
|
"INTEGER NOT NULL DEFAULT 1;"
|
|
)
|
|
op.execute(
|
|
"ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS storage_backend VARCHAR(32);"
|
|
)
|
|
op.execute("ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS storage_key TEXT;")
|
|
op.execute(
|
|
"ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS duration_seconds INTEGER;"
|
|
)
|
|
op.execute("ALTER TABLE podcasts ADD COLUMN IF NOT EXISTS error TEXT;")
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS error;")
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS duration_seconds;")
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS storage_key;")
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS storage_backend;")
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS spec_version;")
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS spec;")
|
|
op.execute("ALTER TABLE podcasts DROP COLUMN IF EXISTS source_content;")
|
|
|
|
# Collapse the expanded lifecycle back onto the original four values.
|
|
op.execute("ALTER TYPE podcast_status RENAME TO podcast_status_new;")
|
|
op.execute(
|
|
"CREATE TYPE podcast_status AS ENUM "
|
|
"('pending', 'generating', 'ready', 'failed');"
|
|
)
|
|
op.execute("ALTER TABLE podcasts ALTER COLUMN status DROP DEFAULT;")
|
|
op.execute(
|
|
"""
|
|
ALTER TABLE podcasts
|
|
ALTER COLUMN status TYPE podcast_status
|
|
USING (
|
|
CASE status::text
|
|
WHEN 'awaiting_brief' THEN 'pending'
|
|
WHEN 'drafting' THEN 'generating'
|
|
WHEN 'awaiting_review' THEN 'generating'
|
|
WHEN 'rendering' THEN 'generating'
|
|
WHEN 'cancelled' THEN 'failed'
|
|
ELSE status::text
|
|
END
|
|
)::podcast_status;
|
|
"""
|
|
)
|
|
op.execute("ALTER TABLE podcasts ALTER COLUMN status SET DEFAULT 'ready';")
|
|
op.execute("DROP TYPE podcast_status_new;")
|