Skip to content

Commit

Permalink
Rename BundleDeprecation to RequestMergeBundleDeprecation
Browse files Browse the repository at this point in the history
Refers to CLOUDDST-4906
  • Loading branch information
yashvardhannanavati authored and lcarva committed Feb 10, 2021
1 parent a6140f3 commit 7b8a99b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Rename bundle deprecation association for merge index.
Revision ID: 7573241a5156
Revises: eec630370e68
Create Date: 2021-02-09 13:50:01.905796
"""
from alembic import op


# revision identifiers, used by Alembic.
revision = '7573241a5156'
down_revision = 'eec630370e68'
branch_labels = None
depends_on = None


def upgrade():

with op.batch_alter_table('bundle_deprecation', schema=None) as batch_op:
batch_op.drop_index('ix_bundle_deprecation_bundle_id')
batch_op.drop_index('ix_bundle_deprecation_merge_index_image_id')

op.rename_table('bundle_deprecation', 'request_merge_bundle_deprecation', schema=None)

with op.batch_alter_table('request_merge_bundle_deprecation', schema=None) as batch_op:
batch_op.create_index(
batch_op.f('ix_request_merge_bundle_deprecation_bundle_id'), ['bundle_id'], unique=False
)
batch_op.create_index(
batch_op.f('ix_request_merge_bundle_deprecation_merge_index_image_id'),
['merge_index_image_id'],
unique=False,
)


def downgrade():
with op.batch_alter_table('request_merge_bundle_deprecation', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_request_merge_bundle_deprecation_merge_index_image_id'))
batch_op.drop_index(batch_op.f('ix_request_merge_bundle_deprecation_bundle_id'))

op.rename_table('request_merge_bundle_deprecation', 'bundle_deprecation', schema=None)

with op.batch_alter_table('bundle_deprecation', schema=None) as batch_op:
batch_op.create_index(
'ix_bundle_deprecation_merge_index_image_id', ['merge_index_image_id'], unique=False
)
batch_op.create_index('ix_bundle_deprecation_bundle_id', ['bundle_id'], unique=False)
4 changes: 2 additions & 2 deletions iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def pretty(cls, num):
return cls(num).name.replace('_', '-')


class BundleDeprecation(db.Model):
class RequestMergeBundleDeprecation(db.Model):
"""An association table between index merge requests and bundle images which they deprecate."""

# A primary key is required by SQLAlchemy when using declaritive style tables, so a composite
Expand Down Expand Up @@ -1138,7 +1138,7 @@ class RequestMergeIndexImage(Request):
'Image', foreign_keys=[binary_image_resolved_id], uselist=False
)

deprecation_list = db.relationship('Image', secondary=BundleDeprecation.__table__)
deprecation_list = db.relationship('Image', secondary=RequestMergeBundleDeprecation.__table__)

index_image_id = db.Column(db.Integer, db.ForeignKey('image.id'))
index_image = db.relationship('Image', foreign_keys=[index_image_id], uselist=False)
Expand Down
54 changes: 50 additions & 4 deletions tests/test_web/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import flask_migrate
import pytest

from iib.web.models import RequestAdd, RequestRegenerateBundle, RequestRm
from iib.web.models import RequestAdd, RequestMergeIndexImage, RequestRegenerateBundle, RequestRm


INITIAL_DB_REVISION = '274ba38408e8'
Expand All @@ -17,35 +17,81 @@ def test_migrate_to_polymorphic_requests(app, auth_env, client, db):
with app.test_request_context(environ_base=auth_env):
# Generate some data to verify migration
for i in range(total_requests):
if random.choice((True, False)):
request_class = random.choice((RequestAdd, RequestRm))
if request_class == RequestAdd:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
else:
elif request_class == RequestRm:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'operators': [f'operator-{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestRm.from_json(data)

if i % 5 == 0:
# Simulate failed request
request.add_state('failed', 'Failed due to an unknown error')
db.session.add(request)
db.session.commit()

expected_rv_json = client.get(f'/api/v1/builds?per_page={total_requests}&verbose=true').json

flask_migrate.downgrade(revision=INITIAL_DB_REVISION)
flask_migrate.upgrade()

actual_rv_json = client.get(f'/api/v1/builds?per_page={total_requests}&verbose=true').json
assert expected_rv_json == actual_rv_json


def test_migrate_to_merge_index_endpoints(app, auth_env, client, db):
merge_index_revision = '4c9db41195ec'
total_requests = 20
# flask_login.current_user is used in RequestAdd.from_json and RequestRm.from_json,
# which requires a request context
with app.test_request_context(environ_base=auth_env):
# Generate some data to verify migration
for i in range(total_requests):
request_class = random.choice((RequestAdd, RequestMergeIndexImage, RequestRm))
if request_class == RequestAdd:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'bundles': [f'quay.io/namespace/bundle:{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestAdd.from_json(data)
elif request_class == RequestRm:
data = {
'binary_image': 'quay.io/namespace/binary_image:latest',
'operators': [f'operator-{i}'],
'from_index': f'quay.io/namespace/repo:{i}',
}
request = RequestRm.from_json(data)
elif request_class == RequestMergeIndexImage:
data = {
'source_from_index': f'quay.io/namespace/repo:{i}',
'target_index': f'quay.io/namespace/repo:{i}',
'binary_image': 'quay.io/namespace/binary_image:latest',
}
request = RequestMergeIndexImage.from_json(data)

if i % 5 == 0:
# Simulate failed request
request.add_state('failed', 'Failed due to an unknown error')
db.session.add(request)
db.session.commit()

expected_rv_json = client.get(f'/api/v1/builds?per_page={total_requests}&verbose=true').json
flask_migrate.downgrade(revision=merge_index_revision)
flask_migrate.upgrade()

actual_rv_json = client.get(f'/api/v1/builds?per_page={total_requests}&verbose=true').json
assert expected_rv_json == actual_rv_json


def test_abort_when_downgrading_from_regenerate_bundle_request(app, auth_env, client, db):
"""Verify downgrade is prevented if "regenerate-bundle" requests exist."""
total_requests = 20
Expand Down

0 comments on commit 7b8a99b

Please sign in to comment.