From 2a1d2be232e863708f3237d6514be7190045ddad Mon Sep 17 00:00:00 2001 From: zhangxunhui Date: Sun, 15 Aug 2021 15:54:19 +0800 Subject: [PATCH] change tables --- app/db/__init__.py | 0 app/db/alembic/env.py | 5 +-- ...51442c_add_two_indexes_to_pull_requests.py | 28 +++++++++++++++ ...change_comment_or_not_to_default_0_and_.py | 34 +++++++++++++++++++ ...965_add_column_installation_id_to_pull_.py | 28 +++++++++++++++ ...e69e7db9d798_change_table_pull_requests.py | 30 ++++++++++++++++ .../ec616b7d4340_change_index_format.py | 32 +++++++++++++++++ .../f076c766cb48_add_an_index_to_comments.py | 28 +++++++++++++++ app/db/operators/base_operator.py | 4 ++- app/db/operators/pull_request_operator.py | 11 ++++++ 10 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 app/db/__init__.py create mode 100644 app/db/alembic/versions/3f138d51442c_add_two_indexes_to_pull_requests.py create mode 100644 app/db/alembic/versions/4939a95bd8e1_change_comment_or_not_to_default_0_and_.py create mode 100644 app/db/alembic/versions/8ef9b7227965_add_column_installation_id_to_pull_.py create mode 100644 app/db/alembic/versions/e69e7db9d798_change_table_pull_requests.py create mode 100644 app/db/alembic/versions/ec616b7d4340_change_index_format.py create mode 100644 app/db/alembic/versions/f076c766cb48_add_an_index_to_comments.py diff --git a/app/db/__init__.py b/app/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/db/alembic/env.py b/app/db/alembic/env.py index e821ca3..b821a89 100644 --- a/app/db/alembic/env.py +++ b/app/db/alembic/env.py @@ -10,9 +10,6 @@ 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 @@ -29,7 +26,7 @@ from app.db.tables import sqlalchemy_orm target_metadata = sqlalchemy_orm.Base.metadata env = ConfigLoader().load_env() -connection_url = "mysql+mysqldb://{user}:{password}@{host}:{port}/{db}".format(user=env["MYSQL"]["USER"], password=env["MYSQL"]["PASSWORD"], host=env["MYSQL"]["HOST"], port=env["MYSQL"]["PORT"], db=env["MYSQL"]["DB"]) +connection_url = "mysql+pymysql://{user}:{password}@{host}:{port}/{db}".format(user=env["MYSQL"]["USER"], password=env["MYSQL"]["PASSWORD"], host=env["MYSQL"]["HOST"], port=env["MYSQL"]["PORT"], db=env["MYSQL"]["DB"]) config.set_main_option("sqlalchemy.url", connection_url) # 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, diff --git a/app/db/alembic/versions/3f138d51442c_add_two_indexes_to_pull_requests.py b/app/db/alembic/versions/3f138d51442c_add_two_indexes_to_pull_requests.py new file mode 100644 index 0000000..96921c8 --- /dev/null +++ b/app/db/alembic/versions/3f138d51442c_add_two_indexes_to_pull_requests.py @@ -0,0 +1,28 @@ +"""add two indexes to pull_requests + +Revision ID: 3f138d51442c +Revises: 8ef9b7227965 +Create Date: 2021-08-15 15:25:04.176696 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '3f138d51442c' +down_revision = '8ef9b7227965' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/app/db/alembic/versions/4939a95bd8e1_change_comment_or_not_to_default_0_and_.py b/app/db/alembic/versions/4939a95bd8e1_change_comment_or_not_to_default_0_and_.py new file mode 100644 index 0000000..d4cfc82 --- /dev/null +++ b/app/db/alembic/versions/4939a95bd8e1_change_comment_or_not_to_default_0_and_.py @@ -0,0 +1,34 @@ +"""change comment_or_not to default=0 and not nullable and add unique index for unique pull_request + +Revision ID: 4939a95bd8e1 +Revises: ec616b7d4340 +Create Date: 2021-08-15 15:50:55.855430 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = '4939a95bd8e1' +down_revision = 'ec616b7d4340' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column('pull_requests', 'comment_or_not', + existing_type=mysql.TINYINT(display_width=1), + nullable=False) + op.create_index('owner_repo_num', 'pull_requests', ['owner_login', 'repo_name', 'number'], unique=True) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('owner_repo_num', table_name='pull_requests') + op.alter_column('pull_requests', 'comment_or_not', + existing_type=mysql.TINYINT(display_width=1), + nullable=True) + # ### end Alembic commands ### diff --git a/app/db/alembic/versions/8ef9b7227965_add_column_installation_id_to_pull_.py b/app/db/alembic/versions/8ef9b7227965_add_column_installation_id_to_pull_.py new file mode 100644 index 0000000..e2de566 --- /dev/null +++ b/app/db/alembic/versions/8ef9b7227965_add_column_installation_id_to_pull_.py @@ -0,0 +1,28 @@ +"""add column installation_id to pull_requests + +Revision ID: 8ef9b7227965 +Revises: e69e7db9d798 +Create Date: 2021-08-15 15:21:53.401153 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8ef9b7227965' +down_revision = 'e69e7db9d798' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('pull_requests', sa.Column('installation_id', sa.Integer(), nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('pull_requests', 'installation_id') + # ### end Alembic commands ### diff --git a/app/db/alembic/versions/e69e7db9d798_change_table_pull_requests.py b/app/db/alembic/versions/e69e7db9d798_change_table_pull_requests.py new file mode 100644 index 0000000..0cc7970 --- /dev/null +++ b/app/db/alembic/versions/e69e7db9d798_change_table_pull_requests.py @@ -0,0 +1,30 @@ +"""change table pull_requests + +Revision ID: e69e7db9d798 +Revises: 4ad7b4a06e6c +Create Date: 2021-08-15 13:15:50.974380 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = 'e69e7db9d798' +down_revision = '4ad7b4a06e6c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('pull_requests', sa.Column('last_comment_at', sa.TIMESTAMP(), nullable=True)) + op.drop_column('pull_requests', 'updated_at') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('pull_requests', sa.Column('updated_at', mysql.TIMESTAMP(), nullable=True)) + op.drop_column('pull_requests', 'last_comment_at') + # ### end Alembic commands ### diff --git a/app/db/alembic/versions/ec616b7d4340_change_index_format.py b/app/db/alembic/versions/ec616b7d4340_change_index_format.py new file mode 100644 index 0000000..d78422b --- /dev/null +++ b/app/db/alembic/versions/ec616b7d4340_change_index_format.py @@ -0,0 +1,32 @@ +"""change index format + +Revision ID: ec616b7d4340 +Revises: f076c766cb48 +Create Date: 2021-08-15 15:30:23.121589 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ec616b7d4340' +down_revision = 'f076c766cb48' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index(op.f('ix_comments_pull_request_id'), 'comments', ['pull_request_id'], unique=False) + op.create_index(op.f('ix_pull_requests_locked'), 'pull_requests', ['locked'], unique=False) + op.create_index(op.f('ix_pull_requests_state'), 'pull_requests', ['state'], unique=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_pull_requests_state'), table_name='pull_requests') + op.drop_index(op.f('ix_pull_requests_locked'), table_name='pull_requests') + op.drop_index(op.f('ix_comments_pull_request_id'), table_name='comments') + # ### end Alembic commands ### diff --git a/app/db/alembic/versions/f076c766cb48_add_an_index_to_comments.py b/app/db/alembic/versions/f076c766cb48_add_an_index_to_comments.py new file mode 100644 index 0000000..6474d3e --- /dev/null +++ b/app/db/alembic/versions/f076c766cb48_add_an_index_to_comments.py @@ -0,0 +1,28 @@ +"""add an index to comments + +Revision ID: f076c766cb48 +Revises: 3f138d51442c +Create Date: 2021-08-15 15:25:58.162771 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f076c766cb48' +down_revision = '3f138d51442c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/app/db/operators/base_operator.py b/app/db/operators/base_operator.py index 898a27d..fc94339 100644 --- a/app/db/operators/base_operator.py +++ b/app/db/operators/base_operator.py @@ -10,6 +10,8 @@ class BaseOperator: def __init__(self) -> None: try: - self.engine = create_async_engine(ConfigLoader().load_env()["MYSQL"]["MYSQL_CONNECTION"], echo=True) + env = ConfigLoader().load_env() + url = "mysql+aiomysql://{user}:{password}@{host}:{port}/{db}".format(user=env["MYSQL"]["USER"], password=env["MYSQL"]["PASSWORD"], host=env["MYSQL"]["HOST"], port=env["MYSQL"]["PORT"], db=env["MYSQL"]["DB"]) + self.engine = create_async_engine(url, echo=True) except Exception as e: print("error with initialization of BaseOperator: %s" % (repr(e))) \ No newline at end of file diff --git a/app/db/operators/pull_request_operator.py b/app/db/operators/pull_request_operator.py index 2d3dc43..898c544 100644 --- a/app/db/operators/pull_request_operator.py +++ b/app/db/operators/pull_request_operator.py @@ -17,11 +17,15 @@ class PullRequestOperator(BaseOperator): super().__init__() + ''' + check whether pr exists, if exists then update + ''' async def insert_pull_request(self, pr: PullRequest) -> None: def run_queries(session): session.execute(insert(tModel.PullRequest).values( owner_login = pr.owner.login, repo_name = pr.repo.name, + installation_id = pr.installation.id, number = pr.number, state = pr.state, locked = pr.locked, @@ -35,6 +39,13 @@ class PullRequestOperator(BaseOperator): except Exception as e: print("error with func insert_pull_request: %s" % (repr(e))) + + async def update_pull_request(self, pr: PullRequest) -> None: + try: + pass + except Exception as e: + print("error with func update_pull_request: %s" % (repr(e))) + async def query_prs_4_scheduler(self) -> List[PullRequest]: try: