refactor: prefix every table with its module
Two tables were named after the concept they came from rather than the module
they belong to, so the schema read as if the template tables were part of the
paper module. Renamed (data preserved, `RENAME TABLE` moves rows in place):
paper_template -> template the 模板 module
section_field -> template_field_library the 字段库 the 模板 module owns
The paper tables and `template_field` already followed the rule. The rename
carries through everything that named a module:
models Template, TemplateField, TemplateFieldLibrary
schemas Template*, TemplateFieldLibrary*
crud app/crud/template.py, app/crud/template_field_library.py
API /template-field-library (was /section-fields); handlers are now
named after library entries, which removes the ambiguity with
TemplateField — a placement, a different thing entirely
client src/api/templateFieldLibrary.ts
`paper_template_filed_sort` is deliberately untouched: it is a column of the
paper module, spelled as the feature was specified.
TiDB v8.5 with tidb_enable_foreign_key on — as this cluster runs — enforces
foreign keys rather than ignoring them, so the docs' "TiDB does not enforce
foreign keys" was wrong. Corrected, with what actually follows from it: the
rename was rehearsed (RENAME TABLE carries a referencing constraint along), the
API keeps checking first so a violation names the row instead of surfacing a
driver error, and the ORM cascades stay so behaviour does not depend on a
cluster setting.
Revision f27a1c6d9e04 verified both ways; 40 smoke checks, type-check and build
all pass.
This commit is contained in:
+15
-12
@@ -1,4 +1,4 @@
|
||||
"""Seed the section-field library and a couple of starter templates.
|
||||
"""Seed the template-field library and a couple of starter templates.
|
||||
|
||||
Idempotent: fields are matched by name and templates by name, so running it
|
||||
twice adds nothing. ``--reset`` empties the three tables first, which is the
|
||||
@@ -27,7 +27,7 @@ from sqlalchemy import delete, select # noqa: E402
|
||||
from sqlalchemy.orm import Session # noqa: E402
|
||||
|
||||
from app.db.session import SessionLocal # noqa: E402
|
||||
from app.models import PaperTemplate, SectionField, TemplateField # noqa: E402
|
||||
from app.models import Template, TemplateFieldLibrary, TemplateField # noqa: E402
|
||||
|
||||
# --- the field library -------------------------------------------------------
|
||||
#
|
||||
@@ -118,20 +118,20 @@ TEMPLATES: list[tuple[str, str, list[str]]] = [
|
||||
def reset(db: Session) -> None:
|
||||
"""Empty the three tables in dependency order."""
|
||||
db.execute(delete(TemplateField))
|
||||
db.execute(delete(PaperTemplate))
|
||||
db.execute(delete(SectionField))
|
||||
db.execute(delete(Template))
|
||||
db.execute(delete(TemplateFieldLibrary))
|
||||
db.commit()
|
||||
|
||||
|
||||
def seed_fields(db: Session) -> dict[str, SectionField]:
|
||||
def seed_fields(db: Session) -> dict[str, TemplateFieldLibrary]:
|
||||
"""Insert any missing library fields and return name -> field."""
|
||||
existing = {field.name: field for field in db.scalars(select(SectionField)).all()}
|
||||
existing = {field.name: field for field in db.scalars(select(TemplateFieldLibrary)).all()}
|
||||
|
||||
created = 0
|
||||
for name, level, font_size, font_color in FIELDS:
|
||||
if name in existing:
|
||||
continue
|
||||
field = SectionField(
|
||||
field = TemplateFieldLibrary(
|
||||
name=name, level=level, font_size=font_size, font_color=font_color
|
||||
)
|
||||
db.add(field)
|
||||
@@ -142,13 +142,16 @@ def seed_fields(db: Session) -> dict[str, SectionField]:
|
||||
for field in existing.values():
|
||||
db.refresh(field)
|
||||
|
||||
print(f" section_field : {created} created, {len(existing) - created} already present")
|
||||
print(
|
||||
f" template_field_library : {created} created, "
|
||||
f"{len(existing) - created} already present"
|
||||
)
|
||||
return existing
|
||||
|
||||
|
||||
def seed_templates(db: Session, fields: dict[str, SectionField]) -> None:
|
||||
def seed_templates(db: Session, fields: dict[str, TemplateFieldLibrary]) -> None:
|
||||
"""Insert any missing starter templates."""
|
||||
existing = {name for name in db.scalars(select(PaperTemplate.name)).all()}
|
||||
existing = {name for name in db.scalars(select(Template.name)).all()}
|
||||
|
||||
created = 0
|
||||
for name, abstract, field_names in TEMPLATES:
|
||||
@@ -159,7 +162,7 @@ def seed_templates(db: Session, fields: dict[str, SectionField]) -> None:
|
||||
if missing:
|
||||
raise SystemExit(f"template {name!r} references unknown fields: {missing}")
|
||||
|
||||
template = PaperTemplate(name=name, abstract=abstract)
|
||||
template = Template(name=name, abstract=abstract)
|
||||
# sort is the list position: 1-based, ascending, with gaps allowed
|
||||
# because the UI lets the user type any integer.
|
||||
template.items = [
|
||||
@@ -170,7 +173,7 @@ def seed_templates(db: Session, fields: dict[str, SectionField]) -> None:
|
||||
created += 1
|
||||
|
||||
db.commit()
|
||||
print(f" paper_template: {created} created, {len(existing)} already present")
|
||||
print(f" template: {created} created, {len(existing)} already present")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user