Skip to content

Commit

Permalink
Init migration
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Sep 17, 2024
1 parent 781177e commit 42613b9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
9 changes: 6 additions & 3 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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():
Expand Down
94 changes: 94 additions & 0 deletions migrations/versions/5c7fb996a88b_initial_migration.py
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit 42613b9

Please sign in to comment.