feat(project): 목표 표 3열에서 줄 추가 = 같은 항목 칸(2열) 안에 — 항목 칸 묶음

- goal_rows.label_cont(마이그레이션 016): 바로 위 줄 항목 칸에 묶임, 이름은 묶음 첫 줄, 2열 rowspan(_goal_label_spans)
- 3열 우클릭 위/아래에 줄 추가 → same_label: 같은 항목 칸 안(이름 묻지 않음), 첫 줄 위면 새 줄이 첫 줄로 이름 넘겨받음
- 2열 메뉴·+ 줄 추가는 묶음 밖(묶음 맨 위/맨 아래 기준), 묶인 2열 우클릭 → 항목 칸 나누기(split-label)
- 묶음 첫 줄 삭제 → 다음 줄이 첫 줄·이름 넘겨받음, 묶인 줄 이동 400, 새 연도 복사에 묶음 유지
- 캐시 버전 20261001k, DB 회귀 test_goal_label_groups, 문서 갱신

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 13:37:40 +09:00
parent ac0672bc11
commit 164153dc9c
15 changed files with 271 additions and 48 deletions
+102 -17
View File
@@ -1877,25 +1877,35 @@ class ProjectStore:
(src["year"],),
).fetchall()
plan = []
groups = self._column_exists(conn, "goal_rows", "label_cont")
for a in areas:
labels = conn.execute(
"SELECT label FROM goal_rows WHERE area_id = %s ORDER BY sort_order, id",
"SELECT label" + (", label_cont" if groups else "") + " FROM goal_rows "
"WHERE area_id = %s ORDER BY sort_order, id",
(a["id"],),
).fetchall()
plan.append((a["name"], a["color"], tuple(r["label"] for r in labels) or ("",)))
plan.append((a["name"], a["color"],
tuple((r["label"], bool(r.get("label_cont"))) for r in labels) or (("", False),)))
else:
plan = list(store.DEFAULT_GOAL_AREAS)
plan = [(n, c, tuple((lb, False) for lb in labels)) for n, c, labels in store.DEFAULT_GOAL_AREAS]
groups = self._column_exists(conn, "goal_rows", "label_cont")
for i, (name, color, labels) in enumerate(plan):
aid = conn.execute(
"INSERT INTO goal_areas (year, name, color, sort_order) "
"VALUES (%s, %s, %s, %s) RETURNING id",
(y, name, color, i),
).fetchone()["id"]
for j, label in enumerate(labels):
conn.execute(
"INSERT INTO goal_rows (area_id, label, sort_order) VALUES (%s, %s, %s)",
(aid, label, j),
)
for j, (label, cont) in enumerate(labels):
if groups:
conn.execute(
"INSERT INTO goal_rows (area_id, label, label_cont, sort_order) VALUES (%s, %s, %s, %s)",
(aid, label, cont and j > 0, j),
)
else:
conn.execute(
"INSERT INTO goal_rows (area_id, label, sort_order) VALUES (%s, %s, %s)",
(aid, label, j),
)
def delete_goal_year(self, *, year: int) -> None:
with self._pool.connection() as conn:
@@ -1937,8 +1947,9 @@ class ProjectStore:
"SELECT id, name, color FROM goal_areas WHERE year = %s ORDER BY sort_order, id",
(year,),
).fetchall()
groups = self._column_exists(conn, "goal_rows", "label_cont")
row_rows = conn.execute(
"SELECT r.id, r.area_id, r.label FROM goal_rows r "
"SELECT r.id, r.area_id, r.label" + (", r.label_cont" if groups else "") + " FROM goal_rows r "
"JOIN goal_areas a ON a.id = r.area_id WHERE a.year = %s "
"ORDER BY r.sort_order, r.id",
(year,),
@@ -1974,7 +1985,7 @@ class ProjectStore:
rid = int(r["id"])
rows_by_area.setdefault(int(r["area_id"]), []).append(
{"id": rid, "label": r["label"], "cells": cells.get(rid, {}),
"merges": merges.get(rid, {})}
"merges": merges.get(rid, {}), "label_cont": bool(r.get("label_cont"))}
)
areas = [
{"id": int(a["id"]), "name": a["name"], "color": a["color"],
@@ -1982,7 +1993,7 @@ class ProjectStore:
for a in area_rows
]
return {"year": int(head["year"]), "sales_unit": head["sales_unit"],
"metrics": metrics, "areas": areas, "halves": halves, "rect": rect}
"metrics": metrics, "areas": areas, "halves": halves, "rect": rect, "groups": groups}
def set_goal_metric(
self, *, year: int, metric: str, kind: str, month: int, value: float | None, updated_by: str = ""
@@ -2084,15 +2095,35 @@ class ProjectStore:
if 0 <= j < len(ids) and any(g["r1"] > g["r0"] and (g["r0"] <= i <= g["r1"] or g["r0"] <= j <= g["r1"])
for g in merges):
raise ValueError("세로로 병합된 칸에 걸린 줄은 옮길 수 없습니다. 병합을 해제한 뒤 옮기세요.")
if 0 <= j < len(ids):
conts = self._label_conts(int(me["area_id"]), ids)
in_group = lambda k: conts[k] or (k + 1 < len(ids) and conts[k + 1]) # noqa: E731
if in_group(i) or in_group(j):
raise ValueError("같은 항목 칸으로 묶인 줄은 옮길 수 없습니다. 항목 칸을 나눈 뒤 옮기세요.")
self._move_goal_item("goal_rows", "area_id", row_id, direction)
def _label_conts(self, area_id: int, ids: list[int]) -> list[bool]:
"""영역 줄 순서(ids)대로 label_cont — 016 미적용이면 모두 False."""
with self._pool.connection() as conn:
if not self._column_exists(conn, "goal_rows", "label_cont"):
return [False] * len(ids)
got = {int(r["id"]): bool(r["label_cont"]) for r in conn.execute(
"SELECT id, label_cont FROM goal_rows WHERE area_id = %s", (area_id,)).fetchall()}
return [got.get(i, False) for i in ids]
def add_goal_row(
self, *, area_id: int, label: str = "", ref_row_id: int | None = None, place: str = "below"
self, *, area_id: int, label: str = "", ref_row_id: int | None = None, place: str = "below",
same_label: bool = False,
) -> int:
"""줄 추가 — 기준 줄(ref_row_id)이 있으면 그 줄 위(place="above")/아래("below")에, 없으면 영역 맨 끝에."""
"""줄 추가 — 기준 줄(ref_row_id)이 있으면 그 줄 위(place="above")/아래("below")에, 없으면 영역 맨 끝에.
same_label(3열에서 추가, 016) = 새 줄을 기준 줄과 **같은 항목 칸(2열)** 안에 넣는다: 아래면 새 줄이 묶음에 이어 붙고
(label_cont), 위면 — 기준 줄이 묶음 첫 줄이면 새 줄이 첫 줄이 되어 이름을 넘겨받고 기준 줄이 이어 붙는다."""
lb = store.validate_goal_row_label(label)
if place not in ("above", "below"):
raise ValueError("place 는 above 또는 below 입니다.")
if same_label and ref_row_id is None:
raise ValueError("같은 항목 칸에 추가하려면 기준 줄이 필요합니다.")
with self._pool.connection() as conn:
with conn.transaction():
if not conn.execute(
@@ -2112,11 +2143,29 @@ class ProjectStore:
).fetchall()]
if ref_row_id not in ids:
raise KeyError(ref_row_id)
rid = int(conn.execute(
"INSERT INTO goal_rows (area_id, label, sort_order) VALUES (%s, %s, 0) RETURNING id",
(area_id, lb),
).fetchone()["id"])
if same_label:
if not self._column_exists(conn, "goal_rows", "label_cont"):
raise store.GoalMigration016Needed()
ref = conn.execute("SELECT label, label_cont FROM goal_rows WHERE id = %s", (ref_row_id,)).fetchone()
if place == "above" and not ref["label_cont"]:
# 묶음 첫 줄 위 — 새 줄이 첫 줄(이름을 넘겨받음), 기준 줄은 이어 붙는다
rid = int(conn.execute(
"INSERT INTO goal_rows (area_id, label, label_cont, sort_order) VALUES (%s, %s, false, 0) "
"RETURNING id", (area_id, ref["label"]),
).fetchone()["id"])
conn.execute("UPDATE goal_rows SET label = '', label_cont = true WHERE id = %s", (ref_row_id,))
else:
rid = int(conn.execute(
"INSERT INTO goal_rows (area_id, label, label_cont, sort_order) VALUES (%s, '', true, 0) "
"RETURNING id", (area_id,),
).fetchone()["id"])
else:
rid = int(conn.execute(
"INSERT INTO goal_rows (area_id, label, sort_order) VALUES (%s, %s, 0) RETURNING id",
(area_id, lb),
).fetchone()["id"])
i = ids.index(ref_row_id) + (1 if place == "below" else 0)
# 따로 추가(2열 메뉴·+)는 화면이 묶음의 맨 위(위에)·맨 아래(아래) 줄을 기준으로 보내 묶음 밖에 들어간다
# 세로 병합 안쪽(시작 줄 아래 ~ 마지막 줄)에 끼우면 엑셀처럼 그 병합이 한 줄 늘어난다
if self._table_exists(conn, "goal_cell_merges") and self._column_exists(
conn, "goal_cell_merges", "row_span"):
@@ -2172,8 +2221,44 @@ class ProjectStore:
"DELETE FROM goal_cell_merges WHERE row_id = %s AND start_month = %s",
(g["row_id"], g["start"]),
)
if self._column_exists(conn, "goal_rows", "label_cont"):
# 묶음 첫 줄을 지우면 바로 아래 이어 붙은 줄이 첫 줄이 되고 이름을 넘겨받는다(비어 있으면)
me2 = conn.execute("SELECT label, label_cont, sort_order FROM goal_rows WHERE id = %s",
(row_id,)).fetchone()
if not me2["label_cont"]:
nxt = conn.execute(
"SELECT id, label, label_cont FROM goal_rows WHERE area_id = %s AND id <> %s "
"AND (sort_order, id) > (%s, %s) ORDER BY sort_order, id LIMIT 1",
(me["area_id"], row_id, me2["sort_order"], row_id),
).fetchone()
if nxt and nxt["label_cont"]:
conn.execute(
"UPDATE goal_rows SET label_cont = false, label = CASE WHEN label = '' THEN %s ELSE label END "
"WHERE id = %s", (me2["label"], nxt["id"]),
)
conn.execute("DELETE FROM goal_rows WHERE id = %s", (row_id,))
def split_goal_label(self, *, row_id: int) -> int:
"""항목 칸 나누기 — row_id(묶음 첫 줄) 아래 이어 붙은 줄들을 각자 항목 칸으로(이름은 빈 칸). 나눈 줄 수."""
with self._pool.connection() as conn:
with conn.transaction():
if not self._column_exists(conn, "goal_rows", "label_cont"):
raise store.GoalMigration016Needed()
me = conn.execute("SELECT area_id FROM goal_rows WHERE id = %s FOR UPDATE", (row_id,)).fetchone()
if me is None:
raise KeyError(row_id)
rows = conn.execute(
"SELECT id, label_cont FROM goal_rows WHERE area_id = %s ORDER BY sort_order, id", (me["area_id"],)
).fetchall()
ids = [int(r["id"]) for r in rows]
n = 0
for r in rows[ids.index(row_id) + 1:]:
if not r["label_cont"]:
break
conn.execute("UPDATE goal_rows SET label_cont = false WHERE id = %s", (r["id"],))
n += 1
return n
def set_goal_cell(self, *, row_id: int, month: int, body: str, updated_by: str = "") -> None:
m = store.validate_goal_month(month, allow_year=True)
text = store.validate_goal_text(body)