pythia-package

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Designing 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
makes new code indistinguishable from the codebase. This skill adds what the codebase may not show: how a unit should be shaped so that callers can trust it. When the house does it differently, the house wins —
.pythia/conventions.md
first,
reference/plsql-standards.md
for everything it does not decide.
开始时声明:“正在使用 pythia-package——先设计 spec,再编写 body。”
阶段: Learn(学习)→ Ask(询问)→ Do(执行)——先了解相邻模块和调用方,约定好契约,然后将 spec 和 body 分别写入两个文件
pythia-write
能让新代码与现有代码库保持一致。本 skill 补充代码库本身可能无法体现的内容:如何塑造一个单元,让调用方能够信任它。如果团队采用不同做法,以团队做法为准——先看
.pythia/conventions.md
,其中未决定的事项再参考
reference/plsql-standards.md
。

The Iron Law

铁律

THE SPEC IS A CONTRACT: DESIGN IT FIRST, AND NEVER BREAK IT SILENTLY
A 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

工作流

  1. Learn —
    pythia similar <NAME>
    and
    pythia src
    on the top two or three;
    pythia args
    for every unit you will call;
    pythia impact <PKG>
    when the package exists (a spec change reaches all of it);
    pythia cols
    for every table touched.
  2. Shape the contract (open behaviour goes to
    pythia-spec
    first):
    • 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
      %TYPE
      ;
      IN
      by default;
      OUT
      only in procedures;
      NOCOPY
      for large
      IN OUT
      collections and LOBs; defaults only for genuinely optional inputs.
    • Functions return one thing, have no
      OUT
      parameters, and do no DML when they can be called from SQL.
    • Error contract: named exceptions in the spec, or one documented
      -20xxx
      range per package; callers can tell "not found" from "broken".
    • 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;
      DBMS_APPLICATION_INFO.SET_MODULE/SET_ACTION
      on long-running entry points so a DBA can see what runs.
    • Security:
      AUTHID DEFINER
      unless the spec needs invoker rights;
      ACCESSIBLE BY
      (12c) for internal helpers;
      DBMS_ASSERT
      for any identifier that reaches dynamic SQL; no credentials in code.
  3. Write two files — spec, then body (one object per file,
    /
    at the end,
    pythia unistr
    for Vietnamese messages). Templates in
    reference/plsql-standards.md
    ; comments through
    pythia-comment
    .
  4. Triggers stay thin: derivation and audit only — no business process, no
    COMMIT
    , 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.
  5. Check —
    pythia lint spec.sql body.sql
    , then
    pythia-review
    .
  6. Land — spec first, then body:
    pythia deploy spec.sql body.sql
    (one approval, in order) or two
    pythia apply
    runs. Exit 3 is broken, never done.
  7. Verify —
    pythia errors <PKG>
    ,
    pythia invalid
    against the baseline,
    pythia plscope <PKG>
    for unused or never-assigned identifiers.
  1. 学习——先运行
    pythia similar <NAME>
    找到最相近的两三个单元,再对这些单元运行
    pythia src
    ;对每个将要调用的单元运行
    pythia args
    ;如果 package 已存在,则运行
    pythia impact <PKG>
    (spec 变更会影响其全部内容);对涉及的每张表运行
    pythia cols
    。
  2. 塑造契约(公开行为先交给
    pythia-spec
    ):
    • 使用 package,而不是 standalone 单元,除非团队本来就编写 standalone 单元:这样既能实现封装和 private helper,也能让 body 变更不会使任何对象失效。
    • Public surface = 调用方所需。 其余内容全部设为 private,仅放在 body 中。新增 public 项放在 spec 末尾——借助细粒度依赖跟踪(11g+),现有依赖项仍保持 VALID。
    • 参数类型使用
      %TYPE
      锚定;默认使用
      IN
      ;
      OUT
      仅用于 procedure;大型
      IN OUT
      collection 和 LOB 使用
      NOCOPY
      ;仅对真正可选的输入设置默认值。
    • Function 只返回一种结果,不包含
      OUT
      参数;如果可以在 SQL 中调用,则不执行 DML。
    • 错误契约:在 spec 中定义命名异常,或者为每个 package 划定一个已文档化的
      -20xxx
      范围;让调用方能够区分“未找到”和“发生故障”。
    • 事务契约:由最外层调用方负责
      COMMIT
      。如果某个单元会执行
      COMMIT
      ,其名称或 spec 注释必须明确说明——它无法被组合到更大的事务中。
    • 日志与埋点通过团队 logger 实现;对于耗时较长的入口点,使用
      DBMS_APPLICATION_INFO.SET_MODULE/SET_ACTION
      ,使 DBA 能够看到正在运行的内容。
    • 安全:使用
      AUTHID DEFINER
      ,除非 spec 需要 invoker rights;使用
      ACCESSIBLE BY
      (12c)保护 internal helper;任何将进入 dynamic SQL 的 identifier 都应通过
      DBMS_ASSERT
      校验;代码中不得包含凭据。
  3. 编写两个文件——先写 spec,再写 body(每个文件只放一个对象,末尾加
    /
    ;越南语消息使用
    pythia unistr
    )。模板见
    reference/plsql-standards.md
    ;注释使用
    pythia-comment
    。
  4. Trigger 保持轻量:只负责数据派生和审计——不承载业务流程,不执行
    COMMIT
    ,也不调用远程系统。同一张表存在多个 timing point 时,应使用一个 compound trigger(它也能解决 ORA-04091 和 mutating table 问题)。业务规则应放在 API package 中,而不是被 trigger 隐藏起来。
  5. 检查——运行
    pythia lint spec.sql body.sql
    ,然后运行
    pythia-review
    。
  6. 落地——先 spec 后 body:运行
    pythia deploy spec.sql body.sql
    (一次审批,按该顺序执行),或者分两次运行
    pythia apply
    。退出码 3 表示内容损坏,绝不能视为完成。
  7. 验证——运行
    pythia errors <PKG>
    ,将
    pythia invalid
    的结果与基线对比,并使用
    pythia plscope <PKG>
    检查未使用或从未赋值的 identifier。

