@mrciphersmith/keryx 0.4.29 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/dist/cli.js +2408 -1369
  2. package/dist/core.js +90 -27
  3. package/dist/keryx-general-worker-entry.js +82 -25
  4. package/package.json +1 -1
  5. package/src/gdskills/bundled/install-manifest.json +2 -0
  6. package/src/gdskills/bundled/rules/core/api-contracts.mdc +21 -136
  7. package/src/gdskills/bundled/rules/core/async-patterns.mdc +18 -171
  8. package/src/gdskills/bundled/rules/core/clean-architecture.mdc +21 -3
  9. package/src/gdskills/bundled/rules/core/cli-interface-design.mdc +63 -194
  10. package/src/gdskills/bundled/rules/core/code-style-patterns.mdc +36 -49
  11. package/src/gdskills/bundled/rules/core/concurrency-patterns.mdc +32 -0
  12. package/src/gdskills/bundled/rules/core/database-patterns.mdc +26 -148
  13. package/src/gdskills/bundled/rules/core/error-handling.mdc +7 -8
  14. package/src/gdskills/bundled/rules/core/frontend-assistant.mdc +14 -29
  15. package/src/gdskills/bundled/rules/core/layout-rendering.mdc +51 -0
  16. package/src/gdskills/bundled/rules/core/mobx-store-template.mdc +69 -511
  17. package/src/gdskills/bundled/rules/core/nestjs-dto.mdc +18 -36
  18. package/src/gdskills/bundled/rules/core/playwright-testing.mdc +35 -308
  19. package/src/gdskills/bundled/rules/core/security-baseline.mdc +18 -140
  20. package/src/gdskills/bundled/rules/core/solid-principles.mdc +31 -7
  21. package/src/gdskills/bundled/rules/core/storybook-guidelines.mdc +20 -22
  22. package/src/gdskills/bundled/rules/core/tdd-workflow.mdc +37 -74
  23. package/src/gdskills/bundled/rules/core/ui-anti-slop.mdc +48 -37
  24. package/src/gdskills/bundled/skills/quality/deprecation-path/SKILL.md +1 -1
  25. package/src/gdskills/bundled/skills/review/review-architecture/SKILL.md +24 -60
  26. package/src/gdskills/bundled/skills/review/review-backend/SKILL.md +13 -65
  27. package/src/gdskills/bundled/skills/review/review-clean-code/SKILL.md +22 -35
  28. package/src/gdskills/bundled/skills/review/review-frontend/SKILL.md +16 -309
  29. package/src/gdskills/bundled/skills/review/review-frontend-conventions/SKILL.md +7 -21
  30. package/src/gdskills/bundled/skills/review/review-highload/SKILL.md +83 -227
  31. package/src/gdskills/bundled/skills/review/review-layout/SKILL.md +22 -28
  32. package/src/gdskills/bundled/skills/review/review-logic/SKILL.md +8 -8
  33. package/src/gdskills/bundled/skills/review/review-performance/SKILL.md +15 -37
  34. package/src/gdskills/bundled/skills/review/review-security-code/SKILL.md +7 -7
  35. package/src/gdskills/bundled/skills/review/review-style/SKILL.md +31 -66
  36. package/src/gdskills/bundled/skills/review/review-testing-practices/SKILL.md +21 -39
  37. package/src/gdskills/bundled/skills/review/review-ui-ux/SKILL.md +42 -82
  38. package/src/gdskills/bundled/stacks/angular/rules/coding-style.mdc +15 -57
  39. package/src/gdskills/bundled/stacks/angular/rules/patterns.mdc +16 -60
  40. package/src/gdskills/bundled/stacks/angular/rules/security.mdc +11 -49
  41. package/src/gdskills/bundled/stacks/angular/rules/testing.mdc +13 -52
  42. package/src/gdskills/bundled/stacks/c-cpp/rules/coding-style.mdc +33 -57
  43. package/src/gdskills/bundled/stacks/c-cpp/rules/patterns.mdc +35 -62
  44. package/src/gdskills/bundled/stacks/c-cpp/rules/security.mdc +35 -62
  45. package/src/gdskills/bundled/stacks/c-cpp/rules/testing.mdc +34 -54
  46. package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/patterns.mdc +27 -56
  47. package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/security.mdc +51 -119
  48. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/coding-style.mdc +42 -74
  49. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/patterns.mdc +45 -84
  50. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/security.mdc +34 -59
  51. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/testing.mdc +33 -62
  52. package/src/gdskills/bundled/stacks/django/rules/coding-style.mdc +15 -56
  53. package/src/gdskills/bundled/stacks/django/rules/patterns.mdc +16 -68
  54. package/src/gdskills/bundled/stacks/django/rules/security.mdc +13 -71
  55. package/src/gdskills/bundled/stacks/django/rules/testing.mdc +16 -62
  56. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/coding-style.mdc +30 -53
  57. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/patterns.mdc +20 -56
  58. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/security.mdc +52 -122
  59. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/testing.mdc +23 -42
  60. package/src/gdskills/bundled/stacks/fastapi/rules/coding-style.mdc +13 -47
  61. package/src/gdskills/bundled/stacks/fastapi/rules/patterns.mdc +18 -81
  62. package/src/gdskills/bundled/stacks/fastapi/rules/security.mdc +13 -75
  63. package/src/gdskills/bundled/stacks/fastapi/rules/testing.mdc +14 -58
  64. package/src/gdskills/bundled/stacks/flutter-dart/rules/coding-style.mdc +36 -74
  65. package/src/gdskills/bundled/stacks/flutter-dart/rules/patterns.mdc +30 -66
  66. package/src/gdskills/bundled/stacks/flutter-dart/rules/security.mdc +33 -67
  67. package/src/gdskills/bundled/stacks/flutter-dart/rules/testing.mdc +41 -73
  68. package/src/gdskills/bundled/stacks/go/rules/coding-style.mdc +36 -57
  69. package/src/gdskills/bundled/stacks/go/rules/patterns.mdc +27 -42
  70. package/src/gdskills/bundled/stacks/go/rules/security.mdc +26 -43
  71. package/src/gdskills/bundled/stacks/go/rules/testing.mdc +23 -41
  72. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/coding-style.mdc +18 -37
  73. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/patterns.mdc +26 -43
  74. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/security.mdc +25 -37
  75. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/testing.mdc +34 -50
  76. package/src/gdskills/bundled/stacks/kotlin-android/rules/coding-style.mdc +28 -65
  77. package/src/gdskills/bundled/stacks/kotlin-android/rules/patterns.mdc +34 -78
  78. package/src/gdskills/bundled/stacks/kotlin-android/rules/security.mdc +35 -68
  79. package/src/gdskills/bundled/stacks/kotlin-android/rules/testing.mdc +34 -68
  80. package/src/gdskills/bundled/stacks/mobx/rules/coding-style.mdc +18 -64
  81. package/src/gdskills/bundled/stacks/mobx/rules/patterns.mdc +13 -66
  82. package/src/gdskills/bundled/stacks/mobx/rules/security.mdc +8 -35
  83. package/src/gdskills/bundled/stacks/mobx/rules/testing.mdc +10 -39
  84. package/src/gdskills/bundled/stacks/nestjs/rules/coding-style.mdc +32 -45
  85. package/src/gdskills/bundled/stacks/nestjs/rules/patterns.mdc +40 -60
  86. package/src/gdskills/bundled/stacks/nestjs/rules/security.mdc +30 -50
  87. package/src/gdskills/bundled/stacks/nestjs/rules/testing.mdc +27 -47
  88. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/coding-style.mdc +14 -48
  89. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/patterns.mdc +15 -67
  90. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/security.mdc +12 -50
  91. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/testing.mdc +11 -43
  92. package/src/gdskills/bundled/stacks/php-laravel/rules/coding-style.mdc +17 -58
  93. package/src/gdskills/bundled/stacks/php-laravel/rules/patterns.mdc +15 -56
  94. package/src/gdskills/bundled/stacks/php-laravel/rules/security.mdc +13 -50
  95. package/src/gdskills/bundled/stacks/php-laravel/rules/testing.mdc +14 -55
  96. package/src/gdskills/bundled/stacks/python/rules/coding-style.mdc +13 -41
  97. package/src/gdskills/bundled/stacks/python/rules/patterns.mdc +20 -61
  98. package/src/gdskills/bundled/stacks/python/rules/security.mdc +15 -57
  99. package/src/gdskills/bundled/stacks/python/rules/testing.mdc +16 -50
  100. package/src/gdskills/bundled/stacks/react/rules/coding-style.mdc +14 -40
  101. package/src/gdskills/bundled/stacks/react/rules/patterns.mdc +15 -55
  102. package/src/gdskills/bundled/stacks/react/rules/security.mdc +13 -49
  103. package/src/gdskills/bundled/stacks/react/rules/testing.mdc +10 -37
  104. package/src/gdskills/bundled/stacks/ruby-rails/rules/coding-style.mdc +12 -45
  105. package/src/gdskills/bundled/stacks/ruby-rails/rules/patterns.mdc +18 -67
  106. package/src/gdskills/bundled/stacks/ruby-rails/rules/security.mdc +11 -60
  107. package/src/gdskills/bundled/stacks/ruby-rails/rules/testing.mdc +14 -65
  108. package/src/gdskills/bundled/stacks/rust/rules/coding-style.mdc +39 -65
  109. package/src/gdskills/bundled/stacks/rust/rules/patterns.mdc +33 -60
  110. package/src/gdskills/bundled/stacks/rust/rules/security.mdc +35 -57
  111. package/src/gdskills/bundled/stacks/rust/rules/testing.mdc +29 -52
  112. package/src/gdskills/bundled/stacks/sql-db/rules/coding-style.mdc +12 -48
  113. package/src/gdskills/bundled/stacks/sql-db/rules/patterns.mdc +22 -113
  114. package/src/gdskills/bundled/stacks/sql-db/rules/security.mdc +11 -56
  115. package/src/gdskills/bundled/stacks/sql-db/rules/testing.mdc +14 -59
  116. package/src/gdskills/bundled/stacks/swift-ios/rules/coding-style.mdc +29 -65
  117. package/src/gdskills/bundled/stacks/swift-ios/rules/patterns.mdc +41 -88
  118. package/src/gdskills/bundled/stacks/swift-ios/rules/security.mdc +28 -53
  119. package/src/gdskills/bundled/stacks/swift-ios/rules/testing.mdc +32 -65
  120. package/src/gdskills/bundled/stacks/vue/rules/coding-style.mdc +17 -52
  121. package/src/gdskills/bundled/stacks/vue/rules/patterns.mdc +17 -63
  122. package/src/gdskills/bundled/stacks/vue/rules/security.mdc +11 -41
  123. package/src/gdskills/bundled/stacks/vue/rules/testing.mdc +16 -48
