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
This commit is contained in:
Jim Rollenhagen 2016-02-12 16:55:38 +00:00 committed by Vladyslav Drok
parent dab2fc347b
commit c53a649b24
2 changed files with 59 additions and 0 deletions

View File

@ -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}))

View File

@ -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')