From 712f693a72936e102c58a29344aee37e50f57cab Mon Sep 17 00:00:00 2001 From: Doug Szumski Date: Fri, 20 Dec 2019 15:23:03 +0000 Subject: [PATCH] Fix notification method type DB schema migration The Stein release does away with the concept of built in notifications, in favour of treating all notification types equally. This patch fixes an issue with the DB schema migration associated with this change, which will fail if any notifications using built-in notification types are configured at the time of the upgrade. Story: 2006984 Task: 37746 Change-Id: I2e3f08edf1ab6aec526ad93d04effb91ddca600a --- ...98bb7_remove_builtin_notification_types.py | 24 ++++++++++++++++--- ...ration-issue-2006984-6676bd3a8a34c9ae.yaml | 6 +++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix-db-migration-issue-2006984-6676bd3a8a34c9ae.yaml diff --git a/monasca_api/db/alembic/versions/26083b298bb7_remove_builtin_notification_types.py b/monasca_api/db/alembic/versions/26083b298bb7_remove_builtin_notification_types.py index ea5605a15..46c203db2 100644 --- a/monasca_api/db/alembic/versions/26083b298bb7_remove_builtin_notification_types.py +++ b/monasca_api/db/alembic/versions/26083b298bb7_remove_builtin_notification_types.py @@ -22,6 +22,8 @@ from alembic import op import sqlalchemy as sa from sqlalchemy.sql import table +_NM_BUILT_IN_TYPES = set(['EMAIL', 'WEBHOOK', 'PAGERDUTY']) + # revision identifiers, used by Alembic. revision = '26083b298bb7' down_revision = 'f69cb3152a76' @@ -34,13 +36,29 @@ _nm_types = table( sa.String(length=20), nullable=False)) +_nm = table( + 'notification_method', + sa.Column('type', + sa.String(length=20), + nullable=False)) + def upgrade(): # Built-in notification types have been removed. Here, we - # remove them and rely on monasca_notification to re-populate - # the table according to what is set in its config file. + # remove them (where not in use) and rely on Monasca Notification + # to re-populate the table according to what is set in its config file. + + # Start by creating a set of all notification method types currently + # configured in the Monasca DB + connection = op.get_bind() + nm_types_configured = connection.execute(_nm.select()).fetchall() + nm_types_configured = set([nm_type[0] for nm_type in nm_types_configured]) + + # Remove all built in notification types which are currently *not* + # configured. + nm_types_to_remove = _NM_BUILT_IN_TYPES.difference(nm_types_configured) op.execute(_nm_types.delete().where( - _nm_types.c.name.in_(('EMAIL', 'WEBHOOK', 'PAGERDUTY')))) + _nm_types.c.name.in_(nm_types_to_remove))) def downgrade(): diff --git a/releasenotes/notes/fix-db-migration-issue-2006984-6676bd3a8a34c9ae.yaml b/releasenotes/notes/fix-db-migration-issue-2006984-6676bd3a8a34c9ae.yaml new file mode 100644 index 000000000..a7f1a761c --- /dev/null +++ b/releasenotes/notes/fix-db-migration-issue-2006984-6676bd3a8a34c9ae.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + An issue with migrating the DB schema in the Stein release + which can cause an upgrade to fail. See `story + 2006984 `__