diff --git a/redash/tasks/failure_report.py b/redash/tasks/failure_report.py index 3e543372..d763cc94 100644 --- a/redash/tasks/failure_report.py +++ b/redash/tasks/failure_report.py @@ -91,6 +91,7 @@ def track_failure(query, error): logging.debug(error) query.schedule_failures += 1 + query.skip_updated_at = True models.db.session.add(query) models.db.session.commit() diff --git a/redash/tasks/queries/execution.py b/redash/tasks/queries/execution.py index 463a5b78..dd7f2c6e 100644 --- a/redash/tasks/queries/execution.py +++ b/redash/tasks/queries/execution.py @@ -275,6 +275,7 @@ class QueryExecutor(object): self.scheduled_query, load=False ) self.scheduled_query.schedule_failures = 0 + self.scheduled_query.skip_updated_at = True models.db.session.add(self.scheduled_query) query_result = models.QueryResult.store_result( diff --git a/tests/tasks/test_queries.py b/tests/tasks/test_queries.py index 0fb3b605..a6de6dc4 100644 --- a/tests/tasks/test_queries.py +++ b/tests/tasks/test_queries.py @@ -1,12 +1,13 @@ from unittest import TestCase from collections import namedtuple import uuid +import datetime import mock from tests import BaseTestCase from redash import redis_connection, models -from redash.utils import json_dumps +from redash.utils import json_dumps, utcnow from redash.query_runner.pg import PostgreSQL from redash.tasks.queries.execution import ( QueryExecutionError, @@ -199,3 +200,27 @@ class QueryExecutorTests(BaseTestCase): ) q = models.Query.get_by_id(q.id) self.assertEqual(q.schedule_failures, 0) + + def test_doesnt_change_updated_at_timestamp(self): + cm = mock.patch("celery.app.task.Context.delivery_info", + {'routing_key': 'test'}) + + month_ago = utcnow() + datetime.timedelta(-30) + q = self.factory.create_query(query_text="SELECT 1, 2", schedule={"interval": 300}, updated_at=month_ago) + with cm, mock.patch.object(PostgreSQL, "run_query") as qr: + qr.side_effect = ValueError("broken") + with self.assertRaises(QueryExecutionError): + execute_query("SELECT 1, 2", self.factory.data_source.id, {}, + scheduled_query_id=q.id) + q = models.Query.get_by_id(q.id) + self.assertEqual(q.schedule_failures, 1) + self.assertEqual(q.updated_at, month_ago) + + with cm, mock.patch.object(PostgreSQL, "run_query") as qr: + qr.return_value = ([1, 2], None) + execute_query("SELECT 1, 2", + self.factory.data_source.id, {}, + scheduled_query_id=q.id) + q = models.Query.get_by_id(q.id) + self.assertEqual(q.schedule_failures, 0) + self.assertEqual(q.updated_at, month_ago)