@@ -12,71 +12,48 @@ Narrows `core-common-rules`' stack-agnostic testing rules to Rust's built-in
12
12
 
13
13
  ## Layout and naming
14
14
 
15
- - Unit tests live in a `#[cfg(test)] mod tests { use super::*; ... }` block
16
- at the bottom of the file they test, giving them access to private
17
- items; integration tests (exercising only the crate's public API) live
18
- under `tests/` at the crate root, one file per test binary.
19
- - Name a test function for the behavior it verifies
20
- (`rejects_negative_amount`, not `test1` or `test_validate`); group
21
- related cases as separate `#[test]` functions or, for many similar
22
- inputs, a parameterized loop/table within one function.
23
- - Fixture/golden data a test reads lives under `tests/fixtures/` (or a
24
- crate-local `testdata/` if the project already uses that convention) —
25
- match whatever the crate already does rather than inventing a new one.
15
+ - Unit tests in a `#[cfg(test)] mod tests { use super::*; ... }` block at the
16
+ bottom of the file; integration tests under `tests/` at the crate root,
17
+ one file per test binary.
18
+ - Name a test for the behavior it verifies (`rejects_negative_amount`, not
19
+ `test1`).
20
+ - Fixture/golden data under `tests/fixtures/` (or `testdata/` if the project
21
+ already uses it); match the crate's existing convention.
26
22
 
27
23
  ## Test structure
28
24
 
29
- - Prefer a small table of cases (a `Vec` of a local struct, or a slice of
30
- tuples) iterated in one `#[test]` function over many near-duplicate test
31
- functions, when the cases share the same assertion shape — mirrors the
32
- table-driven convention from other stacks, adapted to Rust's lack of a
33
- built-in subtest runner.
34
- - Assert with `assert_eq!`/`assert!`/`assert_ne!` from `std`, or the
35
- project's already-adopted assertion crate (`pretty_assertions`,
36
- `assert_matches`) — do not introduce a new assertion library into a
37
- crate that has not adopted one.
25
+ - A small table of cases iterated in one `#[test]` over many near-duplicate
26
+ functions when cases share an assertion shape.
27
+ - `assert_eq!`/`assert!`/`assert_ne!` from `std`, or the project's adopted
28
+ assertion crate (`pretty_assertions`, `assert_matches`); do not add a new
29
+ one.
38
30
  - For a `Result`-returning function, assert the specific error variant
