From c53a649b24f5ff4d0370f4548bc4907871561e1a Mon Sep 17 00:00:00 2001 From: Jim Rollenhagen Date: Fri, 12 Feb 2016 16:55:38 +0000 Subject: [PATCH] Set existing ports pxe_enabled=True when adding pxe_enabled column The `default` parameter to Column() doesn't have any effect on existing rows, therefore when this migration runs it is set to NULL. After adding the column, update existing ports in the database to have pxe_enabled=True, which maintains behavior before this column was added. Closes-Bug: #1545068 Change-Id: Ibee65fbe26d89d27925a2202795d38461ec60f1f --- .../f6fdb920c182_set_pxe_enabled_true.py | 38 +++++++++++++++++++ .../unit/db/sqlalchemy/test_migrations.py | 21 ++++++++++ 2 files changed, 59 insertions(+) create mode 100644 ironic/db/sqlalchemy/alembic/versions/f6fdb920c182_set_pxe_enabled_true.py diff --git a/ironic/db/sqlalchemy/alembic/versions/f6fdb920c182_set_pxe_enabled_true.py b/ironic/db/sqlalchemy/alembic/versions/f6fdb920c182_set_pxe_enabled_true.py new file mode 100644 index 0000000000..1dc5e28236 --- /dev/null +++ b/ironic/db/sqlalchemy/alembic/versions/f6fdb920c182_set_pxe_enabled_true.py @@ -0,0 +1,38 @@ +# 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. + +"""Set Port.pxe_enabled to True if NULL + +Revision ID: f6fdb920c182 +Revises: 5ea1b0d310e +Create Date: 2016-02-12 16:53:21.008580 + +""" + +# revision identifiers, used by Alembic. +revision = 'f6fdb920c182' +down_revision = '5ea1b0d310e' + +from alembic import op +from sqlalchemy import Boolean, String +from sqlalchemy.sql import table, column, null + +port = table('ports', + column('uuid', String(36)), + column('pxe_enabled', Boolean())) + + +def upgrade(): + op.execute( + port.update().where( + port.c.pxe_enabled == null()).values( + {'pxe_enabled': True})) diff --git a/ironic/tests/unit/db/sqlalchemy/test_migrations.py b/ironic/tests/unit/db/sqlalchemy/test_migrations.py index e46a9672b8..9d89b232e9 100644 --- a/ironic/tests/unit/db/sqlalchemy/test_migrations.py +++ b/ironic/tests/unit/db/sqlalchemy/test_migrations.py @@ -393,6 +393,27 @@ class MigrationCheckersMixin(object): isinstance(ports.c.pxe_enabled.type, sqlalchemy.types.Integer)) + def _pre_upgrade_f6fdb920c182(self, engine): + # add some ports. + ports = db_utils.get_table(engine, 'ports') + data = [{'uuid': uuidutils.generate_uuid(), 'pxe_enabled': None}, + {'uuid': uuidutils.generate_uuid(), 'pxe_enabled': None}] + ports.insert().values(data).execute() + return data + + def _check_f6fdb920c182(self, engine, data): + ports = db_utils.get_table(engine, 'ports') + result = engine.execute(ports.select()) + + def _was_inserted(uuid): + for row in data: + if row['uuid'] == uuid: + return True + + for row in result: + if _was_inserted(row['uuid']): + self.assertTrue(row['pxe_enabled']) + def test_upgrade_and_version(self): with patch_with_engine(self.engine): self.migration_api.upgrade('head')