Fix changeset_repository.py get_for_plan to preserve per-changeset granularity #8194
@@ -452,17 +452,50 @@ class SqliteChangeSetStore:
|
||||
self,
|
||||
plan_id: str,
|
||||
) -> list[SpecChangeSet]:
|
||||
"""Return all ChangeSets associated with *plan_id*."""
|
||||
"""Return all ChangeSets associated with *plan_id*.
|
||||
|
||||
Returns one ``SpecChangeSet`` per distinct changeset, preserving
|
||||
per-changeset granularity instead of collapsing every entry into a
|
||||
single container. Entry ordering is by timestamp within each
|
||||
changeset.
|
||||
"""
|
||||
if not plan_id:
|
||||
return []
|
||||
|
||||
entries = self._entry_repo.get_entries_for_plan(plan_id)
|
||||
if not entries:
|
||||
session = self._entry_repo._session()
|
||||
try:
|
||||
rows = (
|
||||
session.query(ChangeSetEntryModel)
|
||||
.filter_by(plan_id=plan_id)
|
||||
.order_by(ChangeSetEntryModel.timestamp)
|
||||
.all()
|
||||
)
|
||||
except (OperationalError, SQLAlchemyDatabaseError) as exc:
|
||||
raise DatabaseError(f"Failed to get entries for plan: {exc}") from exc
|
||||
|
||||
if not rows:
|
||||
return []
|
||||
|
||||
return [
|
||||
SpecChangeSet(plan_id=plan_id, entries=entries),
|
||||
]
|
||||
# Group raw DB rows by their original changeset_id so each
|
||||
# SpecChangeSet represents one logical chunk of work.
|
||||
groups: dict[str, list[ChangeSetEntryModel]] = {}
|
||||
for row in rows:
|
||||
cs_id = cast(str, row.changeset_id)
|
||||
groups.setdefault(cs_id, []).append(row)
|
||||
|
||||
result: list[SpecChangeSet] = []
|
||||
for cs_id in sorted(groups):
|
||||
group_rows = groups[cs_id]
|
||||
entries = [self._entry_repo._to_domain(r) for r in group_rows]
|
||||
plan_id_from_entries = entries[0].plan_id if entries else plan_id
|
||||
result.append(
|
||||
SpecChangeSet(
|
||||
changeset_id=cs_id,
|
||||
plan_id=plan_id_from_entries or plan_id,
|
||||
entries=entries,
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
def summarize(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user