From 5f329a912ec83f75f23a7bc55457fd4e51ce1ddf Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Thu, 20 Feb 2014 18:41:33 +0100 Subject: [PATCH] Rename id to alarm_id of Alarm in SqlAlchemy This change makes the schema more consistent. It also fixes a bug preventig querying alarm_id on /v2/query/alarms API. Closes-Bug: #1282667 Change-Id: Id4d67fa5ee679fe1c8d86b8575a99bfb89e384fa --- ceilometer/storage/impl_sqlalchemy.py | 10 +++---- .../versions/033_alarm_id_rename.py | 28 +++++++++++++++++++ ceilometer/storage/sqlalchemy/models.py | 2 +- .../tests/storage/test_storage_scenarios.py | 10 +++++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 ceilometer/storage/sqlalchemy/migrate_repo/versions/033_alarm_id_rename.py diff --git a/ceilometer/storage/impl_sqlalchemy.py b/ceilometer/storage/impl_sqlalchemy.py index 6a491c729..725b4f14e 100644 --- a/ceilometer/storage/impl_sqlalchemy.py +++ b/ceilometer/storage/impl_sqlalchemy.py @@ -796,7 +796,7 @@ class Connection(base.Connection): @staticmethod def _row_to_alarm_model(row): - return api_models.Alarm(alarm_id=row.id, + return api_models.Alarm(alarm_id=row.alarm_id, enabled=row.enabled, type=row.type, name=row.name, @@ -841,7 +841,7 @@ class Connection(base.Connection): if project is not None: query = query.filter(models.Alarm.project_id == project) if alarm_id is not None: - query = query.filter(models.Alarm.id == alarm_id) + query = query.filter(models.Alarm.alarm_id == alarm_id) return self._retrieve_alarms(query) @@ -852,7 +852,7 @@ class Connection(base.Connection): """ session = self._get_db_session() with session.begin(): - alarm_row = models.Alarm(id=alarm.alarm_id) + alarm_row = models.Alarm(alarm_id=alarm.alarm_id) alarm_row.update(alarm.as_dict()) session.add(alarm_row) @@ -869,7 +869,7 @@ class Connection(base.Connection): alarm.user_id) Connection._create_or_update(session, models.Project, alarm.project_id) - alarm_row = session.merge(models.Alarm(id=alarm.alarm_id)) + alarm_row = session.merge(models.Alarm(alarm_id=alarm.alarm_id)) alarm_row.update(alarm.as_dict()) return self._row_to_alarm_model(alarm_row) @@ -882,7 +882,7 @@ class Connection(base.Connection): session = self._get_db_session() with session.begin(): session.query(models.Alarm).filter( - models.Alarm.id == alarm_id).delete() + models.Alarm.alarm_id == alarm_id).delete() @staticmethod def _row_to_alarm_change_model(row): diff --git a/ceilometer/storage/sqlalchemy/migrate_repo/versions/033_alarm_id_rename.py b/ceilometer/storage/sqlalchemy/migrate_repo/versions/033_alarm_id_rename.py new file mode 100644 index 000000000..4eb795f93 --- /dev/null +++ b/ceilometer/storage/sqlalchemy/migrate_repo/versions/033_alarm_id_rename.py @@ -0,0 +1,28 @@ +# -*- encoding: utf-8 -*- +# +# 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 MetaData +from sqlalchemy import Table + + +def upgrade(migrate_engine): + meta = MetaData(bind=migrate_engine) + users = Table('alarm', meta, autoload=True) + users.c.id.alter(name='alarm_id') + + +def downgrade(migrate_engine): + meta = MetaData(bind=migrate_engine) + users = Table('alarm', meta, autoload=True) + users.c.alarm_id.alter(name='id') diff --git a/ceilometer/storage/sqlalchemy/models.py b/ceilometer/storage/sqlalchemy/models.py index c3ad5c9a4..b79507f02 100644 --- a/ceilometer/storage/sqlalchemy/models.py +++ b/ceilometer/storage/sqlalchemy/models.py @@ -290,7 +290,7 @@ class Alarm(Base): Index('ix_alarm_user_id', 'user_id'), Index('ix_alarm_project_id', 'project_id'), ) - id = Column(String(255), primary_key=True) + alarm_id = Column(String(255), primary_key=True) enabled = Column(Boolean) name = Column(Text) type = Column(String(50)) diff --git a/ceilometer/tests/storage/test_storage_scenarios.py b/ceilometer/tests/storage/test_storage_scenarios.py index 726bc9c4b..8f4c24cdc 100644 --- a/ceilometer/tests/storage/test_storage_scenarios.py +++ b/ceilometer/tests/storage/test_storage_scenarios.py @@ -2550,6 +2550,16 @@ class ComplexAlarmQueryTest(AlarmTestBase, self.assertIn(a.name, set(["yellow-alert", "red-alert"])) self.assertTrue(a.enabled) + def test_filter_for_alarm_id(self): + self.add_some_alarms() + filter_expr = {"=": {"alarm_id": "0r4ng3"}} + + result = list(self.conn.query_alarms(filter_expr=filter_expr)) + + self.assertEqual(1, len(result)) + for a in result: + self.assertEqual(a.alarm_id, "0r4ng3") + def test_filter_and_orderby(self): self.add_some_alarms() result = list(self.conn.query_alarms(filter_expr={"=":