Fix sqlalchemy deprecation error

Running unit tests fails with the error:
monasca_api.tests.test_a_repository.TestAlarmRepoDB.test_should_count ...FAILED
'Textual column expression 'metric_name' should be explicitly declared with
text('metric_name'), or use column('metric_name') for more specificity'
Using strings as SQL fragments was deprecated in v1.0:
(https://docs.sqlalchemy.org/en/13/changelog/migration_10.html#warnings-emitted-when-coercing-full-sql-fragments-into-text)
Deprecation warnings now throw exceptions as of v1.3:
(https://docs.sqlalchemy.org/en/13/changelog/migration_13.html#deprecation-warnings-are-emitted-for-all-deprecated-elements-new-deprecations-added)

This patch modifies the sql query to make use of column constructor.

Change-Id: Ic258a3f453e95844249eaa763aa507d6be0d132e
This commit is contained in:
Isaac Prior 2019-06-14 15:16:19 +01:00
parent 45a5ab167b
commit 8eea21c00d
1 changed files with 2 additions and 2 deletions

View File

@ -22,7 +22,7 @@ from monasca_api.common.repositories import alarms_repository
from monasca_api.common.repositories import exceptions
from monasca_api.common.repositories.sqla import models
from monasca_api.common.repositories.sqla import sql_repository
from sqlalchemy import (MetaData, update, delete, select, text,
from sqlalchemy import (MetaData, update, delete, select, text, column,
bindparam, func, literal_column, asc, desc)
from sqlalchemy import or_
@ -492,7 +492,7 @@ class AlarmsRepository(sql_repository.SQLRepository,
query_from = query_from.join(sub_query, sub_query.c.alarm_id == a.c.id)
query_columns = [func.count().label('count')]
query_columns.extend(group_by_columns)
query_columns.extend([column(col) for col in group_by_columns])
query = (select(query_columns)
.select_from(query_from)