Red Flags — STOP if you catch yourself thinking

危险信号——一旦发现自己在以下想法,就立即停止

ThoughtReality
"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
RAISE
the caller believes it worked. Log, then re-raise.
"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.
DBMS_ASSERT.SQL_OBJECT_NAME
costs one call.
"Standards say X, the codebase does Y"The codebase wins; propose X separately.
想法实际情况
“我只要给现有 procedure 加一个参数就行”所有调用方都会报错或被迫重新编译。请添加 overload,或者在 spec 末尾新增一个单元。
“有
WHEN OTHERS THEN log;
就够了”
如果不带
RAISE
,调用方会以为执行成功。先记录日志,再重新抛出异常。
“为了保险,最后执行一下
COMMIT
”
现在没人能在自己的事务中调用它了。
COMMIT
应由调用方负责。
“Trigger 是实现这条规则最省事的地方”而它恰恰是数据修复时最难查找、测试和绕过的位置。应使用 API package。
“
PRAGMA AUTONOMOUS_TRANSACTION
能解决 mutating table”
它只会掩盖错误并读取陈旧数据。应使用 compound trigger。
“拼接表名没问题,反正它是内部使用的”今天是内部使用,将来可能不是。调用
DBMS_ASSERT.SQL_OBJECT_NAME
只需一次调用。
“标准说 X,代码库却采用 Y”以代码库为准;如需改进,另行提出 X。

When NOT to use this skill

何时不应使用本 skill

  • A small edit inside an existing unit →
    pythia-write
    is enough.
  • Table design →
    pythia-table
    . Performance →
    pythia-tune
    .
  • Reviewing someone else's unit →
    pythia-review
    .
  • 现有 unit 内的小改动 →
    pythia-write
    就足够了。
  • 表设计 →
    pythia-table
    。
  • 性能优化 →
    pythia-tune
    。
  • 审查他人的 unit →
    pythia-review
    。