pythia-package
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDesigning Packages, Procedures, Functions, Triggers
设计 Packages、Procedures、Functions、Triggers
Announce at start: "Using pythia-package — designing the spec before the body."
Phase: Learn → Ask → Do — neighbours and callers first, the contract agreed, then spec and body as two files
pythia-write.pythia/conventions.mdreference/plsql-standards.md开始时声明:“正在使用 pythia-package——先设计 spec,再编写 body。”
阶段: Learn(学习)→ Ask(询问)→ Do(执行)——先了解相邻模块和调用方,约定好契约,然后将 spec 和 body 分别写入两个文件
pythia-write.pythia/conventions.mdreference/plsql-standards.mdThe Iron Law
铁律
THE SPEC IS A CONTRACT: DESIGN IT FIRST, AND NEVER BREAK IT SILENTLYA package spec change invalidates every dependent at once; a body change
invalidates nothing. That asymmetry decides most of the design.
THE SPEC IS A CONTRACT: DESIGN IT FIRST, AND NEVER BREAK IT SILENTLY一次 package spec 变更会同时让所有依赖项失效,而 body 变更不会让任何依赖项失效。这种不对称性决定了大多数设计决策。
The Workflow
工作流
- Learn — and
pythia similar <NAME>on the top two or three;pythia srcfor every unit you will call;pythia argswhen the package exists (a spec change reaches all of it);pythia impact <PKG>for every table touched.pythia cols - Shape the contract (open behaviour goes to first):
pythia-spec- Package, not standalone, unless the house writes standalone units: encapsulation, private helpers, and body changes that invalidate nobody.
- Public surface = what callers need. Everything else private, in the body only. New public items go at the end of the spec — with fine-grained dependency tracking (11g+) existing dependents stay VALID.
- Parameters anchored with ;
%TYPEby default;INonly in procedures;OUTfor largeNOCOPYcollections and LOBs; defaults only for genuinely optional inputs.IN OUT - Functions return one thing, have no parameters, and do no DML when they can be called from SQL.
OUT - Error contract: named exceptions in the spec, or one documented
range per package; callers can tell "not found" from "broken".
-20xxx - Transaction contract: the outermost caller commits. A unit that commits says so in its name or its spec comment — it cannot be composed into a larger transaction.
- Logging/instrumentation through the house logger;
on long-running entry points so a DBA can see what runs.
DBMS_APPLICATION_INFO.SET_MODULE/SET_ACTION - Security: unless the spec needs invoker rights;
AUTHID DEFINER(12c) for internal helpers;ACCESSIBLE BYfor any identifier that reaches dynamic SQL; no credentials in code.DBMS_ASSERT
- Write two files — spec, then body (one object per file, at the end,
/for Vietnamese messages). Templates inpythia unistr; comments throughreference/plsql-standards.md.pythia-comment - Triggers stay thin: derivation and audit only — no business process,
no , no calls to remote systems. Several timing points on one table → one compound trigger (also the cure for ORA-04091, mutating table). Business rules belong in the API package the trigger would otherwise hide.
COMMIT - Check — , then
pythia lint spec.sql body.sql.pythia-review - Land — spec first, then body: (one approval, in order) or two
pythia deploy spec.sql body.sqlruns. Exit 3 is broken, never done.pythia apply - Verify — ,
pythia errors <PKG>against the baseline,pythia invalidfor unused or never-assigned identifiers.pythia plscope <PKG>
- 学习——先运行 找到最相近的两三个单元,再对这些单元运行
pythia similar <NAME>;对每个将要调用的单元运行pythia src;如果 package 已存在,则运行pythia args(spec 变更会影响其全部内容);对涉及的每张表运行pythia impact <PKG>。pythia cols - 塑造契约(公开行为先交给 ):
pythia-spec- 使用 package,而不是 standalone 单元,除非团队本来就编写 standalone 单元:这样既能实现封装和 private helper,也能让 body 变更不会使任何对象失效。
- Public surface = 调用方所需。 其余内容全部设为 private,仅放在 body 中。新增 public 项放在 spec 末尾——借助细粒度依赖跟踪(11g+),现有依赖项仍保持 VALID。
- 参数类型使用 锚定;默认使用
%TYPE;IN仅用于 procedure;大型OUTcollection 和 LOB 使用IN OUT;仅对真正可选的输入设置默认值。NOCOPY - Function 只返回一种结果,不包含 参数;如果可以在 SQL 中调用,则不执行 DML。
OUT - 错误契约:在 spec 中定义命名异常,或者为每个 package 划定一个已文档化的 范围;让调用方能够区分“未找到”和“发生故障”。
-20xxx - 事务契约:由最外层调用方负责 。如果某个单元会执行
COMMIT,其名称或 spec 注释必须明确说明——它无法被组合到更大的事务中。COMMIT - 日志与埋点通过团队 logger 实现;对于耗时较长的入口点,使用 ,使 DBA 能够看到正在运行的内容。
DBMS_APPLICATION_INFO.SET_MODULE/SET_ACTION - 安全:使用 ,除非 spec 需要 invoker rights;使用
AUTHID DEFINER(12c)保护 internal helper;任何将进入 dynamic SQL 的 identifier 都应通过ACCESSIBLE BY校验;代码中不得包含凭据。DBMS_ASSERT
- 编写两个文件——先写 spec,再写 body(每个文件只放一个对象,末尾加 ;越南语消息使用
/)。模板见pythia unistr;注释使用reference/plsql-standards.md。pythia-comment - Trigger 保持轻量:只负责数据派生和审计——不承载业务流程,不执行 ,也不调用远程系统。同一张表存在多个 timing point 时,应使用一个 compound trigger(它也能解决 ORA-04091 和 mutating table 问题)。业务规则应放在 API package 中,而不是被 trigger 隐藏起来。
COMMIT - 检查——运行 ,然后运行
pythia lint spec.sql body.sql。pythia-review - 落地——先 spec 后 body:运行 (一次审批,按该顺序执行),或者分两次运行
pythia deploy spec.sql body.sql。退出码 3 表示内容损坏,绝不能视为完成。pythia apply - 验证——运行 ,将
pythia errors <PKG>的结果与基线对比,并使用pythia invalid检查未使用或从未赋值的 identifier。pythia plscope <PKG>
Red Flags — STOP if you catch yourself thinking
危险信号——一旦发现自己在以下想法,就立即停止
| Thought | Reality |
|---|---|
| "I'll just add a parameter to the existing procedure" | Every caller breaks or recompiles. Add an overload or a new unit at the end of the spec. |
| "WHEN OTHERS THEN log; is enough" | Without |
| "COMMIT at the end to be safe" | Now nobody can call it inside their transaction. The caller owns the commit. |
| "The trigger is the easiest place for this rule" | And the hardest to find, test and bypass for a data fix. API package. |
| "PRAGMA AUTONOMOUS_TRANSACTION fixes the mutating table" | It hides the error and reads stale data. Compound trigger. |
| "Concatenating the table name is fine, it's internal" | Internal today. |
| "Standards say X, the codebase does Y" | The codebase wins; propose X separately. |
| 想法 | 实际情况 |
|---|---|
| “我只要给现有 procedure 加一个参数就行” | 所有调用方都会报错或被迫重新编译。请添加 overload,或者在 spec 末尾新增一个单元。 |
“有 | 如果不带 |
“为了保险,最后执行一下 | 现在没人能在自己的事务中调用它了。 |
| “Trigger 是实现这条规则最省事的地方” | 而它恰恰是数据修复时最难查找、测试和绕过的位置。应使用 API package。 |
“ | 它只会掩盖错误并读取陈旧数据。应使用 compound trigger。 |
| “拼接表名没问题,反正它是内部使用的” | 今天是内部使用,将来可能不是。调用 |
| “标准说 X,代码库却采用 Y” | 以代码库为准;如需改进,另行提出 X。 |
When NOT to use this skill
何时不应使用本 skill
- A small edit inside an existing unit → is enough.
pythia-write - Table design → . Performance →
pythia-table.pythia-tune - Reviewing someone else's unit → .
pythia-review
- 现有 unit 内的小改动 → 就足够了。
pythia-write - 表设计 → 。
pythia-table - 性能优化 → 。
pythia-tune - 审查他人的 unit → 。
pythia-review