Implement proper Alembic migration
This commit is contained in:
parent
8eaac35ad4
commit
a0f8c23343
2 changed files with 41 additions and 9 deletions
|
@ -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),
|
||||
|
|
|
@ -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")
|
||||
|
|
Reference in a new issue