From f43bf7baeb4088e9329af6a2b9a4a035471ba2bf Mon Sep 17 00:00:00 2001 From: Mike Fedosin Date: Mon, 22 Jul 2019 13:16:16 +0200 Subject: [PATCH] Allow to delete multiple objects with advanced filters This commit adds a possibility to delete db objects with advanced filters, like 'in', 'eq', 'has' and so on. Change-Id: I97bd3a5627f62081a2cb9487cc91920c5a11561b --- mistral/db/v2/sqlalchemy/api.py | 4 +++- .../unit/db/v2/test_sqlalchemy_db_api.py | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/mistral/db/v2/sqlalchemy/api.py b/mistral/db/v2/sqlalchemy/api.py index 25dc2c2c6..5a162cd40 100644 --- a/mistral/db/v2/sqlalchemy/api.py +++ b/mistral/db/v2/sqlalchemy/api.py @@ -259,7 +259,9 @@ def _delete_all(model, **kwargs): # NOTE(kong): Because we use 'in_' operator in _secure_query(), delete() # method will raise error with default parameter. Please refer to # http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html#sqlalchemy.orm.query.Query.delete - _secure_query(model).filter_by(**kwargs).delete(synchronize_session=False) + query = _secure_query(model) + query = db_filters.apply_filters(query, model, **kwargs) + query.delete(synchronize_session=False) def _get_collection(model, insecure=False, limit=None, marker=None, diff --git a/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py b/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py index d4cb92785..5a12d2ebe 100644 --- a/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py +++ b/mistral/tests/unit/db/v2/test_sqlalchemy_db_api.py @@ -2778,6 +2778,26 @@ class EnvironmentTest(SQLAlchemyTest): created.name ) + def test_delete_environments(self): + created0 = db_api.create_environment(ENVIRONMENTS[0]) + created1 = db_api.create_environment(ENVIRONMENTS[1]) + + db_api.delete_environments( + name={'in': [created0.name, created1.name]} + ) + + self.assertRaises( + exc.DBEntityNotFoundError, + db_api.get_environment, + created0.id + ) + + self.assertRaises( + exc.DBEntityNotFoundError, + db_api.get_environment, + created1.id + ) + def test_environment_repr(self): s = db_api.create_environment(ENVIRONMENTS[0]).__repr__()