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:
@@ -7,6 +7,22 @@ be added to the imports below, not only to its own module.
|
||||
The import order here is for readability only: relationships are declared by
|
||||
name and resolved through the SQLAlchemy registry after every module has been
|
||||
imported, so a cycle between two model modules is not a problem.
|
||||
|
||||
Table naming
|
||||
------------
|
||||
Every table is prefixed with the module it belongs to, and a class is named
|
||||
after its table (``Template`` -> ``template``):
|
||||
|
||||
template the 模板 module
|
||||
template_field a field placed in a template, with its position
|
||||
template_field_library the 字段库 the template module draws on
|
||||
paper the 论文 module
|
||||
paper_sentence one sentence of one paper
|
||||
paper_sentence_reference a citation of one sentence
|
||||
|
||||
``template_field`` and ``template_field_library`` are one word apart and mean
|
||||
opposite things: the first is a *placement* (this template puts this field
|
||||
here, ``sort`` included), the second is the *catalogue* it was picked from.
|
||||
"""
|
||||
|
||||
from app.models.mixins import TimestampMixin
|
||||
@@ -19,9 +35,9 @@ from app.models.paper import (
|
||||
)
|
||||
from app.models.paper_sentence import PaperSentence
|
||||
from app.models.paper_sentence_reference import PaperSentenceReference
|
||||
from app.models.paper_template import PaperTemplate
|
||||
from app.models.section_field import SectionField
|
||||
from app.models.template import Template
|
||||
from app.models.template_field import TemplateField
|
||||
from app.models.template_field_library import TemplateFieldLibrary
|
||||
|
||||
__all__ = [
|
||||
"PAPER_STATUSES",
|
||||
@@ -31,8 +47,8 @@ __all__ = [
|
||||
"Paper",
|
||||
"PaperSentence",
|
||||
"PaperSentenceReference",
|
||||
"PaperTemplate",
|
||||
"SectionField",
|
||||
"Template",
|
||||
"TemplateField",
|
||||
"TemplateFieldLibrary",
|
||||
"TimestampMixin",
|
||||
]
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
A paper is a bag of metadata plus two things that are deliberately kept apart:
|
||||
|
||||
* its **structure**, which is not stored here at all. Every render reads the
|
||||
:class:`~app.models.paper_template.PaperTemplate` the paper points at, live.
|
||||
:class:`~app.models.template.Template` the paper points at, live.
|
||||
Nothing is copied, so switching ``template_id`` re-shapes the whole document
|
||||
in one write.
|
||||
* its **content**, which lives in
|
||||
@@ -33,7 +33,7 @@ from app.models.mixins import TimestampMixin
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover - typing only
|
||||
from app.models.paper_sentence import PaperSentence
|
||||
from app.models.paper_template import PaperTemplate
|
||||
from app.models.template import Template
|
||||
|
||||
#: Writing state of a paper: 草稿 / 撰写中 / 已完成.
|
||||
#:
|
||||
@@ -65,7 +65,7 @@ class Paper(TimestampMixin, Base):
|
||||
#: ``RESTRICT`` documents the intent (a template in use must not vanish);
|
||||
#: TiDB does not enforce it, so the API refuses the template delete too.
|
||||
template_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("paper_template.id", ondelete="RESTRICT"),
|
||||
ForeignKey("template.id", ondelete="RESTRICT"),
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
@@ -93,7 +93,7 @@ class Paper(TimestampMixin, Base):
|
||||
|
||||
#: Eager-loaded with the paper: every read of a paper shows its template
|
||||
#: name, and a lazy load there would be one query per row in the table.
|
||||
template: Mapped["PaperTemplate | None"] = relationship(lazy="joined")
|
||||
template: Mapped["Template | None"] = relationship(lazy="joined")
|
||||
|
||||
#: The paper's sentences, in document order.
|
||||
#:
|
||||
|
||||
@@ -10,7 +10,7 @@ whole design:
|
||||
meaningfully have in common. Swap the paper's template and this sentence
|
||||
lands on whatever the new template puts at that position, with no per
|
||||
sentence editing. (The name keeps the spelling the feature was specified
|
||||
with; it reads ``paper_template_field_sort``.)
|
||||
with; it reads ``template_field_sort``.)
|
||||
|
||||
``sort``
|
||||
Where this sentence sits *inside* that paragraph. A paragraph is reassembled
|
||||
@@ -40,7 +40,7 @@ from app.models.mixins import TimestampMixin
|
||||
if TYPE_CHECKING: # pragma: no cover - typing only
|
||||
from app.models.paper import Paper
|
||||
from app.models.paper_sentence_reference import PaperSentenceReference
|
||||
from app.models.paper_template import PaperTemplate
|
||||
from app.models.template import Template
|
||||
|
||||
|
||||
class PaperSentence(TimestampMixin, Base):
|
||||
@@ -70,7 +70,7 @@ class PaperSentence(TimestampMixin, Base):
|
||||
#: The template this sentence was written against — provenance only. Reads
|
||||
#: never filter on it; see the module docstring.
|
||||
template_id: Mapped[int | None] = mapped_column(
|
||||
ForeignKey("paper_template.id", ondelete="SET NULL"),
|
||||
ForeignKey("template.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"""Paper templates (模板表).
|
||||
"""Templates (模板表) — the outlines a paper can be written against.
|
||||
|
||||
A template is a named, ordered selection of library fields — the outline a
|
||||
paper is written against. It stores no typography of its own: font size and
|
||||
colour are read from the referenced :class:`~app.models.section_field.SectionField`,
|
||||
colour are read from the referenced
|
||||
:class:`~app.models.template_field_library.TemplateFieldLibrary`,
|
||||
so correcting a field's styling updates every template that uses it.
|
||||
"""
|
||||
|
||||
@@ -18,10 +19,10 @@ if TYPE_CHECKING: # pragma: no cover - typing only
|
||||
from app.models.template_field import TemplateField
|
||||
|
||||
|
||||
class PaperTemplate(TimestampMixin, Base):
|
||||
class Template(TimestampMixin, Base):
|
||||
"""A named outline: a template name, a summary, and its ordered fields."""
|
||||
|
||||
__tablename__ = "paper_template"
|
||||
__tablename__ = "template"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
|
||||
@@ -43,4 +44,4 @@ class PaperTemplate(TimestampMixin, Base):
|
||||
)
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover - debugging aid
|
||||
return f"<PaperTemplate id={self.id} name={self.name!r}>"
|
||||
return f"<Template id={self.id} name={self.name!r}>"
|
||||
@@ -25,8 +25,8 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.db.base import Base
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover - typing only
|
||||
from app.models.paper_template import PaperTemplate
|
||||
from app.models.section_field import SectionField
|
||||
from app.models.template import Template
|
||||
from app.models.template_field_library import TemplateFieldLibrary
|
||||
|
||||
|
||||
class TemplateField(Base):
|
||||
@@ -44,7 +44,7 @@ class TemplateField(Base):
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
|
||||
template_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("paper_template.id", ondelete="CASCADE"),
|
||||
ForeignKey("template.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
@@ -52,7 +52,7 @@ class TemplateField(Base):
|
||||
#: under it. The API enforces this in Python as well, since TiDB does not
|
||||
#: enforce foreign keys itself.
|
||||
field_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("section_field.id", ondelete="RESTRICT"),
|
||||
ForeignKey("template_field_library.id", ondelete="RESTRICT"),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
@@ -60,11 +60,11 @@ class TemplateField(Base):
|
||||
#: sorts render first regardless of the field's ``level``.
|
||||
sort: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
|
||||
template: Mapped["PaperTemplate"] = relationship(back_populates="items")
|
||||
template: Mapped["Template"] = relationship(back_populates="items")
|
||||
|
||||
#: Eager-loaded because every template read needs the field's name and
|
||||
#: typography; lazy loading would emit one query per row.
|
||||
field: Mapped["SectionField"] = relationship(lazy="joined")
|
||||
field: Mapped["TemplateFieldLibrary"] = relationship(lazy="joined")
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover - debugging aid
|
||||
return (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""The reusable section-field library (字段表).
|
||||
"""The reusable template-field library (字段库).
|
||||
|
||||
A *section field* is one heading a paper can contain — "1. Introduction",
|
||||
A *library entry* is one heading a paper can contain — "1. Introduction",
|
||||
"2.1 Dataset", "0 Abstract". Fields live in this one global library and are
|
||||
never owned by a template.
|
||||
|
||||
@@ -9,7 +9,7 @@ Why there is no ``parent_id``
|
||||
A tree would make a field usable under exactly one parent, so a level-2 heading
|
||||
such as "Background" could not sit under both "1. Introduction" and
|
||||
"2. Related Work". Hierarchy is therefore expressed only by
|
||||
:attr:`SectionField.level` (1, 2, 3 ...), which is a *rendering hint* — it
|
||||
:attr:`TemplateFieldLibrary.level` (1, 2, 3 ...), which is a *rendering hint* — it
|
||||
drives indentation and numbering semantics in the UI — while a field stays
|
||||
free to be attached to any number of templates and any number of parents
|
||||
within them.
|
||||
@@ -20,6 +20,11 @@ user ("1. Introduction", "0 Abstract"). Nothing derives or rewrites it.
|
||||
Ordering inside a template is *not* stored here: it lives in
|
||||
``template_field.sort``. This table has no ``sort`` column on purpose, so that
|
||||
one library field can occupy a different position in every template.
|
||||
|
||||
Naming: the table is ``template_field_library`` and one row is one entry of it
|
||||
— the class is named after the table, as everywhere else in this package. The
|
||||
library is global, but it belongs to the 模板 module: it is the raw material a
|
||||
template is built from, and it is managed from the same menu.
|
||||
"""
|
||||
|
||||
from decimal import Decimal
|
||||
@@ -31,10 +36,10 @@ from app.db.base import Base
|
||||
from app.models.mixins import TimestampMixin
|
||||
|
||||
|
||||
class SectionField(TimestampMixin, Base):
|
||||
class TemplateFieldLibrary(TimestampMixin, Base):
|
||||
"""A single reusable heading, with the typography it should render in."""
|
||||
|
||||
__tablename__ = "section_field"
|
||||
__tablename__ = "template_field_library"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
|
||||
@@ -72,6 +77,6 @@ class SectionField(TimestampMixin, Base):
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover - debugging aid
|
||||
return (
|
||||
f"<SectionField id={self.id} name={self.name!r} "
|
||||
f"<TemplateFieldLibrary id={self.id} name={self.name!r} "
|
||||
f"level={self.level} color={self.font_color}>"
|
||||
)
|
||||
Reference in New Issue
Block a user