AAP-68024 Remove dead Host.last_job and last_job_host_summary FK fields (#16529)

* Remove dead Host.last_job and Host.last_job_host_summary FK fields

PR #16332 replaced these denormalized ForeignKey columns with dynamic
derivation via Host.latest_summary, but left the columns in place.
Nothing writes to or reads from them anymore — drop the dead weight.

- Remove last_job and last_job_host_summary ForeignKey fields from Host
- Convert HostSerializer to use SerializerMethodField instead of FK-backed fields
- Remove redundant JobHostSummary.latest_for_host/latest_job_for_host classmethods
- Add migration 0206 to drop the columns
- Update tests to use Host.latest_summary instead of removed fields/methods

* Remove dead last_job_host_summary_id UPDATE from cleanup_jobs

The Host.last_job_host_summary FK was removed in the prior commit,
so the raw SQL UPDATE that NULLed it before deleting JHS rows now
references a non-existent column and crashes.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Alan Rominger
2026-08-26 14:28:49 -04:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 3de8dd7918
commit 3d1ca5a25d
10 changed files with 43 additions and 97 deletions
+11 -8
View File
@@ -174,8 +174,6 @@ SUMMARIZABLE_FK_FIELDS = {
'workflow_approval': DEFAULT_SUMMARY_FIELDS + ('timeout',),
'schedule': DEFAULT_SUMMARY_FIELDS + ('next_run',),
'unified_job_template': DEFAULT_SUMMARY_FIELDS + ('unified_job_type',),
# last_job and last_job_host_summary are derived from JobHostSummary in HostSerializer,
# not from the stale FK fields on Host.
'last_update': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'license_error'),
'current_update': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'license_error'),
'current_job': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'license_error'),
@@ -1803,6 +1801,8 @@ class HostSerializer(BaseSerializerWithVariables):
has_active_failures = serializers.SerializerMethodField()
has_inventory_sources = serializers.SerializerMethodField()
last_job = serializers.SerializerMethodField()
last_job_host_summary = serializers.SerializerMethodField()
class Meta:
model = Host
@@ -1818,7 +1818,7 @@ class HostSerializer(BaseSerializerWithVariables):
'last_job_host_summary',
'ansible_facts_modified',
)
read_only_fields = ('last_job', 'last_job_host_summary', 'ansible_facts_modified')
read_only_fields = ('ansible_facts_modified',)
def build_relational_field(self, field_name, relation_info):
field_class, field_kwargs = super(HostSerializer, self).build_relational_field(field_name, relation_info)
@@ -1953,13 +1953,16 @@ class HostSerializer(BaseSerializerWithVariables):
return ret
if 'inventory' in ret and not obj.inventory:
ret['inventory'] = None
last_summary = obj.latest_summary
if 'last_job' in ret:
ret['last_job'] = last_summary.job_id if last_summary else None
if 'last_job_host_summary' in ret:
ret['last_job_host_summary'] = last_summary.pk if last_summary else None
return ret
def get_last_job(self, obj):
last_summary = obj.latest_summary
return last_summary.job_id if last_summary else None
def get_last_job_host_summary(self, obj):
last_summary = obj.latest_summary
return last_summary.pk if last_summary else None
def get_has_active_failures(self, obj):
last_summary = obj.latest_summary
return bool(last_summary and last_summary.failed)