Fixed Alembic migrations to use python instead of SQL

This commit is contained in:
Luxray5474 2020-08-29 15:49:02 -04:00
parent c903218b8f
commit 6e619465cf

View file

@ -15,37 +15,24 @@ depends_on = None
def upgrade(): def upgrade():
conn = op.get_bind()
op.add_column( op.add_column(
"post", "post",
sa.Column("file_last_modified_time", sa.DateTime(), nullable=True), sa.Column("file_last_modified_time", sa.DateTime(), nullable=True),
) )
posts = sa.Table(
op.execute( "post",
""" sa.MetaData(),
DO sa.Column("id", sa.Integer, primary_key=True),
$do$ sa.Column("creation_time", sa.DateTime(), nullable=False),
DECLARE creation_time_candidate TIMESTAMP; sa.Column("file_last_modified_time", sa.DateTime(), nullable=True),
BEGIN )
WHILE EXISTS ( for row in conn.execute(posts.select()):
SELECT creation_time from "post" if row.file_last_modified_time is None:
WHERE file_last_modified_time IS NULL conn.execute(
) LOOP posts.update()
FOR creation_time_candidate IN ( .where(posts.c.id == row.id)
SELECT creation_time FROM "post" .values(file_last_modified_time=row.creation_time)
WHERE file_last_modified_time IS NULL
) LOOP
UPDATE "post"
SET file_last_modified_time = creation_time
WHERE
NOT EXISTS (
SELECT creation_time FROM "post"
WHERE file_last_modified_time = creation_time_candidate
);
END LOOP;
END LOOP;
END
$do$
"""
) )
op.alter_column("post", "file_last_modified_time", nullable=False) op.alter_column("post", "file_last_modified_time", nullable=False)