schemadoc

package
v0.9.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schemadoc 实现 appkit schema:把 db/migrations 派生成一份可 review、 可 grep、GitHub 直接渲染的 schema 视图(db/SCHEMA.md + db/schema/)。

为什么需要它:迁移是不可变的追加日志(pgmigrate 存 sha256,改动已应用文件即 MIGRATION_DRIFT 拒启动),一张表的真实形状因此散落在 N 个文件里——0003 建表、 0007 加列、0012 改默认值。想看「这个域的 schema 长什么样」,只能在脑子里重放 整条迁移历史。本包把那次重放交给 Postgres 做一遍,再把结果写成文件。

事实源仍然是 db/migrations:本包的产出全是生成物,带 DO NOT EDIT 头, 由 appkit schema -check 守住不漂移。

包内分工(这个接缝是刻意的):

  • introspect.go 是唯一需要数据库的文件,产出纯粹的 Schema 值;
  • render.go / diagram.go 是 Schema 的纯函数,因此渲染、分簇、漂移比对 全部可以用零 DB 的单测覆盖。

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAdopted = errors.New("本仓库尚未启用 schema 文档")

ErrNotAdopted 表示仓库尚未启用 schema 文档:db/SCHEMA.md 与 db/schema/ 都不存在。

这是一个「启用门」,不是疏漏。domain-ci.yml 经 @main 被全部存量域仓库共享, 新增一个硬检查会让它们在合并那一刻集体变红;而 appkit new 必须离线可跑, 生成不出需要数据库的产物,新仓库首推的 CI 也不该是红的。

代价是诚实的:从不启用的仓库永远不被检查(docs/DESIGN.md §7 记 ▲ 不记 ★)。 自愈性在于——跑过一次 make schema 之后就永久转严,而删目录逃检查是 diff 里看得见的删除。

Functions

func Adopted

func Adopted(dir string) (bool, error)

Adopted 报告仓库是否已启用:任一产出存在即算启用,两者都不在才是未启用。 只删其中一半不能让检查静音——那样「删掉就不检查了」会变成一条真实的逃生路。

它是纯文件系统判断、不连库,所以调用方可以在要求 DSN **之前**先问一次: 未启用的仓库不该为了被告知「未启用」而先去准备一个数据库。

func Check

func Check(ctx context.Context, o Options) ([]string, error)

Check 比对磁盘上的产出与重新生成的内容,漂移时返回错误。 两份产出都不存在(尚未启用)时返回 ErrNotAdopted,由调用方决定如何处理。

无论是否漂移,都一并返回缺 COMMENT ON TABLE 的业务表清单——那是软约束 (渲染层标 ⚠,CI 打 ::warning),不该让检查变红,但值得被点名。

func Generate

func Generate(ctx context.Context, o Options) ([]string, error)

Generate 重新生成 schema 文档并写入 o.Dir,返回写入的仓库相对路径(有序)。 db/schema/ 下不再对应任何表的陈旧文件会被删除(迁移删表后必须跟着消失)。

func MissingComments

func MissingComments(s Schema) []string

MissingComments 返回缺 COMMENT ON TABLE 的业务表(schema.table 形式,有序)。 谓词必须与 render.go 打 ⚠ 的那条保持一致:文档里标了 ⚠ 的表就是这里点名的表。 框架表不算——它的 DDL 归 appkit 库函数维护,域仓库改不动自己那份已应用的 0001。

func Render

func Render(s Schema) (map[string]string, error)

Render 把 Schema 渲染成全部产出(键为仓库相对斜杠路径)。

纯函数:同一个 Schema 逐字节稳定,因此渲染、分簇、漂移比对都能用零 DB 的 单测覆盖,而不必先起一个 Postgres 才敢改一行 Markdown。

Types

type Column

type Column struct {
	Name     string
	Type     string // format_type 的输出,如 numeric(19,4)
	NotNull  bool
	Default  string
	Identity string
	Comment  string
}

Column 是一列。Default 为空表示无默认值; Identity 非空时是 "GENERATED ALWAYS AS IDENTITY" 这样的原文子句。

type Constraint

type Constraint struct {
	Name       string
	Type       ConstraintType
	Def        string
	Columns    []string // p/u/f 的本表列
	RefTable   string   // 仅 f
	RefColumns []string // 仅 f
}

Constraint 是一条表约束。Def 是 pg_get_constraintdef 的原文——难的部分一律 交给 Postgres 自己渲染,本包只在需要结构化信息(画 ER 图、标注列)时才拆解。

type ConstraintType

type ConstraintType string

ConstraintType 对应 pg_constraint.contype 中本包支持的四种。

const (
	ConstraintPrimaryKey ConstraintType = "p"
	ConstraintUnique     ConstraintType = "u"
	ConstraintForeignKey ConstraintType = "f"
	ConstraintCheck      ConstraintType = "c"
)

