From a0f8c23343bb4167241ea040d6a9d41acbd1713b Mon Sep 17 00:00:00 2001 From: Luxray5474 Date: Thu, 27 Aug 2020 09:55:06 -0400 Subject: [PATCH] Implement proper Alembic migration --- .../336a76ec1338_create_post_tables.py | 1 - ...6c358b0ca93_add_file_last_modified_time.py | 49 ++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/server/szurubooru/migrations/versions/336a76ec1338_create_post_tables.py b/server/szurubooru/migrations/versions/336a76ec1338_create_post_tables.py index 69fcbfc9..aea6cc78 100644 --- a/server/szurubooru/migrations/versions/336a76ec1338_create_post_tables.py +++ b/server/szurubooru/migrations/versions/336a76ec1338_create_post_tables.py @@ -20,7 +20,6 @@ def upgrade(): sa.Column("id", sa.Integer(), nullable=False), sa.Column("user_id", sa.Integer(), nullable=True), sa.Column("creation_time", sa.DateTime(), nullable=False), - sa.Column("file_last_modified_time", sa.DateTime(), nullable=False), sa.Column("last_edit_time", sa.DateTime(), nullable=True), sa.Column("safety", sa.Unicode(length=32), nullable=False), sa.Column("type", sa.Unicode(length=32), nullable=False), diff --git a/server/szurubooru/migrations/versions/46c358b0ca93_add_file_last_modified_time.py b/server/szurubooru/migrations/versions/46c358b0ca93_add_file_last_modified_time.py index cca725fb..2f694fe2 100644 --- a/server/szurubooru/migrations/versions/46c358b0ca93_add_file_last_modified_time.py +++ b/server/szurubooru/migrations/versions/46c358b0ca93_add_file_last_modified_time.py @@ -1,22 +1,55 @@ -''' +""" Add file last modified time Revision ID: 46c358b0ca93 Created at: 2020-08-26 17:08:17.845827 -''' +""" import sqlalchemy as sa from alembic import op - - -revision = '46c358b0ca93' -down_revision = '54de8acc6cef' +revision = "46c358b0ca93" +down_revision = "54de8acc6cef" branch_labels = None depends_on = None + def upgrade(): - pass + op.add_column( + "post", + sa.Column("file_last_modified_time", sa.DateTime(), nullable=True), + ) + + op.execute( + """ + DO + $do$ + DECLARE creation_time_candidate TIMESTAMP; + BEGIN + WHILE EXISTS ( + SELECT creation_time from "post" + WHERE file_last_modified_time IS NULL + ) LOOP + FOR creation_time_candidate IN ( + SELECT creation_time FROM "post" + 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) + def downgrade(): - pass + op.drop_column("post", "file_last_modified_time")