Remove models usage from migrations

Models shouldn't be used in migrations because
they represents only latest state of appropriate
tables.

Closes-Bug: #1473885
Change-Id: Ic8313fda4e82232bb71212c4bd8d882b84770789
This commit is contained in:
Igor Malinovskiy 2015-07-13 11:57:51 +03:00
parent 1d00bfdfd2
commit 03ddb878d9
3 changed files with 64 additions and 9 deletions

View File

@ -28,26 +28,30 @@ down_revision = '533646c7af38'
from alembic import op
import sqlalchemy as sa
from manila.db.sqlalchemy import models
from manila.db.migrations import utils
def upgrade():
# NOTE(vponomaryov): shares has some statuses as uppercase, so
# transform them in addition to statuses of share servers.
for model in (models.Share, models.ShareServer):
_transform_case(model, make_upper=False)
for table in ('shares', 'share_servers'):
_transform_case(table, make_upper=False)
def downgrade():
# NOTE(vponomaryov): transform share server statuses to uppercase and
# leave share statuses as is.
_transform_case(models.ShareServer, make_upper=True)
_transform_case('share_servers', make_upper=True)
def _transform_case(model, make_upper):
def _transform_case(table_name, make_upper):
connection = op.get_bind()
session = sa.orm.Session(bind=connection.connect())
table = utils.load_table(table_name, connection)
case = sa.func.upper if make_upper else sa.func.lower
session.query(model).update(
{model.status: case(model.status)}, synchronize_session='fetch')
session.commit()
for row in connection.execute(table.select()):
op.execute(
table.update().where(
table.c.id == row.id
).values({'status': case(row.status)})
)

View File

@ -0,0 +1,21 @@
# Copyright 2015 Mirantis Inc.
# All Rights Reserved.
#
# 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.
import sqlalchemy as sa
def load_table(name, connection):
return sa.Table(name, sa.MetaData(), autoload=True,
autoload_with=connection)

View File

@ -0,0 +1,30 @@
# Copyright 2015 Mirantis Inc.
# All Rights Reserved.
#
# 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.
from manila.db.migrations import utils
from manila.db.sqlalchemy import api
from manila import test
class MigrationUtilsTestCase(test.TestCase):
def test_load_table(self):
connection = api.get_engine()
table_name = 'shares'
actual_result = utils.load_table(table_name, connection)
self.assertIsNotNone(actual_result)
self.assertEqual(table_name, actual_result.name)