type Enum

type Enum struct {
	Name   string
	Values []string
}

Enum 是一个枚举类型。少了它,schema 视图里会出现一个叫 order_status 的类型 却说不出它能取哪些值——那正是本包要消灭的东西。

type Function

type Function struct {
	Name      string
	Signature string // 参数列表,如 "id uuid, at timestamptz"
	Returns   string
}

Function 是 schema 里的一个函数/存储过程,只记签名不记函数体。

记它是为了不静默漏东西:本包的产出叫「schema 视图」,一个 schema 里有函数 却只字不提,读者会以为没有。函数体不进产出——那是代码,事实源在迁移文件里。

type Index

type Index struct {
	Name string
	Def  string
}

Index 是一个非约束附带的索引,Def 为 pg_get_indexdef 原文。

type Options

type Options struct {
	Dir    string // 仓库根目录
	DSN    string // 任一 Postgres 连接串;迁移会应用到由它派生的一次性临时库
	Schema string // 域 schema 名(.appkit.yml 的 domain)
	// Partitioned documents one logical template in Schema, not live partitions.
	// Prefixless migrations are applied once through the normal migration runner.
	Partitioned bool
	// Migrations supplies a caller-captured filesystem rooted at migration SQL
	// filenames. When non-nil, Introspect never reads Dir or reopens source paths.
	// The caller must keep this snapshot immutable for the duration of the call.
	// Nil retains the existing Dir/db/migrations behavior.
	Migrations fs.FS
}

Options 是一次生成/检查的输入。

type RLS added in v0.7.0

type RLS struct {
	Enabled  bool
	Force    bool
	Policies []RLSPolicy
}

RLS 是一张表的行级安全状态。租户域的业务表经 pgtx.TenantPolicySQL 挂上 ENABLE/FORCE/策略三件套;这里如实渲染进 db/schema/<表>.sql——策略被删、 被改名、FORCE 被摘,schema 文档与 CI 漂移检查都会跟着变红。

type RLSPolicy added in v0.7.0

type RLSPolicy struct {
	Name      string
	Cmd       string // ALL / SELECT / INSERT / UPDATE / DELETE
	Roles     string // 数组字面量,如 {PUBLIC}
	Using     string
	WithCheck string
}

RLSPolicy 对应 pg_policies 的一行,表达式是 Postgres 展开后的文本。

type Schema

type Schema struct {
	Name      string
	Tables    []Table    // 按 Name 升序
	Enums     []Enum     // 按 Name 升序
	Functions []Function // 按 Name 升序
	// LogicalTemplate marks a representative partitioned-domain model. It does
	// not claim that any deployed partition exists or matches this snapshot.
	LogicalTemplate bool
}

Schema 是一个域 schema 的完整结构快照,也是全部渲染的唯一输入。

func Introspect

func Introspect(ctx context.Context, o Options) (result Schema, resultErr error)

Introspect 把 db/migrations 应用到一次性临时库,再从 pg_catalog 读回结构。

为什么建临时库而不是直接读 o.DSN 指的库:产出必须是 db/migrations 的纯函数。 直接读现有开发库的话,谁手工 ALTER 过一次,生成物就跟着歪,而 CI 上是干净库, 于是本地绿、CI 红,且没人看得出为什么。用一个新库名建库也不可能毁掉已有数据。

为什么不用 pg_dump:它要求客户端版本 ≥ 服务端(CI runner 上的 client 可能低于 postgres:18),要额外装二进制,而关系图需要的外键结构无论如何都得查 catalog。 CLI 本来就已经链接了 pgx,这条路零新依赖。

type Table

type Table struct {
	Name        string
	Kind        TableKind
	Comment     string
	Columns     []Column
	Constraints []Constraint
	Indexes     []Index
	Triggers    []Trigger
	ViewDef     string // 仅 view/matview
	RLS         *RLS   // 开了 RLS 或挂了策略时非 nil(见 RLS)
}

Table 是一张表/视图。Comment 为空即「缺说明」,渲染时标注 ⚠—— 表的用途属于 schema 设计,写在迁移的 COMMENT ON TABLE 里才会跟着表一起演进。

func (Table) Framework

func (t Table) Framework() bool

Framework 报告本表是否为 appkit 基础设施表。

type TableKind

type TableKind string

TableKind 区分普通表与(物化)视图。本包只渲染这三种,其余一律报错。

const (
	KindTable   TableKind = "table"
	KindView    TableKind = "view"
	KindMatView TableKind = "matview"
)

type Trigger

type Trigger struct {
	Name string
	Def  string
}

Trigger 是一个触发器,Def 为 pg_get_triggerdef 原文。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL