"""devices1 Revision ID: 34fe3d061c5f Revises: 8c06ac60d125 Create Date: 2020-10-27 19:38:48.445908 """ import sqlalchemy as sa from alembic import op # revision identifiers, used by Alembic. from sqlalchemy import column, select, table from sqlalchemy.dialects import postgresql revision = "34fe3d061c5f" down_revision = "8c06ac60d125" branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### product = table( "products", column("sale_category_id", postgresql.UUID()), column("sale_category_name", sa.Unicode(length=255)) ) sale_category = table("sale_categories", column("id", postgresql.UUID()), column("name", sa.Unicode(length=255))) op.execute( product.update( values={ "sale_category_id": select([sale_category.c.id]).where( product.c.sale_category_name == sale_category.c.name ) } ) ) section_printer = table( "section_printers", column("section_id", postgresql.UUID()), column("section_name", sa.Unicode(length=255)), column("printer_id", postgresql.UUID()), column("printer_name", sa.Unicode(length=255)), ) section = table("sections", column("id", postgresql.UUID()), column("name", sa.Unicode(length=255))) printer = table("printers", column("id", postgresql.UUID()), column("name", sa.Unicode(length=255))) op.execute( section_printer.update( values={"section_id": select([section.c.id]).where(section_printer.c.section_name == section.c.name)} ) ) op.execute( section_printer.update( values={"printer_id": select([printer.c.id]).where(section_printer.c.printer_name == printer.c.name)} ) ) with op.batch_alter_table("products") as batch_op: batch_op.alter_column("sale_category_id", nullable=True) batch_op.drop_column("sale_category_name") with op.batch_alter_table("section_printers") as batch_op: batch_op.alter_column("section_id", nullable=True) batch_op.drop_column("section_name") batch_op.alter_column("printer_id", nullable=True) batch_op.drop_column("printer_name") # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.add_column("products", sa.Column("sale_category_name", sa.Unicode(length=255), nullable=False)) op.drop_constraint("fk_products_sale_category_id_sale_categories", "products", type_="foreignkey") op.drop_column("products", "sale_category_id") # ### end Alembic commands ###