Merge "Remove models usage from migrations"
This commit is contained in:
commit
09282e0e87
@ -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)})
|
||||
)
|
||||
|
21
manila/db/migrations/utils.py
Normal file
21
manila/db/migrations/utils.py
Normal 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)
|
30
manila/tests/db/migrations/test_utils.py
Normal file
30
manila/tests/db/migrations/test_utils.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user