fix: add column check in migration
This commit is contained in:
parent
9041a804ec
commit
ff263c0132
1 changed files with 22 additions and 6 deletions
|
|
@ -17,14 +17,30 @@ down_revision: Union[str, None] = '211ab850ef3d'
|
||||||
branch_labels: Union[str, Sequence[str], None] = None
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
def _get_column(inspector, table, name, schema=None):
|
||||||
|
for col in inspector.get_columns(table, schema=schema):
|
||||||
|
if col["name"] == name:
|
||||||
|
return col
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
op.add_column('data',
|
conn = op.get_bind()
|
||||||
sa.Column('last_accessed', sa.DateTime(timezone=True), nullable=True)
|
insp = sa.inspect(conn)
|
||||||
)
|
|
||||||
# Optionally initialize with created_at values for existing records
|
last_accessed_column = _get_column(insp, "data", "last_accessed")
|
||||||
op.execute("UPDATE data SET last_accessed = created_at")
|
if not last_accessed_column:
|
||||||
|
op.add_column('data',
|
||||||
|
sa.Column('last_accessed', sa.DateTime(timezone=True), nullable=True)
|
||||||
|
)
|
||||||
|
# Optionally initialize with created_at values for existing records
|
||||||
|
op.execute("UPDATE data SET last_accessed = created_at")
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
op.drop_column('data', 'last_accessed')
|
conn = op.get_bind()
|
||||||
|
insp = sa.inspect(conn)
|
||||||
|
|
||||||
|
last_accessed_column = _get_column(insp, "data", "last_accessed")
|
||||||
|
if last_accessed_column:
|
||||||
|
op.drop_column('data', 'last_accessed')
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue