From 42613b9ab8d25d0544a748147b0bf396aa3e6fb5 Mon Sep 17 00:00:00 2001 From: Grey Li Date: Tue, 17 Sep 2024 22:40:17 +0800 Subject: [PATCH] Init migration --- migrations/env.py | 9 +- .../5c7fb996a88b_initial_migration.py | 94 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/5c7fb996a88b_initial_migration.py diff --git a/migrations/env.py b/migrations/env.py index 89f80b2..4c97092 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -19,7 +19,7 @@ def get_engine(): try: # this works with Flask-SQLAlchemy<3 and Alchemical return current_app.extensions['migrate'].db.get_engine() - except TypeError: + except (TypeError, AttributeError): # this works with Flask-SQLAlchemy>=3 return current_app.extensions['migrate'].db.engine @@ -90,14 +90,17 @@ def process_revision_directives(context, revision, directives): directives[:] = [] logger.info('No changes in schema detected.') + conf_args = current_app.extensions['migrate'].configure_args + if conf_args.get("process_revision_directives") is None: + conf_args["process_revision_directives"] = process_revision_directives + connectable = get_engine() with connectable.connect() as connection: context.configure( connection=connection, target_metadata=get_metadata(), - process_revision_directives=process_revision_directives, - **current_app.extensions['migrate'].configure_args + **conf_args ) with context.begin_transaction(): diff --git a/migrations/versions/5c7fb996a88b_initial_migration.py b/migrations/versions/5c7fb996a88b_initial_migration.py new file mode 100644 index 0000000..74e605e --- /dev/null +++ b/migrations/versions/5c7fb996a88b_initial_migration.py @@ -0,0 +1,94 @@ +"""Initial migration + +Revision ID: 5c7fb996a88b +Revises: +Create Date: 2024-09-17 21:49:19.320206 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '5c7fb996a88b' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('admin', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(length=20), nullable=False), + sa.Column('password_hash', sa.String(length=128), nullable=False), + sa.Column('blog_title', sa.String(length=60), nullable=False), + sa.Column('blog_sub_title', sa.String(length=100), nullable=False), + sa.Column('name', sa.String(length=30), nullable=False), + sa.Column('about', sa.Text(), nullable=False), + sa.Column('custom_footer', sa.Text(), nullable=True), + sa.Column('custom_css', sa.Text(), nullable=True), + sa.Column('custom_js', sa.Text(), nullable=True), + sa.PrimaryKeyConstraint('id', name=op.f('pk_admin')) + ) + op.create_table('category', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=30), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_category')), + sa.UniqueConstraint('name', name=op.f('uq_category_name')) + ) + op.create_table('link', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=30), nullable=False), + sa.Column('url', sa.String(length=255), nullable=False), + sa.PrimaryKeyConstraint('id', name=op.f('pk_link')) + ) + op.create_table('post', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('title', sa.String(length=60), nullable=False), + sa.Column('body', sa.Text(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('can_comment', sa.Boolean(), nullable=False), + sa.Column('category_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['category_id'], ['category.id'], name=op.f('fk_post_category_id_category')), + sa.PrimaryKeyConstraint('id', name=op.f('pk_post')) + ) + with op.batch_alter_table('post', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_post_created_at'), ['created_at'], unique=False) + + op.create_table('comment', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('author', sa.String(length=30), nullable=False), + sa.Column('email', sa.String(length=255), nullable=False), + sa.Column('site', sa.String(length=255), nullable=True), + sa.Column('body', sa.Text(), nullable=False), + sa.Column('from_admin', sa.Boolean(), nullable=False), + sa.Column('reviewed', sa.Boolean(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('replied_id', sa.Integer(), nullable=True), + sa.Column('post_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id'], name=op.f('fk_comment_post_id_post')), + sa.ForeignKeyConstraint(['replied_id'], ['comment.id'], name=op.f('fk_comment_replied_id_comment')), + sa.PrimaryKeyConstraint('id', name=op.f('pk_comment')) + ) + with op.batch_alter_table('comment', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_comment_created_at'), ['created_at'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('comment', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_comment_created_at')) + + op.drop_table('comment') + with op.batch_alter_table('post', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_post_created_at')) + + op.drop_table('post') + op.drop_table('link') + op.drop_table('category') + op.drop_table('admin') + # ### end Alembic commands ###