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:
op.create_table(
"portfolio_comments",
sa.Column("id", sa.Text(), primary_key=True),
sa.Column("portfolio_id", sa.Text(), nullable=False),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("NOW()")),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("NOW()")),
# `ensure_schema()` can create this table ahead of Alembic on already-running
# environments, so this migration must be safe to re-apply.
op.execute(
"""
CREATE TABLE IF NOT EXISTS portfolio_comments (
id TEXT NOT NULL,
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(
"idx_portfolio_comments_portfolio_created",
"portfolio_comments",
["portfolio_id", sa.text("created_at DESC")],
unique=False,
op.execute(
"""
CREATE INDEX IF NOT EXISTS idx_portfolio_comments_portfolio_created
ON portfolio_comments (portfolio_id, created_at DESC)
"""
)
def downgrade() -> None:
op.drop_index("idx_portfolio_comments_portfolio_created", table_name="portfolio_comments")
op.drop_table("portfolio_comments")
op.execute("DROP INDEX IF EXISTS idx_portfolio_comments_portfolio_created")
op.execute("DROP TABLE IF EXISTS portfolio_comments")