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

Upgrade deps : sqlalchemy 2.0.0 major migration and others #501

Merged
merged 7 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
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
33 changes: 23 additions & 10 deletions iib/web/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import flask
import kombu
from flask_login import current_user, login_required
from sqlalchemy.orm import with_polymorphic
from sqlalchemy.orm import aliased, with_polymorphic
from sqlalchemy.sql import text
from sqlalchemy import or_
from werkzeug.exceptions import Forbidden, Gone, NotFound
Expand Down Expand Up @@ -396,24 +396,36 @@ def get_builds() -> flask.Response:
query = query.join(Request.user).filter(User.username == user)

if index_image:
# https://sqlalche.me/e/20/xaj2 - Create aliases for self-join (Sqlalchemy 2.0)
request_create_empty_index_alias = aliased(RequestCreateEmptyIndex, flat=True)
request_add_alias = aliased(RequestAdd, flat=True)
request_rm_alias = aliased(RequestRm, flat=True)
request_merge_index_image_alias = aliased(RequestMergeIndexImage, flat=True)

query_params['index_image'] = index_image
# Get the image id of the image to be searched
image_result = Image.query.filter_by(pull_specification=index_image).first()
if image_result:
# join with the Request* tables to get the response as image_ids are stored there
query = (
query.outerjoin(RequestCreateEmptyIndex, Request.id == RequestCreateEmptyIndex.id)
.outerjoin(RequestAdd, Request.id == RequestAdd.id)
.outerjoin(RequestMergeIndexImage, Request.id == RequestMergeIndexImage.id)
.outerjoin(RequestRm, Request.id == RequestRm.id)
query.outerjoin(
request_create_empty_index_alias,
Request.id == request_create_empty_index_alias.id,
)
.outerjoin(request_add_alias, Request.id == request_add_alias.id)
.outerjoin(
request_merge_index_image_alias,
Request.id == request_merge_index_image_alias.id,
)
.outerjoin(request_rm_alias, Request.id == request_rm_alias.id)
)

query = query.filter(
or_(
RequestCreateEmptyIndex.index_image_id == image_result.id,
RequestAdd.index_image_id == image_result.id,
RequestMergeIndexImage.index_image_id == image_result.id,
RequestRm.index_image_id == image_result.id,
request_create_empty_index_alias.index_image_id == image_result.id,
request_add_alias.index_image_id == image_result.id,
request_merge_index_image_alias.index_image_id == image_result.id,
request_rm_alias.index_image_id == image_result.id,
)
)
# if index_image is not found in image table, then raise an error
Expand Down Expand Up @@ -441,7 +453,8 @@ def get_healthcheck() -> flask.Response:
"""
# Test DB connection
try:
db.engine.execute(text('SELECT 1'))
with db.engine.connect() as connection:
connection.execute(text('SELECT 1'))
except Exception:
flask.current_app.logger.exception('DB test failed.')
raise IIBError('Database health check failed.')
Expand Down
1 change: 0 additions & 1 deletion iib/web/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def process_revision_directives(context, revision, directives):
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
render_as_batch=True,
**current_app.extensions['migrate'].configure_args,
)

Expand Down
49 changes: 21 additions & 28 deletions iib/web/migrations/versions/04dd7532d9c5_polymorphic_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
# Create references to the various tables used to migrate data during
# the upgrade and the downgrade processes.


# sqlalchemy 2.0: https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#migration-core-usage
# where clause parameter in select is not longer supported and list in select has been deprecated.
old_request_table = sa.Table(
'request',
sa.MetaData(),
Expand Down Expand Up @@ -216,24 +217,21 @@ def _upgrade_data():
'index_image_id',
],
select(
[
old_request_table.c.id,
old_request_table.c.organization,
old_request_table.c.binary_image_id,
old_request_table.c.binary_image_resolved_id,
old_request_table.c.from_index_id,
old_request_table.c.from_index_resolved_id,
old_request_table.c.index_image_id,
],
old_request_table.c.type == REQUEST_TYPE_ADD,
),
old_request_table.c.id,
old_request_table.c.organization,
old_request_table.c.binary_image_id,
old_request_table.c.binary_image_resolved_id,
old_request_table.c.from_index_id,
old_request_table.c.from_index_resolved_id,
old_request_table.c.index_image_id,
).where(old_request_table.c.type == REQUEST_TYPE_ADD),
)
)

connection.execute(
request_add_bundle_table.insert().from_select(
['request_add_id', 'image_id'],
select([request_bundle_table.c.request_id, request_bundle_table.c.image_id]),
select(request_bundle_table.c.request_id, request_bundle_table.c.image_id),
)
)

Expand All @@ -248,23 +246,20 @@ def _upgrade_data():
'index_image_id',
],
select(
[
old_request_table.c.id,
old_request_table.c.binary_image_id,
old_request_table.c.binary_image_resolved_id,
old_request_table.c.from_index_id,
old_request_table.c.from_index_resolved_id,
old_request_table.c.index_image_id,
],
old_request_table.c.type == REQUEST_TYPE_RM,
),
old_request_table.c.id,
old_request_table.c.binary_image_id,
old_request_table.c.binary_image_resolved_id,
old_request_table.c.from_index_id,
old_request_table.c.from_index_resolved_id,
old_request_table.c.index_image_id,
).where(old_request_table.c.type == REQUEST_TYPE_RM),
)
)

connection.execute(
request_rm_operator_table.insert().from_select(
['request_rm_id', 'operator_id'],
select([request_operator_table.c.request_id, request_operator_table.c.operator_id]),
select(request_operator_table.c.request_id, request_operator_table.c.operator_id),
)
)

Expand Down Expand Up @@ -372,9 +367,7 @@ def _downgrade_data():
connection.execute(
request_bundle_table.insert().from_select(
['request_id', 'image_id'],
select(
[request_add_bundle_table.c.request_add_id, request_add_bundle_table.c.image_id]
),
select(request_add_bundle_table.c.request_add_id, request_add_bundle_table.c.image_id),
)
)

Expand Down Expand Up @@ -402,7 +395,7 @@ def _downgrade_data():
request_operator_table.insert().from_select(
['request_id', 'operator_id'],
select(
[request_rm_operator_table.c.request_rm_id, request_rm_operator_table.c.operator_id]
request_rm_operator_table.c.request_rm_id, request_rm_operator_table.c.operator_id
),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

# Create references to the tables used to migrate data during the upgrade
# and the downgrade processes.

# sqlalchemy 2.0: https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#migration-core-usage
# where clause parameter in select is not longer supported and list in select has been deprecated.
request_table = sa.Table(
'request',
sa.MetaData(),
Expand Down Expand Up @@ -52,7 +55,7 @@ def downgrade():
# there are no records of that type in the database since the data loss is
# irreversible.
regenerate_bundle_requests = connection.execute(
sa.select([sa.func.count()])
sa.select(sa.func.count())
.select_from(request_table)
.where(request_table.c.type == REQUEST_TYPE_REGENERATE_BUNDLE)
).scalar()
Expand Down
Loading