m1m02530125 Claude Sonnet 4.6 commited on
Commit
3674583
·
1 Parent(s): ae2adb5

PostgreSQL対応: strftime('%w')をEXTRACT(DOW)に変換、エラー耐性を追加

Browse files

- db/database.py: _to_pg() に strftime('%w', col) → EXTRACT(DOW FROM col::date)::int 変換を追加
HuggingFace Spaces (PostgreSQL環境) で get_employee_dow_patterns が 500 エラーになっていた原因
- db/repositories.py: get_employee_dow_patterns の GROUP BY をエイリアスでなく式で記述(PostgreSQL互換)、NULL/不正値ガードを強化
- routes/shifts.py: 提案計算を try-except でラップし、失敗時も画面が落ちないよう保護

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (3) hide show
  1. db/database.py +6 -0
  2. db/repositories.py +18 -8
  3. routes/shifts.py +8 -5
db/database.py CHANGED
@@ -40,6 +40,12 @@ def _to_pg(sql: str) -> str:
40
  sql = re.sub(r"INTEGER\s+PRIMARY\s+KEY\s+AUTOINCREMENT", "SERIAL PRIMARY KEY",
41
  sql, flags=re.IGNORECASE)
42
  sql = sql.replace("DEFAULT (datetime('now','localtime'))", "DEFAULT CURRENT_TIMESTAMP")
 
 
 
 
 
 
43
  return sql
44
 
45
 
 
40
  sql = re.sub(r"INTEGER\s+PRIMARY\s+KEY\s+AUTOINCREMENT", "SERIAL PRIMARY KEY",
41
  sql, flags=re.IGNORECASE)
42
  sql = sql.replace("DEFAULT (datetime('now','localtime'))", "DEFAULT CURRENT_TIMESTAMP")
43
+ # SQLite の日付関数 → PostgreSQL 相当
44
+ sql = re.sub(
45
+ r"strftime\('%w',\s*(\w+)\)",
46
+ r"EXTRACT(DOW FROM \1::date)::int",
47
+ sql,
48
+ )
49
  return sql
50
 
51
 
db/repositories.py CHANGED
@@ -576,7 +576,7 @@ def get_employee_dow_patterns(emp_id: int) -> dict[int, str]:
576
  FROM shift_requests
577
  WHERE employee_id=? AND pattern_id IS NOT NULL AND pattern_id != ''
578
  AND pattern_id != 'custom'
579
- GROUP BY dow, pattern_id""",
580
  (emp_id,)
581
  ).fetchall()
582
  conn.close()
@@ -584,14 +584,24 @@ def get_employee_dow_patterns(emp_id: int) -> dict[int, str]:
584
  sqlite_to_py = {0: 6, 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5}
585
  dow_counts: dict[int, dict[str, int]] = {}
586
  for r in rows:
587
- py_dow = sqlite_to_py[int(r["dow"])]
 
 
 
 
 
 
 
588
  dow_counts.setdefault(py_dow, {})
589
- dow_counts[py_dow][r["pattern_id"]] = dow_counts[py_dow].get(r["pattern_id"], 0) + r["cnt"]
590
- return {
591
- dow: max(patterns, key=patterns.get)
592
- for dow, patterns in dow_counts.items()
593
- if max(patterns.values()) >= 2
594
- }
 
 
 
595
 
596
 
597
  def get_schedule_notes(period_id: int) -> dict[str, str]:
 
576
  FROM shift_requests
577
  WHERE employee_id=? AND pattern_id IS NOT NULL AND pattern_id != ''
578
  AND pattern_id != 'custom'
579
+ GROUP BY strftime('%w', date), pattern_id""",
580
  (emp_id,)
581
  ).fetchall()
582
  conn.close()
 
584
  sqlite_to_py = {0: 6, 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5}
585
  dow_counts: dict[int, dict[str, int]] = {}
586
  for r in rows:
587
+ dow_val = r["dow"]
588
+ if dow_val is None:
589
+ continue
590
+ py_dow = sqlite_to_py.get(int(dow_val))
591
+ if py_dow is None:
592
+ continue
593
+ pid = r["pattern_id"]
594
+ cnt = int(r["cnt"])
595
  dow_counts.setdefault(py_dow, {})
596
+ dow_counts[py_dow][pid] = dow_counts[py_dow].get(pid, 0) + cnt
597
+ result = {}
598
+ for dow, patterns in dow_counts.items():
599
+ if not patterns:
600
+ continue
601
+ best_pid = max(patterns, key=patterns.get)
602
+ if patterns[best_pid] >= 2:
603
+ result[dow] = best_pid
604
+ return result
605
 
606
 
607
  def get_schedule_notes(period_id: int) -> dict[str, str]:
routes/shifts.py CHANGED
@@ -93,11 +93,14 @@ def input(period_id):
93
  top_patterns = []
94
  dow_suggestions = {}
95
  if current_emp and current_emp.employment_type != EmploymentType.FULL_TIME:
96
- top_patterns = [
97
- (pid, cnt) for pid, cnt in repo.get_employee_pattern_history(current_emp.id)
98
- if pid in PATTERN_MAP
99
- ]
100
- dow_suggestions = repo.get_employee_dow_patterns(current_emp.id)
 
 
 
101
 
102
  return render_template(
103
  "shifts/input.html",
 
93
  top_patterns = []
94
  dow_suggestions = {}
95
  if current_emp and current_emp.employment_type != EmploymentType.FULL_TIME:
96
+ try:
97
+ top_patterns = [
98
+ (pid, cnt) for pid, cnt in repo.get_employee_pattern_history(current_emp.id)
99
+ if pid in PATTERN_MAP
100
+ ]
101
+ dow_suggestions = repo.get_employee_dow_patterns(current_emp.id)
102
+ except Exception:
103
+ pass
104
 
105
  return render_template(
106
  "shifts/input.html",