39
- (`assert!(matches!(err, MyError::NotFound { .. }))` or
40
- `assert_eq!(err, MyError::NotFound)` when the error implements
41
- `PartialEq`) rather than only `assert!(result.is_err())`, which passes
42
- for the wrong failure just as readily as the right one.
31
+ (`matches!(err, MyError::NotFound { .. })`, or `assert_eq!` when
32
+ `PartialEq`), not only `result.is_err()`.
43
33
 
44
34
  ## Async tests
45
35
 
46
- - `#[tokio::test]` (or the project's already-configured async test
47
- attribute) for an `async fn` test body; never block a `#[test]`
48
- function's thread on a future with a hand-rolled executor when the
49
- project already depends on `tokio`.
50
- - Join spawned tasks with `.await` on their `JoinHandle`, or a channel,
51
- before asserting — never synchronize with `std::thread::sleep`/
52
- `tokio::time::sleep` to "give a spawned task time to finish"; that is
53
- non-deterministic under load and flakes in CI exactly like a Go test
54
- that sleeps instead of joining a goroutine.
36
+ - `#[tokio::test]` (or the project's configured async test attribute) for
37
+ async test bodies; no hand-rolled executor when the project depends on
38
+ `tokio`.
39
+ - Join spawned tasks (`JoinHandle` `.await`, or a channel) before asserting;
40
+ never `std::thread::sleep`/`tokio::time::sleep` to wait for a task.
55
41
 
56
42
  ## Property-based and fuzz testing
57
43
 
58
- - Reach for `proptest` or `quickcheck` (whichever the crate already
59
- depends on) for a function with a large, structured input space
60
- (a parser, a serializer round-trip) instead of hand-writing dozens of
61
- near-identical example-based cases.
62
- - `cargo fuzz` (built on `libFuzzer`) for a parser or decoder that touches
63
- untrusted input formats directly; note in the report that fuzzing runs
64
- separately from `cargo test` and is not part of the default CI test run
65
- unless the project has wired it in.
44
+ - `proptest` or `quickcheck` (whichever the crate depends on) for a large
45
+ structured input space (a parser, a serializer round-trip).
46
+ - `cargo fuzz` (`libFuzzer`) for a parser or decoder of untrusted input;
47
+ note in the report that it runs separately from `cargo test` and is not in
48
+ the default CI run unless wired in.
66
49
 
67
50
  ## Benchmarks
68
51
 
69
- - Use `criterion` (the de facto ecosystem standard) for a benchmark that
70
- needs statistical rigor (warm-up, outlier detection, regression
71
- comparison across runs) rather than the unstable built-in `#[bench]`
72
- attribute, which requires nightly.
52
+ - `criterion` for benchmarks needing statistical rigor, not the nightly-only
53
+ built-in `#[bench]`.
73
54
 
74
55
  ## Running and determinism
75
56
 
76
- - Run `cargo test` (add `--all-features`/`--workspace` when the project's
77
- CI does) before reporting tests as passing; a test relying on wall-clock
78
- timing, real network access, or shared global state is a flake risk —
79
- prefer a fake clock, a mock boundary, or `#[serial]` (from the `serial_test`
80
- crate) for tests that must not run concurrently.
81
- - New behavior gets a new test in the same change; a bug fix gets a
82
- regression test that fails before the fix and passes after.
57
+ - Run `cargo test` (add `--all-features`/`--workspace` when CI does) before
58
+ reporting tests as passing; for wall-clock, real-network, or shared-global
59
+ state use a fake clock, a mock boundary, or `#[serial]` (`serial_test`).
@@ -7,63 +7,27 @@ metadata:
7
7
 
8
8
  # SQL coding style
9
9
 
10
- Narrows `core-common-rules`' stack-agnostic style rules to raw `.sql` files
11
- (schema definitions, migrations, hand-written query files) across Postgres,
12
- MySQL, and generic SQL engines. Applies only to `*.sql` files — application
13
- code that embeds SQL as strings (a Python/Go/JS query builder call) is
14
- covered by that language's own stack pack, not this one.
10
+ Narrows `core-common-rules`' style rules to raw `.sql` files (schema, migrations, hand-written queries) across Postgres, MySQL and generic SQL. Applies only to `*.sql` files; SQL embedded in application code follows that language's stack pack.
15
11
 
16
12
  ## Naming
17
13
 
18
- - Tables and columns are `snake_case`, plural for tables (`orders`,
19
- `order_items`), singular for columns (`user_id`, not `user_ids` on a
20
- scalar foreign key) — match whatever convention the project's existing
21
- schema already uses instead of introducing a second one.
22
- - Name a foreign-key column `<singular_referenced_table>_id`
23
- (`orders.user_id` referencing `users.id`), so its relationship is
24
- readable without opening the schema.
25
- - Name an index `idx_<table>_<column(s)>` and a unique constraint
26
- `uq_<table>_<column(s)>`; name a foreign key constraint
27
- `fk_<table>_<referenced_table>` — explicit names are required so a
28
- failing constraint's error message names something a reader recognizes,
29
- instead of an engine-generated identifier like `orders_user_id_fkey1`.
14
+ - Tables and columns are `snake_case`, plural for tables (`orders`, `order_items`), singular for columns; match the project's existing schema convention.
15
+ - Name a foreign-key column `<singular_referenced_table>_id` (`orders.user_id` referencing `users.id`).
16
+ - Name an index `idx_<table>_<column(s)>`, a unique constraint `uq_<table>_<column(s)>`, and a foreign key constraint `fk_<table>_<referenced_table>`; do not leave engine-generated names like `orders_user_id_fkey1`.
30
17
 
31
18
  ## Formatting
32
19
 
33
- - One clause per line for anything beyond a trivial single-table query —
34
- `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `ORDER BY` each start their own
35
- line, with join conditions immediately under their `JOIN`. A query dense
36
- enough to need review is dense enough to need this shape.
37
- - Keywords uppercase (`SELECT`, `FROM`, `WHERE`, `JOIN`), identifiers
38
- lowercase — do not mix case within one file's own convention.
39
- - Explicit `JOIN ... ON` conditions, never an implicit comma join
40
- (`FROM a, b WHERE a.id = b.a_id`) — the join type and its condition
41
- belong together, not split across `FROM` and `WHERE`.
42
- - Every migration file's DDL statements are wrapped in the smallest
43
- transaction the engine allows (Postgres: a single implicit transaction
44
- per migration is usually safe except around `CREATE INDEX CONCURRENTLY`,
45
- which cannot run inside a transaction block at all — split it into its
46
- own migration step rather than wrapping it anyway).
20
+ - One clause per line beyond a trivial single-table query (`SELECT`, `FROM`, `WHERE`, `GROUP BY`, `ORDER BY`), with join conditions directly under their `JOIN`.
21
+ - Keywords uppercase, identifiers lowercase.
22
+ - Explicit `JOIN ... ON`, never an implicit comma join.
23
+ - Wrap a migration's DDL in the smallest transaction the engine allows; `CREATE INDEX CONCURRENTLY` cannot run in a transaction block, so give it its own migration step.
47
24
 
48
25
  ## Column and type choices
49
26
 
50
- - Pick the narrowest correct type: an `enum`/`CHECK`-constrained text
51
- column over a free `VARCHAR` for a closed set of values, `TIMESTAMPTZ`
52
- (Postgres) / a UTC-normalized `DATETIME` (MySQL) over a naive local-time
53
- column, `NUMERIC`/`DECIMAL` for money — never `FLOAT`/`DOUBLE` for a
54
- value that must round exactly.
55
- - A nullable column means "this can genuinely be absent," not "I didn't
56
- decide yet" — a column with no legitimate absent case gets `NOT NULL`
57
- from the migration that introduces it (with a default, or through the
58
- safe multi-step pattern in `rules/patterns.mdc` when the table already
59
- has rows).
27
+ - Pick the narrowest correct type: an `enum`/`CHECK`-constrained text column over free `VARCHAR` for a closed set, `TIMESTAMPTZ` (Postgres) / UTC-normalized `DATETIME` (MySQL) over naive local time, `NUMERIC`/`DECIMAL` for money, never `FLOAT`/`DOUBLE` for values that must round exactly.
28
+ - A nullable column means the value can genuinely be absent; otherwise add `NOT NULL` in the introducing migration (with a default, or the multi-step pattern in `rules/patterns.mdc` when the table has rows).
60
29
 
61
30
  ## Comments and documentation
62
31
 
63
- - A non-obvious index (a partial index's predicate, a composite index
64
- whose column order looks surprising) gets a one-line comment stating
65
- which query it serves — an index with no stated purpose is a maintenance
66
- liability the next person can't safely drop or reorder.
67
- - A migration that is not naturally reversible (a data-destructive
68
- backfill, a column drop) states its rollback plan or its "no rollback,
69
- here is why" reasoning in a comment at the top of the file.
32
+ - Comment a non-obvious index (a partial index's predicate, a surprising composite column order) with one line stating which query it serves.
33
+ - A migration that is not naturally reversible states its rollback plan, or why there is none, in a comment at the top of the file.
@@ -7,128 +7,37 @@ metadata:
7
7
 
8
8
  # SQL patterns
9
9
 
10
- Narrows `core-common-rules`' stack-agnostic design guidance to migration
11
- safety, query design, and indexing for `.sql` files across Postgres,
12
- MySQL, and generic SQL engines. Extends the general N+1/index/migration
13
- guidance in the core `database-patterns.mdc` rule with engine-specific
14
- mechanics for the zero-downtime cases that rule states only as a general
15
- three-step pattern. Applies only to `*.sql` files.
10
+ Narrows `core-common-rules`' design guidance to migration safety, query design and indexing for `.sql` files across Postgres, MySQL and generic SQL. Adds engine-specific mechanics to the core `database-patterns.mdc` rule's general three-step zero-downtime pattern. Applies only to `*.sql` files.
16
11
 
17
12
  ## Zero-downtime schema changes
18
13
 
19
- - **Adding a nullable column** is always safe as a single migration on
20
- both engines.
21
- - **Adding a `NOT NULL` column with a constant default** does not require
22
- a full table rewrite on modern Postgres (11+) or MySQL (8.0+, via
23
- `ALGORITHM=INSTANT` for an appended column) — prefer `ADD COLUMN ...
24
- NOT NULL DEFAULT <constant>` directly over the older three-step
25
- add-nullable/backfill/constrain dance when every existing and new row
26
- can share the same default value.
27
- - **Adding `NOT NULL` to an existing column that needs a real backfill**
28
- (not a constant default) still needs the multi-step sequence: add the
29
- column nullable, deploy code that writes it on every insert/update,
30
- backfill existing rows in batches (not one giant `UPDATE`, which holds
31
- locks and generates a huge amount of WAL/binlog), then constrain.
32
- - Postgres: prefer `ALTER TABLE t ADD CONSTRAINT t_col_not_null CHECK
33
- (col IS NOT NULL) NOT VALID;` followed by a separate `ALTER TABLE t
34
- VALIDATE CONSTRAINT t_col_not_null;` over a direct `ALTER TABLE t
35
- ALTER COLUMN col SET NOT NULL` — `NOT VALID` skips the initial
36
- table scan (applies only to new rows immediately), and
37
- `VALIDATE CONSTRAINT` then scans under `SHARE UPDATE EXCLUSIVE`,
38
- which does not block concurrent reads/writes the way the scan inside
39
- a plain `SET NOT NULL` does.
40
- - MySQL: `ALTER TABLE t MODIFY COLUMN col <type> NOT NULL` is an
41
- in-place operation but still requires a full table scan/rebuild on
42
- most storage engines — schedule it like any table-rewriting DDL
43
- (off-peak, or through the project's online-migration tool such as
44
- `gh-ost`/gantry-style tooling if one is already in use).
45
- - **Adding an index on an existing table**: Postgres —
46
- `CREATE INDEX CONCURRENTLY` (cannot run inside a transaction block; put
47
- it in its own migration step) to avoid the `ACCESS EXCLUSIVE`-adjacent
48
- write lock a plain `CREATE INDEX` takes. MySQL — `CREATE INDEX` on
49
- InnoDB uses `ALGORITHM=INPLACE` by default for most index additions,
50
- which permits concurrent DML; verify with `ALGORITHM=INPLACE, LOCK=NONE`
51
- explicitly if the table is under sustained write load.
52
- - **Renaming or changing a column's type**: no engine makes this safe as
53
- one step under a rolling deploy — add the new column, dual-write from
54
- application code, backfill, cut reads over, then drop the old column in
55
- a later migration once nothing reads it.
56
- - **Dropping a column or table**: stop reading/writing it from application
57
- code first (a full deploy cycle), then drop it in a subsequent
58
- migration — dropping while old code still reads the column breaks that
59
- code immediately on deploy overlap.
14
+ - **Adding a nullable column** is a single migration on both engines.
15
+ - **Adding a `NOT NULL` column with a constant default**: prefer `ADD COLUMN ... NOT NULL DEFAULT <constant>` directly (no table rewrite on Postgres 11+ or MySQL 8.0+ with `ALGORITHM=INSTANT`) over add-nullable/backfill/constrain when every row can share the default.
16
+ - **Adding `NOT NULL` to an existing column needing a real backfill**: add the column nullable, deploy code that writes it on every insert/update, backfill in batches (not one giant `UPDATE`), then constrain.
17
+ - Postgres: prefer `ALTER TABLE t ADD CONSTRAINT t_col_not_null CHECK (col IS NOT NULL) NOT VALID;` then a separate `ALTER TABLE t VALIDATE CONSTRAINT t_col_not_null;` over `ALTER COLUMN col SET NOT NULL`.
18
+ - MySQL: `ALTER TABLE t MODIFY COLUMN col <type> NOT NULL` still scans/rebuilds on most engines; schedule it off-peak or through the project's online-migration tool (`gh-ost` or equivalent).
19
+ - **Adding an index to an existing table**: Postgres `CREATE INDEX CONCURRENTLY` in its own migration step; MySQL InnoDB `CREATE INDEX` defaults to `ALGORITHM=INPLACE`, so state `ALGORITHM=INPLACE, LOCK=NONE` explicitly under sustained write load.
20
+ - **Renaming or changing a column's type**: add the new column, dual-write from application code, backfill, cut reads over, then drop the old column in a later migration.
21
+ - **Dropping a column or table**: stop reading/writing it from application code first (a full deploy cycle), then drop it in a subsequent migration.
60
22
 
61
23
  ## Indexing
62
24
 
63
- - Index every foreign key column and every column that appears in a
64
- `WHERE`, `JOIN ON`, or `ORDER BY` clause on a table large enough that a
65
- sequential/full-table scan would be noticeable — confirm with
66
- `EXPLAIN ANALYZE` on production-representative data volume, not on an
67
- empty dev table where every plan looks fast.
68
- - **Composite index column order**: put equality-filtered columns before
69
- range-filtered ones, and put the columns a query's `WHERE` actually
70
- restricts before columns only used for `ORDER BY`. An index on
71
- `(status, created_at)` serves `WHERE status = 'open' ORDER BY
72
- created_at` efficiently; the same index in the order `(created_at,
73
- status)` cannot use the equality filter to narrow the scan first and
74
- degrades toward a broader range scan.
75
- - **Covering indexes**: when a query only ever selects a handful of
76
- columns already present in an index's key or `INCLUDE`/covered columns
77
- (Postgres `INCLUDE`, or MySQL's implicit covering when all selected
78
- columns are in the index), the engine can satisfy it as an index-only
79
- scan without touching the table's heap/pages at all — worth adding
80
- narrowly for a hot, narrow query, not as a default habit.
81
- - **An index has a write cost**: every index is maintained on every
82
- insert/update/delete touching its columns. Do not add a composite index
83
- for every column combination a report might someday filter by; add the
84
- ones actual queries use, and periodically check for unused indexes
85
- (`pg_stat_user_indexes.idx_scan = 0` on Postgres) before a schema
86
- accumulates dead weight.
87
- - A partial index (`CREATE INDEX ... WHERE <predicate>`, Postgres) is
88
- narrower and cheaper to maintain than a full index when a query always
89
- filters on a fixed condition (`WHERE deleted_at IS NULL`,
90
- `WHERE status = 'active'`) — prefer it over a full index whose non-
91
- matching rows the query never touches.
25
+ - Index every foreign key column and every column in a `WHERE`, `JOIN ON` or `ORDER BY` on a table large enough for a full scan to matter; confirm with `EXPLAIN ANALYZE` on production-representative volume.
26
+ - **Composite index column order**: equality-filtered columns before range-filtered ones, and columns the `WHERE` restricts before columns used only for `ORDER BY` (`(status, created_at)` for `WHERE status = 'open' ORDER BY created_at`).
27
+ - **Covering indexes** (Postgres `INCLUDE`, MySQL implicit covering): add narrowly for a hot, narrow query, not by default.
28
+ - **Write cost**: add only indexes actual queries use, and check for unused ones (`pg_stat_user_indexes.idx_scan = 0` on Postgres).
29
+ - Prefer a partial index (`CREATE INDEX ... WHERE <predicate>`, Postgres) over a full index when a query always filters on a fixed condition (`WHERE deleted_at IS NULL`, `WHERE status = 'active'`).
92
30
 
93
31
  ## Query design
94
32
 
95
- - A query issued inside an application-code loop, once per row of an
96
- outer result set, is an N+1 — replace it with a single `JOIN`, an
97
- `IN (...)` batch, or the ORM's eager-load option before merging. This
98
- narrows the core `database-patterns.mdc` N+1 rule to the SQL shape
99
- itself: the fix is a query that returns everything needed in one round
100
- trip, not a cache layered on top of the repeated queries.
101
- - Select only the columns a caller actually needs — `SELECT *` in a
102
- hand-written `.sql` file breaks silently when a column is added later
103
- and can leak a sensitive column (a password hash, a token) into a
104
- result set nothing downstream expected to see.
105
- - Wrap a multi-statement write (anything touching more than one table, or
106
- more than one row-affecting statement against the same table) in an
107
- explicit transaction — `BEGIN`/`COMMIT` or the driver's transaction
108
- API — so a failure partway through rolls back cleanly instead of
109
- leaving related tables inconsistent.
110
- - Keep a transaction's lifetime to the database work alone — commit
111
- before making a slow external call (an HTTP request, a queue publish,
112
- a `sleep`), or open a fresh transaction after it completes. A
113
- transaction held open across an external call holds its locks for the
114
- external call's entire duration, which can stall every other writer
115
- touching the same rows.
116
- - Prefer `UPSERT` (Postgres `INSERT ... ON CONFLICT DO UPDATE`, MySQL
117
- `INSERT ... ON DUPLICATE KEY UPDATE`) over a read-then-decide-insert-or-
118
- update round trip when the goal is "write this row whether or not it
119
- already exists" — the read-then-write version races under concurrent
120
- writers even inside a transaction unless it also takes an explicit lock.
33
+ - Replace a query issued per row of an outer result set with a single `JOIN`, an `IN (...)` batch or the ORM's eager-load option; a cache over the repeated queries is not the fix.
34
+ - No `SELECT *` in a hand-written `.sql` file; select the columns the caller needs.
35
+ - Wrap a multi-statement write (more than one table, or more than one row-affecting statement on one table) in an explicit transaction (`BEGIN`/`COMMIT` or the driver's API).
36
+ - Commit before a slow external call (HTTP request, queue publish, `sleep`), or open a fresh transaction after it.
37
+ - Prefer `UPSERT` (Postgres `INSERT ... ON CONFLICT DO UPDATE`, MySQL `INSERT ... ON DUPLICATE KEY UPDATE`) over read-then-insert-or-update when the goal is to write the row whether or not it exists.
121
38
 
122
39
  ## Migration hygiene
123
40
 
124
- - Every migration that mutates existing data (not just schema) states its
125
- reverse operation, or explicitly states there isn't one and why (a
126
- destructive backfill, a data transformation that can't be inverted).
127
- - Make DDL idempotent where the engine allows it (`IF NOT EXISTS` on
128
- `CREATE TABLE`/`CREATE INDEX`, `IF EXISTS` on `DROP`) so a migration
129
- runner that re-applies a partially-run migration after a crash does not
130
- fail on an object that already exists.
131
- - Never combine a long-running backfill and a blocking schema change in
132
- the same migration step — split them so the schema change (fast) and
133
- the backfill (potentially slow, batched) can be reviewed and rolled
134
- back independently.
41
+ - A migration that mutates existing data states its reverse operation, or states there is none and why.
42
+ - Make DDL idempotent where the engine allows (`IF NOT EXISTS` on `CREATE TABLE`/`CREATE INDEX`, `IF EXISTS` on `DROP`).
43
+ - Never combine a long-running backfill and a blocking schema change in one migration step.
@@ -7,68 +7,23 @@ metadata:
7
7
 
8
8
  # SQL security
9
9
 
10
- Narrows `core-common-rules`' stack-agnostic security rules to injection
11
- safety and access control for `.sql` files across Postgres, MySQL, and
12
- generic SQL engines. Applies only to `*.sql` files — parameterized-query
13
- enforcement in application code (the driver/ORM call site) is covered by
14
- that language's own stack pack; this file covers the `.sql` side (stored
15
- procedures, functions, views) and the shared vocabulary both sides share.
10
+ Narrows `core-common-rules`' security rules to injection safety and access control for `.sql` files across Postgres, MySQL and generic SQL. Applies only to `*.sql` files; parameterized-query enforcement at the driver/ORM call site follows that language's stack pack.
16
11
 
17
12
  ## Injection
18
13
 
19
- - Never build a query by concatenating or string-formatting a
20
- user-influenced value into SQL text — this applies inside a stored
21
- procedure or function body exactly as it does in application code: a
22
- dynamic `EXECUTE`/`EXEC` built from concatenated input is injectable
23
- even though the vulnerable code lives in the database, not the app.
24
- - Postgres `EXECUTE`/`format()` inside `PL/pgSQL`: use `format('...%I...',
25
- identifier)` for identifiers (table/column names that must be dynamic)
26
- and `format('...%L...', value)` or a parameterized `USING` clause for
27
- values — `%I`/`%L` quote-escape correctly; plain string concatenation
28
- of either does not.
29
- - MySQL stored procedures: build dynamic SQL with `PREPARE stmt FROM
30
- @sql; EXECUTE stmt USING @param;` — bind user-influenced values through
31
- `USING`, never interpolate them into `@sql` itself before `PREPARE`.
32
- - An ORM or query builder's raw-SQL escape hatch (a `.raw()`, `.exec()`,
33
- or template-literal query method) carries the same rule as a
34
- hand-written query: pass user-influenced values through its
35
- placeholder/parameter API, never through string interpolation into the
36
- raw SQL text, even when the surrounding call looks like it's "just
37
- building a string for convenience."
38
- - A dynamic identifier (a table or column name chosen at runtime, common
39
- in multi-tenant schema-per-tenant designs or admin tooling) cannot go
40
- through a value placeholder at all — placeholders bind values, not
41
- identifiers. Validate it against an allowlist of known-safe identifiers
42
- before using it, never trust it as free text even when it "came from
43
- our own config."
14
+ - Never build a query by concatenating or string-formatting a user-influenced value into SQL text, including a dynamic `EXECUTE`/`EXEC` inside a stored procedure or function.
15
+ - Postgres `PL/pgSQL`: use `format('...%I...', identifier)` for dynamic identifiers and `format('...%L...', value)` or a `USING` clause for values.
16
+ - MySQL stored procedures: build dynamic SQL with `PREPARE stmt FROM @sql; EXECUTE stmt USING @param;`; bind values through `USING`, never interpolate them into `@sql`.
17
+ - An ORM or query builder's raw-SQL escape hatch (`.raw()`, `.exec()`, template-literal queries) takes user-influenced values through its placeholder API, never string interpolation.
18
+ - A dynamic identifier (runtime table/column name) cannot use a value placeholder; validate it against an allowlist of known-safe identifiers.
44
19
 
45
20
  ## Access control
46
21
 
47
- - Grant the narrowest role/privilege a connection actually needs — an
48
- application's runtime database user should not hold `DROP`/`CREATE`/
49
- superuser privileges it never uses; a migration runner's credentials
50
- (which do need DDL rights) should be separate from the application's
51
- runtime connection.
52
- - Use row-level security (Postgres `CREATE POLICY` on a table with `ROW
53
- LEVEL SECURITY` enabled) or an equivalent application-enforced tenant
54
- filter consistently — do not rely on every query remembering to add
55
- `WHERE tenant_id = ?` by convention alone when the engine can enforce it
56
- structurally.
57
- - A view or stored procedure that runs with elevated privileges
58
- (Postgres `SECURITY DEFINER`, a MySQL `DEFINER`-owned routine) is a
59
- privilege-escalation surface if it accepts unvalidated input that
60
- influences what it reads or writes — treat its parameters with the same
61
- injection discipline as a top-level query, since callers with lower
62
- privileges can reach the elevated code path through it.
22
+ - Grant the narrowest role/privilege a connection needs; the application's runtime user holds no `DROP`/`CREATE`/superuser rights, and migration-runner credentials are separate from the runtime connection.
23
+ - Use row-level security (Postgres `CREATE POLICY` with `ROW LEVEL SECURITY` enabled) or an equivalent tenant filter consistently, rather than relying on every query adding `WHERE tenant_id = ?`.
24
+ - Apply top-level injection discipline to the parameters of an elevated-privilege view or routine (Postgres `SECURITY DEFINER`, MySQL `DEFINER`-owned).
63
25
 
64
26
  ## Data exposure
65
27
 
66
- - Never `SELECT *` a table containing a sensitive column (password hash,
67
- API token, PII) into a result set a caller with less trust than the
68
- query's own privilege level will see — select named columns so adding a
69
- sensitive column later can't leak silently into an existing query.
70
- - Encrypt or hash sensitive columns before they reach storage
71
- (`pgcrypto`'s `crypt()`/`digest()` on Postgres, an application-layer
72
- hash before insert) rather than relying on transport encryption alone —
73
- a compromised backup or replica exposes plaintext columns even when
74
- every connection to the primary was encrypted.
28
+ - Never `SELECT *` a table with a sensitive column (password hash, API token, PII) into a result set a less-trusted caller sees; select named columns.
29
+ - Encrypt or hash sensitive columns before storage (`pgcrypto`'s `crypt()`/`digest()`, or an application-layer hash before insert), not transport encryption alone.
@@ -7,77 +7,32 @@ metadata:
7
7
 
8
8
  # SQL testing
9
9
 
10
- Narrows `core-common-rules`' stack-agnostic testing rules to testing raw
11
- `.sql` — migrations, schema, and hand-written query files — across
12
- Postgres, MySQL, and generic SQL engines. Applies only to `*.sql` files;
13
- testing a query or migration issued from application code (a language's
14
- ORM test helpers) is covered by that language's own stack pack, and
15
- should still follow the transactional-wrapping and fixture discipline
16
- below where that language's test suite talks to a real database.
10
+ Narrows `core-common-rules`' testing rules to raw `.sql` (migrations, schema, hand-written queries) across Postgres, MySQL and generic SQL. Applies only to `*.sql` files; tests of SQL issued from application code follow that language's stack pack, with the transactional-wrapping and fixture discipline below where its suite uses a real database.
17
11
 
18
12
  ## Test isolation
19
13
 
20
- - Wrap each test in a transaction that is rolled back at teardown
21
- (`BEGIN` before the test, `ROLLBACK` after) rather than truncating and
22
- re-seeding tables between tests — rollback is faster and guarantees no
23
- leftover state regardless of what the test wrote.
24
- - When a test must exercise something a transaction can't roll back
25
- cleanly (`CREATE INDEX CONCURRENTLY`, an advisory-lock scenario, a
26
- cross-transaction concurrency test), isolate it in its own
27
- database/schema that is torn down and recreated instead of relying on
28
- transactional rollback for that one case.
29
- - Never point a test suite at a shared development or staging database —
30
- a fixed, disposable test database (a local container, an ephemeral
31
- schema per CI run) so tests don't observe or clobber each other's data.
14
+ - Wrap each test in a transaction rolled back at teardown (`BEGIN` before, `ROLLBACK` after), not truncate-and-reseed.
15
+ - Isolate what a transaction cannot roll back (`CREATE INDEX CONCURRENTLY`, advisory locks, cross-transaction concurrency tests) in its own database/schema that is torn down and recreated.
16
+ - Never point a test suite at a shared development or staging database; use a disposable test database (a local container, an ephemeral schema per CI run).
32
17
 
33
18
  ## Fixtures and seed data
34
19
 
35
- - Seed only the rows a given test actually needs, built through the same
36
- helper/factory the rest of the suite uses — do not duplicate a large
37
- shared seed script per test file, and do not depend on a global fixture
38
- a different test also mutates.
39
- - Name fixture data so a failure is legible (`user_with_expired_token`,
40
- not `test_user_3`) — a table-driven or parameterized test case should
41
- carry a descriptive case name for the same reason.
42
- - A fixture representing a timestamp or "now"-relative value is
43
- constructed relative to a fixed, injected clock/reference time in the
44
- test, not `NOW()`/`CURRENT_TIMESTAMP` evaluated live — a live clock
45
- value makes boundary cases (rows that just expired, rows created "just
46
- now") flaky depending on when the test happens to run.
20
+ - Seed only the rows a test needs, through the suite's shared helper/factory; no large per-file seed script, no global fixture another test mutates.
21
+ - Name fixture data and parameterized cases descriptively (`user_with_expired_token`, not `test_user_3`).
22
+ - Build timestamp and "now"-relative fixtures from a fixed, injected reference time, not a live `NOW()`/`CURRENT_TIMESTAMP`.
47
23
 
48
24
  ## Testing migrations
49
25
 
50
- - Test a migration's `up` path against a database seeded to look like
51
- production before the change: representative row counts for anything
52
- the migration scans or backfills, not an empty table where every
53
- approach looks instant.
54
- - Test the reverse/`down` path when the migration states one — running
55
- `up` then `down` should leave the schema equivalent to before, and
56
- running `up` twice (or `down` twice) should not error when the
57
- migration was written to be idempotent.
58
- - For a batched backfill migration, test with a row count large enough to
59
- exercise at least two batches, and assert every row was updated exactly
60
- once (no row skipped at a batch boundary, none processed twice in a way
61
- that would double-apply a non-idempotent update).
26
+ - Test a migration's `up` path against a database seeded like production before the change, with representative row counts for anything it scans or backfills.
27
+ - Test the `down` path when the migration states one: `up` then `down` leaves an equivalent schema; running `up` or `down` twice does not error when the migration is idempotent.
28
+ - For a batched backfill, use enough rows for at least two batches and assert every row is updated exactly once.
62
29
 
63
30
  ## Testing queries
64
31
 
65
- - Assert on the query's actual result set (rows, values, ordering when
66
- the query specifies `ORDER BY`), not merely that it executed without
67
- error — a query testing only "did not throw" would pass even after a
68
- regression silently returns zero or the wrong rows.
69
- - For a query added to fix an N+1, assert the query count too (most
70
- ORMs/test harnesses expose a query counter or log) — a regression test
71
- for an N+1 fix should fail again if the fix regresses back to per-row
72
- queries, not just check the final result shape.
73
- - Test the empty-result case (a `WHERE` that matches nothing) and the
74
- boundary case for any range condition (`created_at >= X`) explicitly —
75
- these are exactly where an off-by-one in a range filter or a `JOIN`
76
- that should have been a `LEFT JOIN` shows up.
32
+ - Assert on the actual result set (rows, values, ordering when the query specifies `ORDER BY`), not merely that the query ran.
33
+ - For a query added to fix an N+1, assert the query count too.
34
+ - Test the empty-result case and the boundary case of any range condition (`created_at >= X`).
77
35
 
78
36
  ## Verification
79
37
 
80
- - Run the full suite against the transactional-rollback isolation
81
- described above before considering a change to `.sql` test fixtures or
82
- migration tests done — a test that passes in isolation but leaves state
83
- for the next test to trip over is not actually isolated.
38
+ - Run the full suite under the transactional-rollback isolation above before treating a change to `.sql` test fixtures or migration tests as done.