fix: make portfolio comments migration idempotent

This commit is contained in:
2026-04-11 00:40:05 +00:00
parent 0d658191d1
commit 1552286088
@@ -18,22 +18,28 @@ depends_on = None
def upgrade() -> None: def upgrade() -> None:
op.create_table( # `ensure_schema()` can create this table ahead of Alembic on already-running
"portfolio_comments", # environments, so this migration must be safe to re-apply.
sa.Column("id", sa.Text(), primary_key=True), op.execute(
sa.Column("portfolio_id", sa.Text(), nullable=False), """
sa.Column("content", sa.Text(), nullable=False), CREATE TABLE IF NOT EXISTS portfolio_comments (
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("NOW()")), id TEXT NOT NULL,
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("NOW()")), portfolio_id TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
PRIMARY KEY (id)
)
"""
) )
op.create_index( op.execute(
"idx_portfolio_comments_portfolio_created", """
"portfolio_comments", CREATE INDEX IF NOT EXISTS idx_portfolio_comments_portfolio_created
["portfolio_id", sa.text("created_at DESC")], ON portfolio_comments (portfolio_id, created_at DESC)
unique=False, """
) )
def downgrade() -> None: def downgrade() -> None:
op.drop_index("idx_portfolio_comments_portfolio_created", table_name="portfolio_comments") op.execute("DROP INDEX IF EXISTS idx_portfolio_comments_portfolio_created")
op.drop_table("portfolio_comments") op.execute("DROP TABLE IF EXISTS portfolio_comments")