From 9f34d34d0ab92400223e3cc87285d0d4fbdf21f9 Mon Sep 17 00:00:00 2001 From: John Griffith Date: Fri, 19 Dec 2014 15:14:22 -0700 Subject: [PATCH] Add a provider_id column to Volumes and Snapshots There are a number of cases where there's some really ugly mapping work in drivers to try and map the backend device-id to the Cinder ID of a Volume or Snapshot. Most drivers seem to work around this in their own creative ways using their own mechanisms (metadata, naming schemes etc). It seems like it would be useful for a number of reasons however to go ahead and add a simple column to the Volumes table that could be used for storing a backend ID for the Volume and Snapshot. Change-Id: Id07cdd1e03feac71ff34325d5abc68c5adc48266 --- .../versions/035_add_provider_id_column.py | 35 +++++++++++++++++++ ...036_add_provider_id_column_to_snapshots.py | 35 +++++++++++++++++++ cinder/db/sqlalchemy/models.py | 2 ++ cinder/tests/test_migrations.py | 18 ++++++++++ 4 files changed, 90 insertions(+) create mode 100644 cinder/db/sqlalchemy/migrate_repo/versions/035_add_provider_id_column.py create mode 100644 cinder/db/sqlalchemy/migrate_repo/versions/036_add_provider_id_column_to_snapshots.py diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/035_add_provider_id_column.py b/cinder/db/sqlalchemy/migrate_repo/versions/035_add_provider_id_column.py new file mode 100644 index 000000000..e0a1ce836 --- /dev/null +++ b/cinder/db/sqlalchemy/migrate_repo/versions/035_add_provider_id_column.py @@ -0,0 +1,35 @@ +# 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 sqlalchemy import Column +from sqlalchemy import MetaData, String, Table + + +def upgrade(migrate_engine): + """Add provider_id column to volumes.""" + meta = MetaData() + meta.bind = migrate_engine + + volumes = Table('volumes', meta, autoload=True) + provider_id = Column('provider_id', String(255)) + volumes.create_column(provider_id) + volumes.update().values(provider_id=None).execute() + + +def downgrade(migrate_engine): + """Remove provider_id column from volumes.""" + meta = MetaData() + meta.bind = migrate_engine + + volumes = Table('volumes', meta, autoload=True) + provider_id = volumes.columns.provider_id + volumes.drop_column(provider_id) diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/036_add_provider_id_column_to_snapshots.py b/cinder/db/sqlalchemy/migrate_repo/versions/036_add_provider_id_column_to_snapshots.py new file mode 100644 index 000000000..7698ec8eb --- /dev/null +++ b/cinder/db/sqlalchemy/migrate_repo/versions/036_add_provider_id_column_to_snapshots.py @@ -0,0 +1,35 @@ +# 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 sqlalchemy import Column +from sqlalchemy import MetaData, String, Table + + +def upgrade(migrate_engine): + """Add provider_id column to snapshots.""" + meta = MetaData() + meta.bind = migrate_engine + + snapshots = Table('snapshots', meta, autoload=True) + provider_id = Column('provider_id', String(255)) + snapshots.create_column(provider_id) + snapshots.update().values(provider_id=None).execute() + + +def downgrade(migrate_engine): + """Remove provider_id column from snapshots.""" + meta = MetaData() + meta.bind = migrate_engine + + snapshots = Table('snapshots', meta, autoload=True) + provider_id = snapshots.columns.provider_id + snapshots.drop_column(provider_id) diff --git a/cinder/db/sqlalchemy/models.py b/cinder/db/sqlalchemy/models.py index 5667f119e..73f960a33 100644 --- a/cinder/db/sqlalchemy/models.py +++ b/cinder/db/sqlalchemy/models.py @@ -146,6 +146,7 @@ class Volume(BASE, CinderBase): provider_location = Column(String(255)) provider_auth = Column(String(255)) provider_geometry = Column(String(255)) + provider_id = Column(String(255)) volume_type_id = Column(String(36)) source_volid = Column(String(36)) @@ -431,6 +432,7 @@ class Snapshot(BASE, CinderBase): volume_type_id = Column(String(36)) provider_location = Column(String(255)) + provider_id = Column(String(255)) volume = relationship(Volume, backref="snapshots", foreign_keys=volume_id, diff --git a/cinder/tests/test_migrations.py b/cinder/tests/test_migrations.py index 1cd8c42d7..739090c12 100644 --- a/cinder/tests/test_migrations.py +++ b/cinder/tests/test_migrations.py @@ -704,6 +704,24 @@ class MigrationsMixin(test_migrations.WalkVersionsMixin): volume_types = db_utils.get_table(engine, 'volume_types') self.assertNotIn('description', volume_types.c) + def _check_035(self, engine, data): + volumes = db_utils.get_table(engine, 'volumes') + self.assertIsInstance(volumes.c.provider_id.type, + sqlalchemy.types.VARCHAR) + + def _post_downgrade_035(self, engine): + volumes = db_utils.get_table(engine, 'volumes') + self.assertNotIn('provider_id', volumes.c) + + def _check_036(self, engine, data): + snapshots = db_utils.get_table(engine, 'snapshots') + self.assertIsInstance(snapshots.c.provider_id.type, + sqlalchemy.types.VARCHAR) + + def _post_downgrade_036(self, engine): + snapshots = db_utils.get_table(engine, 'snapshots') + self.assertNotIn('provider_id', snapshots.c) + def test_walk_versions(self): self.walk_versions(True, False) -- 2.45.2