Change logging of sql to single line

Cleans up Shipyard logs a bit, where queries were nice and easy to read,
but multi-line and needlessly on a separate line from the logging
headers.

Change-Id: I2fdff634dad097ef30207edae5205cb6c7226602
This commit is contained in:
Bryan Strassner 2018-08-06 13:57:24 -05:00
parent 205e1b1b37
commit 1a1cad60ed
3 changed files with 63 additions and 2 deletions

View File

@ -109,7 +109,7 @@ class DbAccess:
executes the supplied query and returns the array of dictionaries of
the row results
"""
LOG.debug('Query: %s', query)
LOG.debug('Query: %s', _query_single_line(query))
result_dict_list = []
if query is not None:
with self.get_engine().connect() as connection:
@ -142,7 +142,15 @@ class DbAccess:
"""
Performs an update/insert/delete
"""
LOG.debug('Query: %s', query)
LOG.debug('Query: %s', _query_single_line(query))
if query is not None:
with self.get_engine().connect() as connection:
return connection.execute(query, **kwargs)
def _query_single_line(query):
"""Reformats a query string to remove newlines and extra spaces
:param query: The query string to log
"""
return " ".join(query.split())

View File

@ -0,0 +1,53 @@
# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# 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 shipyard_airflow.db import common_db
M_QUERY = """
SELECT
junk_id,
junk_name
FROM
junk_table
WHERE
junk_name = "Junk"
"""
M_RES = 'SELECT junk_id, junk_name FROM junk_table WHERE junk_name = "Junk"'
M2_QUERY = """
SELECT
things
FROM
tables
"""
M2_RES = 'SELECT things FROM tables'
S_QUERY = "SELECT 1 FROM dual"
class TestCommonDb():
def test_single_line_query(self):
assert M_RES == common_db._query_single_line(M_QUERY)
assert M2_RES == common_db._query_single_line(M2_QUERY)
assert S_QUERY == common_db._query_single_line(S_QUERY)