add alembic initialization and add two tables

This commit is contained in:
zhangxunhui 2021-08-14 13:39:23 +08:00
parent 8f62c8eccb
commit cb496678e0
5 changed files with 198 additions and 0 deletions

42
alembic.ini Normal file
View File

@ -0,0 +1,42 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = ./app/db/alembic
# sqlalchemy.url = driver://user:pass@localhost/dbname configured in the .env.yaml file
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

61
app/db/alembic/env.py Normal file
View File

@ -0,0 +1,61 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
import sys, pathlib
sys.path.append(str(pathlib.Path(__file__).resolve().parents[3]))
from app.utils.config_loader import ConfigLoader
import pymysql
pymysql.install_as_MySQLdb() # we are using python3
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from app.db.tables import sqlalchemy_orm
target_metadata = sqlalchemy_orm.Base.metadata
config.set_main_option("sqlalchemy.url", ConfigLoader().load_env()["MYSQL_CONNECTION"]) # we need to add this in main option, because the configuration of sqlalchemy.url is in .env.yaml, not in alembic.ini
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
run_migrations_online()

View File

@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}

View File

@ -0,0 +1,47 @@
"""change comment's body column type to Text
Revision ID: 5c2ce5113df2
Revises:
Create Date: 2021-08-14 13:37:46.787734
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '5c2ce5113df2'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('comments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('pull_request_id', sa.Integer(), nullable=False),
sa.Column('comment_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
sa.Column('body', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('pull_requests',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('owner_login', sa.VARCHAR(length=255), nullable=False),
sa.Column('repo_name', sa.VARCHAR(length=255), nullable=False),
sa.Column('number', sa.Integer(), nullable=False),
sa.Column('state', sa.VARCHAR(length=255), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(), nullable=True),
sa.Column('comment_or_not', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('pull_requests')
op.drop_table('comments')
# ### end Alembic commands ###

View File

@ -0,0 +1,24 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, VARCHAR, TIMESTAMP, Boolean, Text
Base = declarative_base()
class PullRequest(Base):
__tablename__ = "pull_requests"
id = Column(Integer, primary_key=True)
owner_login = Column(VARCHAR(255), nullable=False)
repo_name = Column(VARCHAR(255), nullable=False)
number = Column(Integer, nullable=False)
state = Column(VARCHAR(255), nullable=False)
created_at = Column(TIMESTAMP, nullable=False)
updated_at = Column(TIMESTAMP, nullable=True)
comment_or_not = Column(Boolean, nullable=True)
class Comment(Base):
__tablename__ = "comments"
id = Column(Integer, primary_key=True)
pull_request_id = Column(Integer, nullable=False)
comment_id = Column(Integer, nullable=False)
created_at = Column(TIMESTAMP, nullable=False)
body = Column(Text, nullable=True)