Fix PostgreSQL compatibility in Alembic migrations
This commit is contained in:
@@ -15,9 +15,27 @@ branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_column(table_name: str, column_name: str) -> bool:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
return column_name in [c["name"] for c in inspector.get_columns(table_name)]
|
||||
|
||||
|
||||
def upgrade():
|
||||
# engagement_mode column already exists from the earlier partial migration,
|
||||
# so do NOT add it again.
|
||||
# On the original production database this column was added out-of-band,
|
||||
# outside of tracked migration history. On a fresh database (new env,
|
||||
# new backend such as Postgres) it does not exist yet, so create it
|
||||
# defensively here instead of assuming it's already present.
|
||||
if not _has_column("clients", "engagement_mode"):
|
||||
op.add_column(
|
||||
"clients",
|
||||
sa.Column(
|
||||
"engagement_mode",
|
||||
sa.String(length=30),
|
||||
nullable=False,
|
||||
server_default="internal_managed",
|
||||
),
|
||||
)
|
||||
|
||||
# Make partner_id nullable in a SQLite-safe way
|
||||
with op.batch_alter_table("clients") as batch_op:
|
||||
@@ -31,7 +49,7 @@ def upgrade():
|
||||
with op.batch_alter_table("clients") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"engagement_mode",
|
||||
existing_type=sa.String(length=32),
|
||||
existing_type=sa.String(length=30),
|
||||
server_default=None,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user