PMFlow/backend/alembic/versions/20260506_add_execution_runt...

93 lines
3.5 KiB
Python

"""add execution runtime credentials
Revision ID: 20260506_add_execution_runtime_credentials
Revises: 20260505_add_pmflow_changeset_committer
Create Date: 2026-05-06
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
try:
from sqlalchemy.dialects.postgresql import JSONB
except Exception: # pragma: no cover
JSONB = sa.JSON
revision = "20260506_add_execution_runtime_credentials"
down_revision = "20260505_add_pmflow_changeset_committer"
branch_labels = None
depends_on = None
def _json_type(bind):
return JSONB() if bind.dialect.name == "postgresql" else sa.JSON()
def _table_exists(bind, table_name: str) -> bool:
return sa.inspect(bind).has_table(table_name)
def _index_exists(bind, table_name: str, index_name: str) -> bool:
if not _table_exists(bind, table_name):
return False
return any(index["name"] == index_name for index in sa.inspect(bind).get_indexes(table_name))
def upgrade() -> None:
bind = op.get_bind()
table_name = "pmflow_execution_runtime_credentials"
if not _table_exists(bind, table_name):
op.create_table(
table_name,
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("tenant_id", sa.BigInteger(), nullable=False),
sa.Column("key_id", sa.String(length=80), nullable=False),
sa.Column("secret_hash", sa.String(length=128), nullable=False),
sa.Column("project_id", sa.BigInteger(), nullable=False),
sa.Column("task_id", sa.BigInteger(), nullable=False),
sa.Column("prompt_package_id", sa.BigInteger(), nullable=False),
sa.Column("repo_binding_id", sa.BigInteger(), nullable=True),
sa.Column("runtime_target", sa.String(length=32), nullable=False),
sa.Column("allowed_sources", _json_type(bind), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("expires_at", sa.TIMESTAMP(), nullable=False),
sa.Column("created_by", sa.BigInteger(), nullable=True),
sa.Column("last_used_at", sa.TIMESTAMP(), nullable=True),
sa.Column("use_count", sa.Integer(), nullable=False),
sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False),
sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.func.now(), nullable=False),
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
sa.ForeignKeyConstraint(["project_id"], ["projects.id"]),
sa.ForeignKeyConstraint(["prompt_package_id"], ["prompt_packages.id"]),
sa.ForeignKeyConstraint(["repo_binding_id"], ["repo_bindings.id"]),
sa.ForeignKeyConstraint(["task_id"], ["tasks.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("tenant_id", "key_id", name="uq_pmflow_runtime_credentials_tenant_key"),
)
for column_name in (
"tenant_id",
"key_id",
"project_id",
"task_id",
"prompt_package_id",
"repo_binding_id",
"runtime_target",
"status",
"expires_at",
"created_by",
):
index_name = f"ix_{table_name}_{column_name}"
if not _index_exists(bind, table_name, index_name):
op.create_index(index_name, table_name, [column_name], unique=False)
def downgrade() -> None:
bind = op.get_bind()
table_name = "pmflow_execution_runtime_credentials"
if _table_exists(bind, table_name):
op.drop_table(table_name)