Skip to content

Commit 8e7ac42

Browse files
authored
Upstream 16522 - Refactor formatted raw SQL in unified_jobs result_stdout_raw_handle (#587)
1 parent 4a6701a commit 8e7ac42

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

awx/main/models/unified_jobs.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from django.utils.encoding import smart_str
2424
from django.contrib.contenttypes.models import ContentType
2525

26+
# psycopg
27+
from psycopg import sql
28+
2629
# REST Framework
2730
from rest_framework.exceptions import ParseError
2831

@@ -1141,17 +1144,23 @@ def result_stdout_raw_handle(self, enforce_max_bytes=True):
11411144
raise StdoutMaxBytesExceeded(total, max_supported)
11421145

11431146
tbl = self._meta.db_table + 'event'
1144-
created_by_cond = ''
1147+
where_parts = [
1148+
sql.SQL('{} = {}').format(sql.Identifier(self.event_parent_key), sql.Literal(self.id)),
1149+
sql.SQL("stdout != ''"),
1150+
]
11451151
if self.has_unpartitioned_events:
1146-
tbl = f'_unpartitioned_{tbl}'
1152+
tbl = '_unpartitioned_' + tbl
11471153
else:
1148-
created_by_cond = f"job_created='{self.created.isoformat()}' AND "
1154+
where_parts.insert(0, sql.SQL('job_created = {}').format(sql.Literal(self.created)))
11491155

1150-
sql = f"copy (select stdout from {tbl} where {created_by_cond}{self.event_parent_key}={self.id} and stdout != '' order by start_line) to stdout" # nosql
1156+
copy_sql = sql.SQL('COPY (SELECT stdout FROM {} WHERE {} ORDER BY start_line) TO STDOUT').format(
1157+
sql.Identifier(tbl),
1158+
sql.SQL(' AND ').join(where_parts),
1159+
)
11511160
# psycopg3's copy writes bytes, but callers of this
11521161
# function assume a str-based fd will be returned; decode
11531162
# .write() calls on the fly to maintain this interface
1154-
with cursor.copy(sql) as copy:
1163+
with cursor.copy(copy_sql) as copy:
11551164
while data := copy.read():
11561165
fd.write(smart_str(bytes(data)))
11571166

awx/main/tests/functional/conftest.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -790,14 +790,13 @@ class MockCopy:
790790
events = []
791791
index = -1
792792

793-
def __init__(self, sql):
793+
def __init__(self):
794794
self.events = []
795-
parts = sql.split(' ')
796-
tablename = parts[parts.index('from') + 1]
797795
for cls in (JobEvent, AdHocCommandEvent, ProjectUpdateEvent, InventoryUpdateEvent, SystemJobEvent):
798-
if cls._meta.db_table == tablename:
799-
for event in cls.objects.order_by('start_line').all():
800-
self.events.append(event.stdout)
796+
events = list(cls.objects.order_by('start_line').values_list('stdout', flat=True))
797+
if events:
798+
self.events = events
799+
break
801800

802801
def read(self):
803802
self.index = self.index + 1
@@ -818,9 +817,8 @@ def sqlite_copy(request, mocker):
818817
# copy is postgres-specific, and SQLite doesn't support it; mock its
819818
# behavior to test that it writes a file that contains stdout from events
820819

821-
def write_stdout(self, sql):
822-
mock_copy = MockCopy(sql)
823-
return mock_copy
820+
def write_stdout(self, sql, params=None):
821+
return MockCopy()
824822

825823
mocker.patch.object(SQLiteCursorWrapper, 'copy', write_stdout, create=True)
826824

0 commit comments

Comments
 (0)