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:
2026-09-18 17:48:11 +08:00
parent 2d113f9f6b
commit a5f884f440
25 changed files with 371 additions and 260 deletions
@@ -1,8 +1,8 @@
import http from './client'
import type { BatchDeleteResult, PageQuery, PageResult } from './types'
/** Mirrors `app.schemas.section_field.SectionFieldRead`. */
export interface SectionField {
/** Mirrors `app.schemas.template_field_library.TemplateFieldLibraryRead`. */
export interface TemplateFieldLibrary {
id: number
/** Display name, numbering included — e.g. `1. Introduction`. */
name: string
@@ -17,23 +17,23 @@ export interface SectionField {
}
/** Body for creating or updating a library field. */
export interface SectionFieldPayload {
export interface TemplateFieldLibraryPayload {
name: string
level: number
font_size: number
font_color: string
}
export interface SectionFieldQuery extends PageQuery {
export interface TemplateFieldLibraryQuery extends PageQuery {
level?: number | null
}
/** The API caps `page_size` at 500; 200 keeps responses modest while looping. */
const LIBRARY_PAGE_SIZE = 200
export async function listSectionFields(
query: SectionFieldQuery = {},
): Promise<PageResult<SectionField>> {
export async function listTemplateFieldLibrary(
query: TemplateFieldLibraryQuery = {},
): Promise<PageResult<TemplateFieldLibrary>> {
// Drop empty filters so the URL stays clean and the backend sees "no filter"
// rather than `keyword=`.
const params: Record<string, unknown> = {}
@@ -42,7 +42,10 @@ export async function listSectionFields(
if (query.page) params.page = query.page
if (query.page_size) params.page_size = query.page_size
const { data } = await http.get<PageResult<SectionField>>('/section-fields', { params })
const { data } = await http.get<PageResult<TemplateFieldLibrary>>(
'/template-field-library',
{ params },
)
return data
}
@@ -52,12 +55,12 @@ export async function listSectionFields(
* The template form needs every field, not one page, so that a field placed
* deep in the library can still be shown by name in an existing selection.
*/
export async function fetchAllSectionFields(keyword?: string): Promise<SectionField[]> {
const all: SectionField[] = []
export async function fetchAllTemplateFieldLibrary(keyword?: string): Promise<TemplateFieldLibrary[]> {
const all: TemplateFieldLibrary[] = []
let page = 1
for (;;) {
const result = await listSectionFields({
const result = await listTemplateFieldLibrary({
keyword,
page,
page_size: LIBRARY_PAGE_SIZE,
@@ -70,29 +73,29 @@ export async function fetchAllSectionFields(keyword?: string): Promise<SectionFi
}
}
export async function createSectionField(
payload: SectionFieldPayload,
): Promise<SectionField> {
const { data } = await http.post<SectionField>('/section-fields', payload)
export async function createTemplateFieldLibrary(
payload: TemplateFieldLibraryPayload,
): Promise<TemplateFieldLibrary> {
const { data } = await http.post<TemplateFieldLibrary>('/template-field-library', payload)
return data
}
export async function updateSectionField(
export async function updateTemplateFieldLibrary(
id: number,
payload: Partial<SectionFieldPayload>,
): Promise<SectionField> {
const { data } = await http.patch<SectionField>(`/section-fields/${id}`, payload)
payload: Partial<TemplateFieldLibraryPayload>,
): Promise<TemplateFieldLibrary> {
const { data } = await http.patch<TemplateFieldLibrary>(`/template-field-library/${id}`, payload)
return data
}
export async function deleteSectionField(id: number): Promise<void> {
await http.delete(`/section-fields/${id}`)
export async function deleteTemplateFieldLibrary(id: number): Promise<void> {
await http.delete(`/template-field-library/${id}`)
}
export async function batchDeleteSectionFields(
export async function batchDeleteTemplateFieldLibrary(
ids: number[],
): Promise<BatchDeleteResult> {
const { data } = await http.post<BatchDeleteResult>('/section-fields/batch-delete', {
const { data } = await http.post<BatchDeleteResult>('/template-field-library/batch-delete', {
ids,
})
return data
@@ -11,18 +11,18 @@ import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { errorMessage } from '@/api/client'
import {
createSectionField,
updateSectionField,
type SectionField,
type SectionFieldPayload,
} from '@/api/sectionFields'
createTemplateFieldLibrary,
updateTemplateFieldLibrary,
type TemplateFieldLibrary,
type TemplateFieldLibraryPayload,
} from '@/api/templateFieldLibrary'
import { normalizeHexColor, typographyStyle } from '@/utils/format'
const props = defineProps<{
/** Visibility, used with `v-model`. */
modelValue: boolean
/** The field being edited, or `null` to create a new one. */
field: SectionField | null
field: TemplateFieldLibrary | null
}>()
const emit = defineEmits<{
@@ -39,7 +39,7 @@ const visible = computed({
const formRef = ref<FormInstance>()
const saving = ref(false)
const form = reactive<SectionFieldPayload>({
const form = reactive<TemplateFieldLibraryPayload>({
name: '',
level: 1,
// 小四, the conventional body size for a Chinese thesis.
@@ -47,7 +47,7 @@ const form = reactive<SectionFieldPayload>({
font_color: '#000000',
})
const rules: FormRules<SectionFieldPayload> = {
const rules: FormRules<TemplateFieldLibraryPayload> = {
name: [
{ required: true, message: '请填写字段名称', trigger: 'blur' },
{ max: 255, message: '字段名称最多 255 个字符', trigger: 'blur' },
@@ -118,10 +118,10 @@ async function submit(): Promise<void> {
saving.value = true
try {
if (props.field) {
await updateSectionField(props.field.id, { ...form })
await updateTemplateFieldLibrary(props.field.id, { ...form })
ElMessage.success('字段已更新,使用它的模板会同步生效')
} else {
await createSectionField({ ...form })
await createTemplateFieldLibrary({ ...form })
ElMessage.success('字段已创建')
}
emit('saved')
@@ -28,7 +28,7 @@ import {
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { errorMessage } from '@/api/client'
import { fetchAllSectionFields, type SectionField } from '@/api/sectionFields'
import { fetchAllTemplateFieldLibrary, type TemplateFieldLibrary } from '@/api/templateFieldLibrary'
import {
createTemplate,
getTemplate,
@@ -70,7 +70,7 @@ const formRef = ref<FormInstance>()
const loading = ref(false)
const saving = ref(false)
const library = ref<SectionField[]>([])
const library = ref<TemplateFieldLibrary[]>([])
const libraryKeyword = ref('')
const selected = ref<Placement[]>([])
@@ -145,7 +145,7 @@ watch(
try {
// The whole library, so a field that was placed before the keyword
// filter existed still resolves to a name in the selection.
library.value = await fetchAllSectionFields()
library.value = await fetchAllTemplateFieldLibrary()
if (props.template) {
const detail = await getTemplate(props.template.id)
@@ -166,7 +166,7 @@ watch(
)
/** Append a placement, at the end of the current order. */
function addField(field: SectionField): void {
function addField(field: TemplateFieldLibrary): void {
const highest = selected.value.reduce((max, item) => Math.max(max, item.sort), 0)
selected.value.push({ key: nextKey++, field_id: field.id, sort: highest + 1 })
}
+2 -2
View File
@@ -12,7 +12,7 @@ import { useRouter } from 'vue-router'
import { Files, Notebook, Setting } from '@element-plus/icons-vue'
import { listPapers } from '@/api/papers'
import { listSectionFields } from '@/api/sectionFields'
import { listTemplateFieldLibrary } from '@/api/templateFieldLibrary'
import { listTemplates } from '@/api/templates'
import AppLogo from '@/components/AppLogo.vue'
@@ -33,7 +33,7 @@ onMounted(async () => {
const [papers, templates, fields] = await Promise.allSettled([
listPapers({ page: 1, page_size: 1 }),
listTemplates({ page: 1, page_size: 1 }),
listSectionFields({ page: 1, page_size: 1 }),
listTemplateFieldLibrary({ page: 1, page_size: 1 }),
])
if (papers.status === 'fulfilled') paperCount.value = papers.value.total
if (templates.status === 'fulfilled') templateCount.value = templates.value.total
@@ -14,18 +14,18 @@ import { ElMessage, ElMessageBox } from 'element-plus'
import { errorMessage } from '@/api/client'
import {
batchDeleteSectionFields,
deleteSectionField,
listSectionFields,
type SectionField,
} from '@/api/sectionFields'
batchDeleteTemplateFieldLibrary,
deleteTemplateFieldLibrary,
listTemplateFieldLibrary,
type TemplateFieldLibrary,
} from '@/api/templateFieldLibrary'
import FieldFormDialog from '@/components/fields/FieldFormDialog.vue'
import { formatDateTime, levelIndent, typographyStyle } from '@/utils/format'
const loading = ref(false)
const rows = ref<SectionField[]>([])
const rows = ref<TemplateFieldLibrary[]>([])
const total = ref(0)
const selection = ref<SectionField[]>([])
const selection = ref<TemplateFieldLibrary[]>([])
const query = reactive({
keyword: '',
@@ -35,7 +35,7 @@ const query = reactive({
})
const dialogVisible = ref(false)
const editing = ref<SectionField | null>(null)
const editing = ref<TemplateFieldLibrary | null>(null)
const LEVEL_FILTERS = [
{ value: null, label: '全部等级' },
@@ -47,7 +47,7 @@ const LEVEL_FILTERS = [
async function load(): Promise<void> {
loading.value = true
try {
const result = await listSectionFields(query)
const result = await listTemplateFieldLibrary(query)
rows.value = result.items
total.value = result.total
@@ -81,12 +81,12 @@ function onCreate(): void {
dialogVisible.value = true
}
function onEdit(row: SectionField): void {
function onEdit(row: TemplateFieldLibrary): void {
editing.value = row
dialogVisible.value = true
}
async function onDelete(row: SectionField): Promise<void> {
async function onDelete(row: TemplateFieldLibrary): Promise<void> {
try {
await ElMessageBox.confirm(`确定删除字段「${row.name}」?`, '删除字段', {
type: 'warning',
@@ -98,7 +98,7 @@ async function onDelete(row: SectionField): Promise<void> {
}
try {
await deleteSectionField(row.id)
await deleteTemplateFieldLibrary(row.id)
ElMessage.success('已删除')
await load()
} catch (error) {
@@ -121,7 +121,7 @@ async function onBatchDelete(): Promise<void> {
}
try {
const result = await batchDeleteSectionFields(ids)
const result = await batchDeleteTemplateFieldLibrary(ids)
ElMessage.success(`已删除 ${result.deleted} 个字段`)
selection.value = []
await load()
@@ -194,7 +194,7 @@ onMounted(load)
row-key="id"
stripe
class="table"
@selection-change="(value: SectionField[]) => (selection = value)"
@selection-change="(value: TemplateFieldLibrary[]) => (selection = value)"
>
<el-table-column type="selection" width="46" reserve-selection />