Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squash alembic migrations #17607

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
from unittest import mock

import alembic.command
import alembic.script.base
import click.testing
import pretend
import pyramid.testing
@@ -307,6 +308,61 @@ def mock_manifest_cache_buster():
return MockManifestCacheBuster


def run_migrations(cfg):
def load_revisions(self):
migration_dir = _HERE.parent / "warehouse" / "migrations"

snapshots_rev = set()
for file_path in alembic.script.base.Script._list_py_dir(
self, (migration_dir / "snapshot").as_posix()
):
script = alembic.script.base.Script._from_path(self, file_path)
if script is None:
continue
snapshots_rev.add(script.revision)
yield script

pruned_revisions = set()

scripts_rev = {}
for file_path in alembic.script.base.Script._list_py_dir(
self, (migration_dir / "versions").as_posix()
):
script = alembic.script.base.Script._from_path(self, file_path)
if script is None:
continue

scripts_rev[script.revision] = script
if snapshot_rev := getattr(script.module, "snapshot_revision", None):
pruned_revisions.add(script.down_revision)
script.down_revision = snapshot_rev

while pruned_revisions:
rev = pruned_revisions.pop()
if rev not in scripts_rev:
continue

script = scripts_rev[rev]
if isinstance(script.down_revision, str):
pruned_revisions.add(script.down_revision)
elif script.down_revision:
pruned_revisions.update(set(script.down_revision))

del scripts_rev[rev]

for script in scripts_rev.values():
yield script

with mock.patch.object(
alembic.script.base.ScriptDirectory, "_load_revisions", load_revisions
):

# Run migrations:
# This might harmlessly run multiple times if there are several app
# config fixtures in the test session, using the same database.
alembic.command.upgrade(cfg.alembic_config(), "head")


def get_app_config(database, nondefaults=None):
settings = {
"warehouse.prevent_esi": True,
@@ -352,10 +408,7 @@ def get_app_config(database, nondefaults=None):
with mock.patch.object(static, "whitenoise_add_manifest"):
cfg = config.configure(settings=settings)

# Run migrations:
# This might harmlessly run multiple times if there are several app config fixtures
# in the test session, using the same database.
alembic.command.upgrade(cfg.alembic_config(), "head")
run_migrations(cfg)

return cfg

2,930 changes: 2,930 additions & 0 deletions warehouse/migrations/snapshot/dde46c596a6e_feb_2025.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
terms of service engagements
Revision ID: 2f5dbc74c770
Revises: 6cac7b706953
Revises: b2f6cb22fd84
Create Date: 2025-02-20 13:35:44.331611
"""

@@ -25,7 +25,7 @@
from warehouse.utils.db.types import TZDateTime

revision = "2f5dbc74c770"
down_revision = "6cac7b706953"
down_revision = "b2f6cb22fd84"


def upgrade():
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Merging point for Feb 2025 test
Revision ID: b2f6cb22fd84
Revises: 6cac7b706953
Create Date: 2025-02-27 18:52:47.817882
"""
revision = "b2f6cb22fd84"

# This migration is a reconciliation point between either the snapshot fast track and
# the classic migration path.
down_revision = "6cac7b706953"
snapshot_revision = "dde46c596a6e"


def upgrade():
pass


def downgrade():
pass