Skip to content

Commit a5d07a8

Browse files
committed
should pass all checks?
1 parent 45d2a6d commit a5d07a8

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

api/services/efp_bootstrap.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import re
10+
import hashlib
1011
from typing import Dict, Iterable, List
1112

1213
from sqlalchemy import Column, Index, MetaData, Table, create_engine, text
@@ -73,10 +74,30 @@ def _build_table(metadata: MetaData, spec, db_name: str) -> Table:
7374
table = Table(spec["table_name"], metadata, *columns, mysql_charset=spec.get("charset"))
7475
index_cols = spec.get("index") or []
7576
if index_cols:
76-
Index(f"ix_{db_name}_{'_'.join(index_cols)}", *[table.c[col] for col in index_cols])
77+
index_name = _make_index_name(db_name, index_cols)
78+
Index(index_name, *[table.c[col] for col in index_cols])
7779
return table
7880

7981

82+
def _make_index_name(db_name: str, index_cols: Iterable[str], max_len: int = 64) -> str:
83+
"""
84+
Create a MySQL-safe index name capped at 64 characters.
85+
86+
If the generated name is too long, fall back to a truncated db_name with a stable hash
87+
to keep names deterministic and avoid collisions.
88+
"""
89+
base = f"ix_{db_name}_{'_'.join(index_cols)}"
90+
if len(base) <= max_len:
91+
return base
92+
93+
digest = hashlib.sha1(base.encode("utf-8")).hexdigest()[:8]
94+
reserved = len("ix_") + 1 + len(digest)
95+
db_len = max_len - reserved
96+
if db_len <= 0:
97+
return f"ix_{digest}"
98+
return f"ix_{db_name[:db_len]}_{digest}"
99+
100+
80101
def _build_url(host: str, port: int, user: str, password: str, database: str | None = None) -> URL:
81102
"""
82103
Build a SQLAlchemy database URL for MySQL connections.

0 commit comments

Comments
 (0)