From 59649f6a81cf51cadfee39ef86301256282a8e40 Mon Sep 17 00:00:00 2001
From: licanwei
Date: Mon, 19 Jun 2017 16:13:33 +0800
Subject: [PATCH] Fix get_action_plan_list filter error
When using 'query.filter_by(deleted_at=None)'
Will generate the incorrect SQL 'audits.deleted_at', as below:
SELECT * FROM action_plans INNER JOIN audits ON audits.id =
action_plans.audit_id WHERE audits.uuid = '' AND
audits.deleted_at IS NULL ORDER BY action_plans.id ASC
The correct filter field is 'action_plans.deleted_at'
Change-Id: Ie05f35233f78e82bc7af2c26c9effd62ea5f86ab
Closes-Bug: #1698720
---
watcher/db/sqlalchemy/api.py | 2 +-
watcher/tests/db/test_action_plan.py | 8 ++++++++
2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/watcher/db/sqlalchemy/api.py b/watcher/db/sqlalchemy/api.py
index fba1afbde..ebe919773 100644
--- a/watcher/db/sqlalchemy/api.py
+++ b/watcher/db/sqlalchemy/api.py
@@ -815,7 +815,7 @@ class Connection(api.BaseConnection):
query = self._set_eager_options(models.ActionPlan, query)
query = self._add_action_plans_filters(query, filters)
if not context.show_deleted:
- query = query.filter_by(deleted_at=None)
+ query = query.filter(models.ActionPlan.deleted_at.is_(None))
return _paginate_query(models.ActionPlan, limit, marker,
sort_key, sort_dir, query)
diff --git a/watcher/tests/db/test_action_plan.py b/watcher/tests/db/test_action_plan.py
index 7569990bc..94e5757b0 100644
--- a/watcher/tests/db/test_action_plan.py
+++ b/watcher/tests/db/test_action_plan.py
@@ -307,6 +307,14 @@ class DbActionPlanTestCase(base.DbTestCase):
for r in res:
self.assertEqual(audit['id'], r.audit_id)
+ self.dbapi.soft_delete_action_plan(action_plan1['uuid'])
+ res = self.dbapi.get_action_plan_list(
+ self.context,
+ filters={'audit_uuid': audit['uuid']})
+
+ self.assertEqual([action_plan2['id']], [r.id for r in res])
+ self.assertNotEqual([action_plan1['id']], [r.id for r in res])
+
def test_get_action_plan_list_with_filter_by_uuid(self):
action_plan = self._create_test_action_plan()
res = self.dbapi.get_action_plan_list(