Update nullable attribute for created_at field in variable table

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-04-12 12:25:43 -03:00
commit 17f04d8b78

View file

@ -26,19 +26,21 @@ def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
if "variable" not in table_names: if "variable" not in table_names:
return return
columns = [column for column in inspector.get_columns("variable")]
column_names = [column["name"] for column in inspector.get_columns("variable")] column_names = [column["name"] for column in columns]
with op.batch_alter_table("variable", schema=None) as batch_op: with op.batch_alter_table("variable", schema=None) as batch_op:
if "created_at" in column_names: if "created_at" in column_names:
batch_op.alter_column( created_at_colunmn = next(column for column in columns if column["name"] == "created_at")
"created_at", if created_at_colunmn["nullable"] is False:
existing_type=sa.TIMESTAMP(timezone=True), batch_op.alter_column(
nullable=True, "created_at",
# existing_server_default expects str | bool | Identity | Computed | None existing_type=sa.TIMESTAMP(timezone=True),
# sa.text("now()") is not a valid value for existing_server_default nullable=True,
existing_server_default=False, # existing_server_default expects str | bool | Identity | Computed | None
) # sa.text("now()") is not a valid value for existing_server_default
existing_server_default=False,
)
# ### end Alembic commands ### # ### end Alembic commands ###
@ -50,13 +52,17 @@ def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
if "variable" not in table_names: if "variable" not in table_names:
return return
columns = [column for column in inspector.get_columns("variable")]
column_names = [column["name"] for column in columns]
with op.batch_alter_table("variable", schema=None) as batch_op: with op.batch_alter_table("variable", schema=None) as batch_op:
if "created_at" in inspector.get_columns("variable"): if "created_at" in column_names:
batch_op.alter_column( created_at_colunmn = next(column for column in columns if column["name"] == "created_at")
"created_at", if created_at_colunmn["nullable"] is True:
existing_type=sa.TIMESTAMP(timezone=True), batch_op.alter_column(
nullable=False, "created_at",
existing_server_default=False, existing_type=sa.TIMESTAMP(timezone=True),
) nullable=False,
existing_server_default=False,
)
# ### end Alembic commands ### # ### end Alembic commands ###