From 20610f8c89a06003aac9eceea97964fbaa681a0c Mon Sep 17 00:00:00 2001 From: Zeid Zabaneh Date: Tue, 12 Dec 2023 13:13:23 -0500 Subject: [PATCH] models: prepare old model files for porting (bug 1869597) - remove old migration and ancillary files - move old model files to lando.main - move base model definitions to base.py --- landoapi/models/base.py | 54 ------ migrations/README.md | 36 ---- migrations/alembic.ini | 42 ----- migrations/env.py | 92 ---------- migrations/script.py.mako | 24 --- .../06cc1c6a04b5_try_related_changes.py | 37 ---- ...ceca83_add_revision_landing_job_diff_id.py | 29 ---- .../56c6748ee7cf_add_revision_data.py | 36 ---- migrations/versions/7883d80258fb_initial.py | 160 ------------------ .../a8cd4d43c4c3_revision_landing_jobs.py | 93 ---------- .../c5b4350e073b_add_revision_model.py | 41 ----- src/lando/main/models/__init__.py | 1 + src/lando/main/{models.py => models/base.py} | 0 .../lando/main}/models/configuration.py | 0 .../lando/main}/models/landing_job.py | 0 .../lando/main/models/revision.py | 0 16 files changed, 1 insertion(+), 644 deletions(-) delete mode 100644 landoapi/models/base.py delete mode 100644 migrations/README.md delete mode 100644 migrations/alembic.ini delete mode 100644 migrations/env.py delete mode 100644 migrations/script.py.mako delete mode 100644 migrations/versions/06cc1c6a04b5_try_related_changes.py delete mode 100644 migrations/versions/50ffadceca83_add_revision_landing_job_diff_id.py delete mode 100644 migrations/versions/56c6748ee7cf_add_revision_data.py delete mode 100644 migrations/versions/7883d80258fb_initial.py delete mode 100644 migrations/versions/a8cd4d43c4c3_revision_landing_jobs.py delete mode 100644 migrations/versions/c5b4350e073b_add_revision_model.py create mode 100644 src/lando/main/models/__init__.py rename src/lando/main/{models.py => models/base.py} (100%) rename {landoapi => src/lando/main}/models/configuration.py (100%) rename {landoapi => src/lando/main}/models/landing_job.py (100%) rename landoapi/models/revisions.py => src/lando/main/models/revision.py (100%) diff --git a/landoapi/models/base.py b/landoapi/models/base.py deleted file mode 100644 index e7f452ff..00000000 --- a/landoapi/models/base.py +++ /dev/null @@ -1,54 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -import re - -from sqlalchemy.ext.declarative import declared_attr - -from landoapi.storage import db - -# Regex to parse various forms of capitalizations/camel case into snake case. -table_name_re = re.compile("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))") - - -class Base(db.Model): - """An abstract base model that provides common methods and columns.""" - - __abstract__ = True - - id = db.Column(db.Integer, primary_key=True) - created_at = db.Column( - db.DateTime(timezone=True), nullable=False, default=db.func.now() - ) - updated_at = db.Column( - db.DateTime(timezone=True), - nullable=False, - default=db.func.now(), - onupdate=db.func.now(), - ) - - @declared_attr - def __tablename__(self) -> str: - """Return a snake-case version of the class name as the table name. - - To override __tablename__, define this attribute as needed on your - model. - """ - return table_name_re.sub(r"_\1", self.__name__).lower() - - def __repr__(self) -> str: - """Return a human-readable representation of the instance. - - For example, ``. - """ - return f"<{self.__class__.__name__}: {self.id}>" - - @classmethod - def lock_table(cls, mode: str = "SHARE ROW EXCLUSIVE MODE"): - """Lock the table for the model with the given mode. - - Args: - mode (str): the lock mode to apply to the table when locking - """ - query = f"LOCK TABLE {cls.__table__.name} IN {mode};" - db.session.execute(query) diff --git a/migrations/README.md b/migrations/README.md deleted file mode 100644 index acd61c1a..00000000 --- a/migrations/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# Creating a new migration - -To create a new migration file (e.g. after updating your models), follow these steps: - -1. Make sure that your database is created and up to date, by running: - - ```shell - docker-compose run lando-api db upgrade - ``` - -2. Generate a new revision by running - - ```shell - docker-compose run lando-api db revision \ - --autogenerate \ - --message - ``` - -3. Repeat step (1) to run your migration. - - To check that your migrations are up to date, you can run the following command. - The output would show information about the current revision. - - ```shell - $ docker-compose run lando-api db show - - Rev: 7883d80258fb (head) - Parent: - Path: /app/migrations/versions/7883d80258fb_initial.py - - initial - - Revision ID: 7883d80258fb - Revises: - Create Date: 2022-04-07 15:41:46.233567 - ``` diff --git a/migrations/alembic.ini b/migrations/alembic.ini deleted file mode 100644 index 4375794c..00000000 --- a/migrations/alembic.ini +++ /dev/null @@ -1,42 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -[loggers] -keys = root,sqlalchemy,alembic,flask_migrate - -[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 - -[logger_flask_migrate] -level = INFO -handlers = -qualname = flask_migrate - -[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 diff --git a/migrations/env.py b/migrations/env.py deleted file mode 100644 index ca62341a..00000000 --- a/migrations/env.py +++ /dev/null @@ -1,92 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -from __future__ import with_statement - -import logging -from logging.config import fileConfig - -from alembic import context -from flask import current_app - -# 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) -logger = logging.getLogger("alembic.env") - -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -config.set_main_option( - "sqlalchemy.url", - str(current_app.extensions["migrate"].db.get_engine().url).replace("%", "%%"), -) -target_metadata = current_app.extensions["migrate"].db.metadata - -# 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_offline(): - """Run migrations in 'offline' mode. - - This configures the context with just a URL - and not an Engine, though an Engine is acceptable - here as well. By skipping the Engine creation - we don't even need a DBAPI to be available. - - Calls to context.execute() here emit the given string to the - script output. - - """ - url = config.get_main_option("sqlalchemy.url") - context.configure(url=url, target_metadata=target_metadata, literal_binds=True) - - with context.begin_transaction(): - context.run_migrations() - - -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. - - """ - - # this callback is used to prevent an auto-migration from being generated - # when there are no changes to the schema - # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html - def process_revision_directives(context, revision, directives): - if getattr(config.cmd_opts, "autogenerate", False): - script = directives[0] - if script.upgrade_ops.is_empty(): - directives[:] = [] - logger.info("No changes in schema detected.") - - connectable = current_app.extensions["migrate"].db.get_engine() - - with connectable.connect() as connection: - context.configure( - connection=connection, - target_metadata=target_metadata, - process_revision_directives=process_revision_directives, - **current_app.extensions["migrate"].configure_args - ) - - with context.begin_transaction(): - context.run_migrations() - - -if context.is_offline_mode(): - run_migrations_offline() -else: - run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako deleted file mode 100644 index 2c015630..00000000 --- a/migrations/script.py.mako +++ /dev/null @@ -1,24 +0,0 @@ -"""${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"} diff --git a/migrations/versions/06cc1c6a04b5_try_related_changes.py b/migrations/versions/06cc1c6a04b5_try_related_changes.py deleted file mode 100644 index f9a56d36..00000000 --- a/migrations/versions/06cc1c6a04b5_try_related_changes.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Set Phab-related Revision fields to nullable and add target field. - -Revision ID: 06cc1c6a04b5 -Revises: 50ffadceca83 -Create Date: 2023-06-12 20:12:46.910910 - -""" -import sqlalchemy as sa -from alembic import op - -# revision identifiers, used by Alembic. -revision = "06cc1c6a04b5" -down_revision = "50ffadceca83" -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column( - "landing_job", sa.Column("target_commit_hash", sa.Text(), nullable=True) - ) - op.alter_column( - "revision", "revision_id", existing_type=sa.INTEGER(), nullable=True - ) - op.alter_column("revision", "diff_id", existing_type=sa.INTEGER(), nullable=True) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column("revision", "diff_id", existing_type=sa.INTEGER(), nullable=False) - op.alter_column( - "revision", "revision_id", existing_type=sa.INTEGER(), nullable=False - ) - op.drop_column("landing_job", "target_commit_hash") - # ### end Alembic commands ### diff --git a/migrations/versions/50ffadceca83_add_revision_landing_job_diff_id.py b/migrations/versions/50ffadceca83_add_revision_landing_job_diff_id.py deleted file mode 100644 index 80a75d34..00000000 --- a/migrations/versions/50ffadceca83_add_revision_landing_job_diff_id.py +++ /dev/null @@ -1,29 +0,0 @@ -"""add revision_landing_job.diff_id - -Revision ID: 50ffadceca83 -Revises: a8cd4d43c4c3 -Create Date: 2023-05-25 01:50:36.755884 - -""" -import sqlalchemy as sa -from alembic import op - -# revision identifiers, used by Alembic. -revision = "50ffadceca83" -down_revision = "a8cd4d43c4c3" -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column( - "revision_landing_job", sa.Column("diff_id", sa.Integer(), nullable=True) - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column("revision_landing_job", "diff_id") - # ### end Alembic commands ### diff --git a/migrations/versions/56c6748ee7cf_add_revision_data.py b/migrations/versions/56c6748ee7cf_add_revision_data.py deleted file mode 100644 index 52849b3b..00000000 --- a/migrations/versions/56c6748ee7cf_add_revision_data.py +++ /dev/null @@ -1,36 +0,0 @@ -"""add revision data - -Revision ID: 56c6748ee7cf -Revises: 06cc1c6a04b5 -Create Date: 2023-08-31 17:36:58.309190 - -""" -import sqlalchemy as sa -from alembic import op -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision = "56c6748ee7cf" -down_revision = "06cc1c6a04b5" -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column( - "revision", - sa.Column( - "data", - postgresql.JSONB(astext_type=sa.Text()), - nullable=False, - server_default="{}", - ), - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column("revision", "data") - # ### end Alembic commands ### diff --git a/migrations/versions/7883d80258fb_initial.py b/migrations/versions/7883d80258fb_initial.py deleted file mode 100644 index 8516c978..00000000 --- a/migrations/versions/7883d80258fb_initial.py +++ /dev/null @@ -1,160 +0,0 @@ -"""initial - -Revision ID: 7883d80258fb -Revises: -Create Date: 2022-04-07 15:41:46.233567 - -""" -import sqlalchemy as sa -from alembic import op -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision = "7883d80258fb" -down_revision = None -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( - "configuration_variable", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("key", sa.String(), nullable=True), - sa.Column("raw_value", sa.String(length=254), nullable=True), - sa.Column( - "variable_type", - sa.Enum("BOOL", "INT", "STR", name="variabletype"), - nullable=True, - ), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("key"), - ) - op.create_table( - "diff_warning", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("revision_id", sa.Integer(), nullable=False), - sa.Column("diff_id", sa.Integer(), nullable=False), - sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=False), - sa.Column( - "status", - sa.Enum("ACTIVE", "ARCHIVED", name="diffwarningstatus"), - nullable=False, - ), - sa.Column( - "group", sa.Enum("GENERAL", "LINT", name="diffwarninggroup"), nullable=False - ), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "landing_job", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), - sa.Column( - "status", - sa.Enum( - "SUBMITTED", - "IN_PROGRESS", - "DEFERRED", - "FAILED", - "LANDED", - "CANCELLED", - name="landingjobstatus", - ), - nullable=False, - ), - sa.Column( - "revision_to_diff_id", - postgresql.JSONB(astext_type=sa.Text()), - nullable=False, - ), - sa.Column( - "revision_order", postgresql.JSONB(astext_type=sa.Text()), nullable=False - ), - sa.Column("error", sa.Text(), nullable=True), - sa.Column( - "error_breakdown", postgresql.JSONB(astext_type=sa.Text()), nullable=True - ), - sa.Column("requester_email", sa.String(length=254), nullable=False), - sa.Column("repository_name", sa.Text(), nullable=False), - sa.Column("repository_url", sa.Text(), nullable=True), - sa.Column("landed_commit_id", sa.Text(), nullable=True), - sa.Column("attempts", sa.Integer(), nullable=False), - sa.Column("priority", sa.Integer(), nullable=False), - sa.Column("duration_seconds", sa.Integer(), nullable=True), - sa.Column( - "formatted_replacements", - postgresql.JSONB(astext_type=sa.Text()), - nullable=True, - ), - sa.PrimaryKeyConstraint("id"), - ) - op.create_table( - "secapproval_requests", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("revision_id", sa.Integer(), nullable=False), - sa.Column("diff_phid", sa.Text(), nullable=False), - sa.Column( - "comment_candidates", - postgresql.JSONB(astext_type=sa.Text()), - nullable=False, - ), - sa.PrimaryKeyConstraint("id"), - ) - op.create_index( - op.f("ix_secapproval_requests_revision_id"), - "secapproval_requests", - ["revision_id"], - unique=False, - ) - op.create_table( - "transplants", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("request_id", sa.Integer(), nullable=True), - sa.Column( - "status", - sa.Enum( - "aborted", "submitted", "landed", "failed", name="transplantstatus" - ), - nullable=False, - ), - sa.Column( - "revision_to_diff_id", - postgresql.JSONB(astext_type=sa.Text()), - nullable=False, - ), - sa.Column( - "revision_order", postgresql.JSONB(astext_type=sa.Text()), nullable=False - ), - sa.Column("error", sa.Text(), nullable=True), - sa.Column("result", sa.Text(), nullable=True), - sa.Column("requester_email", sa.String(length=254), nullable=True), - sa.Column("repository_url", sa.Text(), nullable=True), - sa.Column("tree", sa.String(length=128), nullable=True), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("request_id"), - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table("transplants") - op.drop_index( - op.f("ix_secapproval_requests_revision_id"), table_name="secapproval_requests" - ) - op.drop_table("secapproval_requests") - op.drop_table("landing_job") - op.drop_table("diff_warning") - op.drop_table("configuration_variable") - # ### end Alembic commands ### diff --git a/migrations/versions/a8cd4d43c4c3_revision_landing_jobs.py b/migrations/versions/a8cd4d43c4c3_revision_landing_jobs.py deleted file mode 100644 index c0255f4e..00000000 --- a/migrations/versions/a8cd4d43c4c3_revision_landing_jobs.py +++ /dev/null @@ -1,93 +0,0 @@ -"""revision landing jobs - -Revision ID: a8cd4d43c4c3 -Revises: c5b4350e073b -Create Date: 2023-05-01 16:03:39.129725 - -""" -import sqlalchemy as sa -from alembic import op -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision = "a8cd4d43c4c3" -down_revision = "c5b4350e073b" -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( - "revision_landing_job", - sa.Column("landing_job_id", sa.Integer(), nullable=True), - sa.Column("revision_id", sa.Integer(), nullable=True), - sa.Column("index", sa.Integer(), nullable=True), - sa.ForeignKeyConstraint( - ["landing_job_id"], - ["landing_job.id"], - ), - sa.ForeignKeyConstraint( - ["revision_id"], - ["revision.id"], - ), - ) - op.alter_column( - "landing_job", - "status", - existing_type=postgresql.ENUM( - "SUBMITTED", - "IN_PROGRESS", - "DEFERRED", - "FAILED", - "LANDED", - "CANCELLED", - name="landingjobstatus", - ), - nullable=True, - ) - op.alter_column( - "landing_job", - "revision_to_diff_id", - existing_type=postgresql.JSONB(astext_type=sa.Text()), - nullable=True, - ) - op.alter_column( - "landing_job", - "revision_order", - existing_type=postgresql.JSONB(astext_type=sa.Text()), - nullable=True, - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column( - "landing_job", - "revision_order", - existing_type=postgresql.JSONB(astext_type=sa.Text()), - nullable=False, - ) - op.alter_column( - "landing_job", - "revision_to_diff_id", - existing_type=postgresql.JSONB(astext_type=sa.Text()), - nullable=False, - ) - op.alter_column( - "landing_job", - "status", - existing_type=postgresql.ENUM( - "SUBMITTED", - "IN_PROGRESS", - "DEFERRED", - "FAILED", - "LANDED", - "CANCELLED", - name="landingjobstatus", - ), - nullable=False, - ) - op.drop_table("revision_landing_job") - # ### end Alembic commands ### diff --git a/migrations/versions/c5b4350e073b_add_revision_model.py b/migrations/versions/c5b4350e073b_add_revision_model.py deleted file mode 100644 index 3b11c970..00000000 --- a/migrations/versions/c5b4350e073b_add_revision_model.py +++ /dev/null @@ -1,41 +0,0 @@ -"""add revision model - -Revision ID: c5b4350e073b -Revises: 7883d80258fb -Create Date: 2023-04-11 16:18:55.045882 - -""" -import sqlalchemy as sa -from alembic import op -from sqlalchemy.dialects import postgresql - -# revision identifiers, used by Alembic. -revision = "c5b4350e073b" -down_revision = "7883d80258fb" -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( - "revision", - sa.Column("id", sa.Integer(), nullable=False), - sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), - sa.Column("revision_id", sa.Integer(), nullable=False), - sa.Column("diff_id", sa.Integer(), nullable=False), - sa.Column("patch_bytes", sa.LargeBinary(), nullable=False), - sa.Column( - "patch_data", postgresql.JSONB(astext_type=sa.Text()), nullable=False - ), - sa.PrimaryKeyConstraint("id"), - sa.UniqueConstraint("revision_id"), - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table("revision") - # ### end Alembic commands ### diff --git a/src/lando/main/models/__init__.py b/src/lando/main/models/__init__.py new file mode 100644 index 00000000..9b4161f1 --- /dev/null +++ b/src/lando/main/models/__init__.py @@ -0,0 +1 @@ +from lando.main.models.base import * diff --git a/src/lando/main/models.py b/src/lando/main/models/base.py similarity index 100% rename from src/lando/main/models.py rename to src/lando/main/models/base.py diff --git a/landoapi/models/configuration.py b/src/lando/main/models/configuration.py similarity index 100% rename from landoapi/models/configuration.py rename to src/lando/main/models/configuration.py diff --git a/landoapi/models/landing_job.py b/src/lando/main/models/landing_job.py similarity index 100% rename from landoapi/models/landing_job.py rename to src/lando/main/models/landing_job.py diff --git a/landoapi/models/revisions.py b/src/lando/main/models/revision.py similarity index 100% rename from landoapi/models/revisions.py rename to src/lando/main/models/revision.py