I'm supposed to add time to a date field. We use MySQL with python3, alembic and sqlalchemy. Now I've written a conversion program. It is my first program of such a kind. I'm not certain how it will work, how I can also migrate the data to the new field or if that is a different scenario.
I'm not sure about ? downgrade , what should it do if it is downgrading? In the examples I've looked at examples where nearly all do an ADD TABLE and therefore at the downgrade those function just delete what they created. But what about an ALTER tatement, how can I roll it back in the downgrade function if downgrading (which probably not will happen).
Is there anything else that I should think about?
def upgrade(**args):
db_update_context = mnet_get_required_argument("db_update_context", **args)
if not mnet_column_exists(table_name, column_name, **args):
sql = textwrap.dedent("""\
ALTER TABLE {} MODIFY COLUMN `{}` datetime;
""".format(table_name, column_name))
mnet_execute(db_update_context, sql)
sql = textwrap.dedent("""\
ALTER TABLE {} MODIFY COLUMN `{}` datetime;
""".format(table_name, column_name2))
mnet_execute(db_update_context, sql)
def downgrade(**args): # TODO: What is supposed to happen here
columns_to_drop = [column_name]
for column_to_drop in columns_to_drop:
medinet_drop_column(table_name, column_to_drop, **args)