@hublo/sentinel 1.7.0-alpha.37 → 1.7.0-alpha.39
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.
- package/dist/bin/sentinel.js +2 -2
- package/dist/{chunk-5KNSTZ7F.js → chunk-GYDS3OMU.js} +11 -1
- package/dist/chunk-GYDS3OMU.js.map +1 -0
- package/dist/{chunk-MAC5PJ62.js → chunk-OBI4TDJ7.js} +1469 -542
- package/dist/index.js +2 -2
- package/dist/roles/sql/config.d.ts +28 -2
- package/dist/roles/sql/config.js +1 -1
- package/docs/sql-adoption.md +141 -11
- package/package.json +1 -1
- package/dist/chunk-5KNSTZ7F.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
registerAdapters,
|
|
7
7
|
resolve,
|
|
8
8
|
setDefaultRunner
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-OBI4TDJ7.js";
|
|
10
10
|
import "./chunk-ZZMYV576.js";
|
|
11
11
|
import {
|
|
12
12
|
BaseAdapter
|
|
@@ -20,7 +20,7 @@ import "./chunk-QXFCZON7.js";
|
|
|
20
20
|
import "./chunk-KKHKRY7P.js";
|
|
21
21
|
import "./chunk-YOFCDKTI.js";
|
|
22
22
|
import "./chunk-QJCZ2EDV.js";
|
|
23
|
-
import "./chunk-
|
|
23
|
+
import "./chunk-GYDS3OMU.js";
|
|
24
24
|
import "./chunk-XYOIXC6A.js";
|
|
25
25
|
import "./chunk-PXTU5MPW.js";
|
|
26
26
|
import "./chunk-U7G2Y6YC.js";
|
|
@@ -14,8 +14,9 @@ interface SqlDefaults {
|
|
|
14
14
|
/** Migrations a module chooses not to read. Empty by default. */
|
|
15
15
|
exclude: string[];
|
|
16
16
|
/**
|
|
17
|
-
* Other migrations directories, relative to the module,
|
|
18
|
-
*
|
|
17
|
+
* Other migrations directories, relative to the module, of a database this module queries:
|
|
18
|
+
* never checked, read for their indexes and for the Prisma schema beside them, by
|
|
19
|
+
* `leading-wildcard-like` (trigram indexes) and the rules on Prisma calls (`orm/orm-context.ts`).
|
|
19
20
|
*
|
|
20
21
|
* For a module that queries a database another module migrates. On the monorepo every trigram
|
|
21
22
|
* index is in `libs/nest/db-main` (main 190dc8e3595), and `mission`, `network` or `institution`
|
|
@@ -40,6 +41,31 @@ interface SqlDefaults {
|
|
|
40
41
|
*/
|
|
41
42
|
tests: string[];
|
|
42
43
|
};
|
|
44
|
+
/** What the rules on Prisma Client calls read beyond the code (`orm/`). */
|
|
45
|
+
orm: {
|
|
46
|
+
/**
|
|
47
|
+
* Small reference tables (a list of types, modes, settings) the module names: a read of one
|
|
48
|
+
* with no index costs nothing, and sentinel cannot know the size of a table. Decided on the
|
|
49
|
+
* ORM study: 24 of the 101 missing indexes found on main were on such tables.
|
|
50
|
+
*/
|
|
51
|
+
referenceTables: string[];
|
|
52
|
+
/**
|
|
53
|
+
* The writes the module makes once per row on purpose, `{ file, call: 'model.operation',
|
|
54
|
+
* reason }`: `prisma-write-in-loop` stays silent on them. Decided on the ORM study: 22 of the
|
|
55
|
+
* 113 writes in a loop found on main were deliberate.
|
|
56
|
+
*/
|
|
57
|
+
perRowWrites: Array<{
|
|
58
|
+
file: string;
|
|
59
|
+
call: string;
|
|
60
|
+
reason: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* The module's one-off scripts: a backfill, a seeder, a load test, which may create their own
|
|
64
|
+
* PrismaClient (`prisma-new-client` stays silent there). Measured on the monorepo's main: every
|
|
65
|
+
* `new PrismaClient` outside tests is in one of these folders.
|
|
66
|
+
*/
|
|
67
|
+
scripts: string[];
|
|
68
|
+
};
|
|
43
69
|
/** Directories never read, whatever the includes say. */
|
|
44
70
|
ignore: string[];
|
|
45
71
|
/**
|
package/dist/roles/sql/config.js
CHANGED
package/docs/sql-adoption.md
CHANGED
|
@@ -180,16 +180,19 @@ every setting the module can move and the role's value today, generated from the
|
|
|
180
180
|
cannot drift from them. A setting the module leaves out keeps the role's value; a setting it writes
|
|
181
181
|
**replaces** it, including inside a list, and the html report shows it as overridden by this module.
|
|
182
182
|
|
|
183
|
-
| Setting | Default | What it moves
|
|
184
|
-
| ------------------------ | ------------------------------------------ |
|
|
185
|
-
| `migrations.include` | `["**/migrations/*/migration.sql"]` | which files are migrations
|
|
186
|
-
| `migrations.exclude` | `[]` | migrations never read
|
|
187
|
-
| `migrations.indexesFrom` | `[]` | other migrations folders
|
|
188
|
-
| `
|
|
189
|
-
| `
|
|
190
|
-
| `
|
|
191
|
-
| `
|
|
192
|
-
| `
|
|
183
|
+
| Setting | Default | What it moves |
|
|
184
|
+
| ------------------------ | ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
185
|
+
| `migrations.include` | `["**/migrations/*/migration.sql"]` | which files are migrations |
|
|
186
|
+
| `migrations.exclude` | `[]` | migrations never read |
|
|
187
|
+
| `migrations.indexesFrom` | `[]` | other migrations folders, relative to the module, of a database this module queries: their indexes, and the Prisma schema beside them, are read by `leading-wildcard-like` and the rules on Prisma calls, never checked |
|
|
188
|
+
| `orm.referenceTables` | `[]` | small reference tables the module names: `prisma-filter-without-index` stays silent on them |
|
|
189
|
+
| `orm.perRowWrites` | `[]` | writes made once per row on purpose, `{ file, call: 'model.operation', reason }`: `prisma-write-in-loop` stays silent on them |
|
|
190
|
+
| `orm.scripts` | backfills, seeders, scripts, load tests | the module's one-off scripts: `prisma-new-client` stays silent there |
|
|
191
|
+
| `unsafe.includeTests` | `true` | whether calls in test files are checked, by both rules |
|
|
192
|
+
| `unsafe.tests` | test folders and `*.spec`/`*.test`/… files | what counts as a test file |
|
|
193
|
+
| `ignore` | `node_modules`, `dist`, `coverage` | never read, by either check |
|
|
194
|
+
| `pgVersion` | `"16.8"` | the PostgreSQL version squawk checks against |
|
|
195
|
+
| `rules` | `{}`, every rule at its default | a level per rule, by its name |
|
|
193
196
|
|
|
194
197
|
An example that blocks two rules, silences one and turns an opt-in rule on:
|
|
195
198
|
|
|
@@ -628,6 +631,132 @@ are: 12 searches are served and stay silent. The 26 were read one by one: none h
|
|
|
628
631
|
on its expression in any migration (`unaccent(w."searchName")` on an index of `"searchName"`,
|
|
629
632
|
recruitment's searches on a database whose migrations create the extension and no index).
|
|
630
633
|
|
|
634
|
+
### Prisma reads no index serves, `prisma-filter-without-index`
|
|
635
|
+
|
|
636
|
+
The rules on Prisma calls read **production code only**: a file matching `unsafe.tests` is left out,
|
|
637
|
+
whatever `unsafe.includeTests` says. Tests create their rows in a loop and filter tables of three
|
|
638
|
+
rows on purpose. On the monorepo's main, 1 042 of the 2 259 files calling Prisma are tests.
|
|
639
|
+
|
|
640
|
+
The SQL Prisma Client writes for a `findMany`, `findFirst` or `findUnique` is read too, from the
|
|
641
|
+
call in the TypeScript: a filter (`where`) whose columns no index starts with makes PostgreSQL read
|
|
642
|
+
every row of the table. Prisma's own guide asks for it: "index fields used in where"
|
|
643
|
+
([best practices](https://www.prisma.io/docs/orm/v7/more/best-practices)).
|
|
644
|
+
|
|
645
|
+
**A module with no Prisma schema of its own**, one that queries another module's database (on the
|
|
646
|
+
monorepo, `network` and `mission` read db-main's tables), names that database's migrations in
|
|
647
|
+
`migrations.indexesFrom`, as above. Without it, no rule on Prisma calls has a schema to judge them
|
|
648
|
+
against, and the report says so on the line that counts the reads, rather than a silent "no
|
|
649
|
+
finding" (found validating 1.7.0-alpha.38).
|
|
650
|
+
|
|
651
|
+
What it reads: the module's Prisma schemas (`*.prisma`) and the one beside each folder
|
|
652
|
+
`migrations.indexesFrom` names, and the indexes the migrations beside each schema create, since a
|
|
653
|
+
schema does not declare them all (db-main's migrations create 458 where its schema declares 361).
|
|
654
|
+
A relation is followed to its foreign-key column; a partial index counts as an index.
|
|
655
|
+
|
|
656
|
+
What it never judges, so never reports: a `where` that spreads another object in, a filter through
|
|
657
|
+
a relation (`some`, `is`, ...) or a text search (`contains`, the trigram index's concern), a model
|
|
658
|
+
two schemas declare, a schema not on PostgreSQL or with no migrations folder (an index created by a
|
|
659
|
+
script is not seen), a client that is not Prisma or a transaction (told by its name: `tx`,
|
|
660
|
+
`databaseService`, `mainDatabase`, ...). A small reference table read whole costs nothing, and
|
|
661
|
+
sentinel cannot know a table's size: name it in `orm.referenceTables`.
|
|
662
|
+
|
|
663
|
+
Measured on `main` 49e7abacfbc, production code, against the 279 places the ORM study read by hand:
|
|
664
|
+
of the 101 real missing indexes, it reports **89, with no false one** (precision 100%, recall 88%).
|
|
665
|
+
The 12 it leaves are the cases above, on purpose.
|
|
666
|
+
|
|
667
|
+
### A foreign key a branch adds with no index, `prisma-foreign-key-without-index`
|
|
668
|
+
|
|
669
|
+
PostgreSQL does not create an index for a foreign key: a join or a filter on the column, and every
|
|
670
|
+
delete of the row it points at, read the whole table. Prisma's guide asks to index the relation
|
|
671
|
+
scalar fields. The rule reads the Prisma schemas a branch adds or changes, and reports a relation
|
|
672
|
+
**the branch adds** whose foreign-key column no index starts with, in the schema or in the
|
|
673
|
+
migrations beside it. A relation the base already had is never reported: the schema is read again
|
|
674
|
+
at the base commit, as the role reads migrations, so today's foreign keys block nobody.
|
|
675
|
+
|
|
676
|
+
Measured on `main` 49e7abacfbc, every schema read as if new: 171 foreign keys with no index, 104 of
|
|
677
|
+
them in db-main. Two of them, drawn at random, were checked by hand in the schema and the
|
|
678
|
+
migrations: `churn.client_id` has no index, and `application_contract_type.contract_type_id` is only
|
|
679
|
+
the second column of its unique index. A finding sits on the relation's line of the schema, in the
|
|
680
|
+
page's Migrations tab.
|
|
681
|
+
|
|
682
|
+
### A Prisma read run once per element, `prisma-query-in-loop`
|
|
683
|
+
|
|
684
|
+
The N+1: a `findMany`, `findFirst` or `findUnique` inside a `for`, a `while`, or the callback of
|
|
685
|
+
`.map` / `.forEach` / `.reduce` (`Promise.all(xs.map(...))` included, it sends one query per element
|
|
686
|
+
too, in parallel). The number of queries grows with the data. Prisma's guide: fetch the related data
|
|
687
|
+
in one query, or batch with an `in` filter.
|
|
688
|
+
|
|
689
|
+
A loop over **batches** is the right shape and is left: it walks chunks or slices
|
|
690
|
+
(`chunk(ids, 500)`, `ids.slice(i, i + 500)`), or the read takes the loop's own variable with `in`
|
|
691
|
+
(`where: { id: { in: group } }`), or it pages with `take` and a cursor (`cursor`, or
|
|
692
|
+
`id: { gt: last }`). Only models of a PostgreSQL schema are judged; a model that two schemas of the
|
|
693
|
+
module declare is not, the call does not say which database it reads.
|
|
694
|
+
|
|
695
|
+
Measured before it was written, on `main` 49e7abacfbc, production code: 29 reads in a loop, read
|
|
696
|
+
by hand, 14 real N+1, the 15 others walking batches. Replayed: 14 reported, the 14 real, none false.
|
|
697
|
+
A loop that reads one element, waits and reads again (a retry, a lock being acquired) is not a list
|
|
698
|
+
walked; read by hand, the one such loop of the monorepo is not reported.
|
|
699
|
+
|
|
700
|
+
### A Prisma write run once per element, `prisma-write-in-loop`
|
|
701
|
+
|
|
702
|
+
A `create`, `update`, `upsert` or `delete` (or their `Many` forms) inside a loop, including
|
|
703
|
+
`$transaction(xs.map((x) => tx.model.update(...)))`: one statement and one round trip per row, where
|
|
704
|
+
`createMany`, `updateMany` or `deleteMany` writes the set in one.
|
|
705
|
+
|
|
706
|
+
Left, as for the reads (`orm/batches.ts`): a loop over chunks, slices or groups
|
|
707
|
+
(`groupedBySentAt`, `groupRewritesByData(...)`), and a write that already takes a set from the loop's
|
|
708
|
+
variable or one of its properties (`in: batch`, `in: event.props.institutionIds`, `OR: pair`). Only
|
|
709
|
+
models that the module's schemas declare on PostgreSQL alone are judged.
|
|
710
|
+
|
|
711
|
+
When one write per row is wanted (a different value per row that `updateMany` cannot set, an audit
|
|
712
|
+
row per change, a small catalog), name it in `sql.config.mjs`, with its reason. Every entry is
|
|
713
|
+
listed by `--inspect`, and none hides another call:
|
|
714
|
+
|
|
715
|
+
```js
|
|
716
|
+
overrides: {
|
|
717
|
+
orm: {
|
|
718
|
+
perRowWrites: [
|
|
719
|
+
{
|
|
720
|
+
file: 'src/domains/publication/infrastructure/repositories/publication.prisma.repository.ts',
|
|
721
|
+
call: 'jobBoardPublication.upsert',
|
|
722
|
+
reason: 'one row per job board of the offer, a handful',
|
|
723
|
+
},
|
|
724
|
+
],
|
|
725
|
+
},
|
|
726
|
+
},
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
Measured before it was written, on `main` 49e7abacfbc, production code: 138 writes in a loop read
|
|
730
|
+
by hand, 113 real. Replayed: 115 reported, 110 real, 2 false (the job-board catalog above, which is
|
|
731
|
+
what `perRowWrites` is for), 3 that the hand reading left unsure (swaps of jobs whose merging may not
|
|
732
|
+
be safe), and 3 missed. Those 3 are writes that already take a set (`in: idAdmins`) once per group.
|
|
733
|
+
The rule reads them as batches, and it does so on purpose: the hand reading found the same shape
|
|
734
|
+
false elsewhere (one event, normally).
|
|
735
|
+
|
|
736
|
+
### Production code that creates its own client, `prisma-new-client`
|
|
737
|
+
|
|
738
|
+
Each `new PrismaClient()` opens its own pool of connections to the database. Prisma's guide asks
|
|
739
|
+
for one instance shared by the application; in a NestJS module it is the database service that
|
|
740
|
+
`extends PrismaClient`, injected where it is needed.
|
|
741
|
+
|
|
742
|
+
A guard rather than a backlog. On `main` b9ec08c3312, 113 files create a client: 87 are tests, which
|
|
743
|
+
the rules on Prisma do not read, and 26 are one-off scripts (backfills, seeders, `*.seed.dev.ts`),
|
|
744
|
+
which `orm.scripts` names by default. The replay reports none: the rule keeps it so. A script in a
|
|
745
|
+
folder of its own kind is named in `orm.scripts`.
|
|
746
|
+
|
|
747
|
+
### A model a branch adds with a name out of line, `prisma-model-naming`
|
|
748
|
+
|
|
749
|
+
Prisma's guide names models in PascalCase and in the singular, and maps the table with `@@map`
|
|
750
|
+
(`model User { ... @@map("users") }`): the client then reads `db.user.findMany()`. The rule reads
|
|
751
|
+
the Prisma schemas a branch changes, again at the base, and reports a model **the branch adds**
|
|
752
|
+
whose name is not PascalCase, or reads as a plural (it ends in an `s` that is not `ss`, `us`, `is`
|
|
753
|
+
or `ness`). The names of today block nobody.
|
|
754
|
+
|
|
755
|
+
Replayed on `main` b9ec08c3312, every schema read as if new: 15 findings, 3 not PascalCase
|
|
756
|
+
(`migrations` twice, `shift_template`) and 12 plurals, of which one is not: `Rpps`, an acronym.
|
|
757
|
+
The plural is a reading of the name's ending, and the message says so; the rule stays at `warn`.
|
|
758
|
+
MongoDB schemas are not judged.
|
|
759
|
+
|
|
631
760
|
### The 19 May incident, `exists-union-correlated-outside`
|
|
632
761
|
|
|
633
762
|
```sql
|
|
@@ -689,7 +818,8 @@ Said here so that a clean report is not read as more than it is:
|
|
|
689
818
|
- **JavaScript files** (`.js`, `.mjs`): the role reads TypeScript (`.ts`, `.mts`, `.cts`).
|
|
690
819
|
- **Files outside a module.** The role runs on a module; a script at the repository's root that
|
|
691
820
|
belongs to no module is read by no role.
|
|
692
|
-
- **What the ORM generates** (`findMany`, `update`, …) is
|
|
821
|
+
- **What the ORM generates** (`findMany`, `update`, …) is read for four shapes only: a filter no
|
|
822
|
+
index serves, a foreign key with no index, a read in a loop, a write in a loop. The SQL Prisma generates is not read.
|
|
693
823
|
|
|
694
824
|
## How it was validated
|
|
695
825
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hublo/sentinel",
|
|
3
|
-
"version": "1.7.0-alpha.
|
|
3
|
+
"version": "1.7.0-alpha.39",
|
|
4
4
|
"description": "One CLI that guards code health across Hublo repos: shared lint/typescript/build/test presets, static & dynamic analysis, and architecture checks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/roles/sql/presets/shared.json","../src/roles/sql/presets/squawk-rules.json","../src/roles/sql/preset-data.ts","../src/roles/sql/configs/define.ts"],"sourcesContent":["{\n \"migrations\": {\n \"include\": [\"**/migrations/*/migration.sql\"],\n \"exclude\": [],\n \"indexesFrom\": []\n },\n \"unsafe\": {\n \"includeTests\": true,\n \"tests\": [\n \"**/{test,tests,__tests__,e2e,fixtures,mocks,__mocks__,test-utils}/**\",\n \"**/*.{spec,test,integration-spec,e2e-spec,steps,test-util,mock}.{ts,mts,cts}\"\n ]\n },\n \"ignore\": [\"**/node_modules/**\", \"**/dist/**\", \"**/coverage/**\"],\n \"pgVersion\": \"16.8\",\n \"rules\": {}\n}\n","{\n \"squawk\": \"2.72.0\",\n \"rules\": [\n \"adding-field-with-default\",\n \"adding-foreign-key-constraint\",\n \"adding-not-nullable-field\",\n \"adding-required-field\",\n \"adding-serial-primary-key-field\",\n \"ban-add-column\",\n \"ban-add-composite-attribute\",\n \"ban-add-enum-value\",\n \"ban-alter-database-options\",\n \"ban-alter-domain-with-add-constraint\",\n \"ban-alter-extension\",\n \"ban-alter-function-options\",\n \"ban-alter-generated-expression\",\n \"ban-alter-identity\",\n \"ban-alter-policy-condition\",\n \"ban-alter-policy-roles\",\n \"ban-alter-role-options\",\n \"ban-alter-row-level-security\",\n \"ban-alter-sequence-values\",\n \"ban-alter-system-options\",\n \"ban-alter-view-options\",\n \"ban-char-field\",\n \"ban-concurrent-index-creation-in-transaction\",\n \"ban-create-domain-with-constraint\",\n \"ban-create-policy\",\n \"ban-detach-inheritance\",\n \"ban-disable-trigger\",\n \"ban-drop-column\",\n \"ban-drop-constraint\",\n \"ban-drop-database\",\n \"ban-drop-default\",\n \"ban-drop-domain\",\n \"ban-drop-extension\",\n \"ban-drop-function\",\n \"ban-drop-generated-expression\",\n \"ban-drop-index\",\n \"ban-drop-not-null\",\n \"ban-drop-policy\",\n \"ban-drop-schema\",\n \"ban-drop-sequence\",\n \"ban-drop-table\",\n \"ban-drop-trigger\",\n \"ban-drop-type\",\n \"ban-drop-view\",\n \"ban-duplicate-column-assignments\",\n \"ban-new-write-restriction\",\n \"ban-replace-view-function\",\n \"ban-replica-identity\",\n \"ban-revoke\",\n \"ban-set-default\",\n \"ban-set-schema\",\n \"ban-truncate-cascade\",\n \"ban-uncommitted-transaction\",\n \"changing-column-type\",\n \"constraint-missing-not-valid\",\n \"disallowed-unique-constraint\",\n \"identifier-too-long\",\n \"prefer-bigint-over-int\",\n \"prefer-bigint-over-smallint\",\n \"prefer-identity\",\n \"prefer-repack\",\n \"prefer-robust-stmts\",\n \"prefer-text-field\",\n \"prefer-timestamp-tz\",\n \"renaming-column\",\n \"renaming-object\",\n \"renaming-table\",\n \"require-concurrent-index-creation\",\n \"require-concurrent-index-deletion\",\n \"require-concurrent-partition-detach\",\n \"require-concurrent-reindex\",\n \"require-enum-value-ordering\",\n \"require-lock-timeout\",\n \"require-statement-timeout\",\n \"require-table-schema\",\n \"transaction-nesting\"\n ],\n \"optIn\": [\n \"ban-add-column\",\n \"ban-add-composite-attribute\",\n \"ban-add-enum-value\",\n \"ban-alter-database-options\",\n \"ban-alter-extension\",\n \"ban-alter-function-options\",\n \"ban-alter-generated-expression\",\n \"ban-alter-policy-condition\",\n \"ban-alter-policy-roles\",\n \"ban-alter-role-options\",\n \"ban-alter-row-level-security\",\n \"ban-alter-sequence-values\",\n \"ban-alter-system-options\",\n \"ban-alter-view-options\",\n \"ban-create-policy\",\n \"ban-detach-inheritance\",\n \"ban-disable-trigger\",\n \"ban-drop-index\",\n \"ban-drop-policy\",\n \"ban-drop-trigger\",\n \"ban-new-write-restriction\",\n \"ban-replace-view-function\",\n \"ban-replica-identity\",\n \"ban-revoke\",\n \"ban-set-default\",\n \"require-table-schema\"\n ],\n \"aliases\": {\n \"prefer-timestamptz\": [\"prefer-timestamp-tz\"],\n \"require-timeout-settings\": [\"require-lock-timeout\", \"require-statement-timeout\"]\n }\n}\n","/**\n * The SQL role's values: what it reads, where it writes, and what a module may move.\n *\n * ## What this role guards, and in which order\n *\n * The study behind it (`docs/sql-verification-study.md`, HUB-45374, issue #150) measured where our\n * SQL lives and set the order, cheapest first:\n *\n * 1. **migrations**, read by squawk. On the monorepo (`main` 0eb8be6550d, 2026-11-14): 871\n * `migration.sql` across 21 modules, 135 of them added by 84 pull requests in five weeks.\n * 2. **the `Unsafe` calls** (`$queryRawUnsafe`, `$executeRawUnsafe`), read by a rule of ours. 226\n * calls the same day: 23 in production code, 203 in tests. And `Prisma.raw`, whose text is SQL\n * inside a tagged query, by the same rule under its own name: 97 calls on `main` f12da701121.\n *\n * 3. **the SQL of tagged templates** (`$queryRaw`, `$executeRaw`, `Prisma.sql`), read with\n * PostgreSQL's own parser (`libpg-query`) by rules of ours, the 19 May incident's first (#178):\n * 1 587 templates on `main` 837abe3f29c, 91 % of the production ones read alone.\n *\n * The ORM chain comes later, in this same role.\n *\n * ## Which modules: the ones that hold SQL, whatever their preset\n *\n * Decided from what the module HOLDS, never from its preset, which is the rule sentinel already\n * follows: the build role reads its own Vite config rather than trusting a preset. A preset says\n * which stack a module is written in, not whether it talks to a database. `libs/nest/signal-bus`\n * is `nest` (its code imports `@nestjs/`) and holds no SQL at all, it carries signals over Redis;\n * `libs/nest/db-main` holds 442 of the 871 migrations. Both are `nest`, and only one concerns this\n * role.\n *\n * So the role applies to every preset, like six of the seven before it, and `--init` skips a module\n * holding neither a migration nor an `Unsafe` call. A front end holds neither, so it is skipped\n * with no line of code about front ends. Measured with dry runs on `apps/front/host-admin`,\n * `libs/front/components`, `libs/nest/signal-bus` (skipped), `libs/nest/db-main` and `network`\n * (adopted).\n */\nimport sharedData from './presets/shared.json'\nimport squawkRulesData from './presets/squawk-rules.json'\n\n/** The target this role serves, and the flag that runs it: `sentinel --run --sql`. */\nexport const TARGET = 'sql'\n\n/** The file a module writes at its root to move a setting. Optional at every moment. */\nexport const SQL_CONFIG_FILE = 'sql.config.mjs'\n\n/**\n * Where the files of `--output json|html|markdown` land, inside the module.\n *\n * Its own directory rather than `static-analysis-report/`: two roles writing into one folder would\n * each clear the other's stale files.\n */\nexport const REPORT_DIR = 'sql-report'\n\n/**\n * One script per shape a DEVELOPER runs. `markdown` gets none: it is the pull request comment a CI\n * workflow produces, the decision static analysis took for the same reason.\n */\nexport const SCRIPTS = {\n console: 'sql',\n json: 'sql:json',\n html: 'sql:html',\n} as const\n\n/** A rule's level. `off` is a module's decision, and the report prints it as one. */\nexport type Severity = 'off' | 'warn' | 'error'\n\nexport interface SqlDefaults {\n /** The migration files squawk reads, as globs relative to the module. */\n migrations: {\n /**\n * ⛔ `migration.sql` BY NAME, and that is what keeps the rollback scripts out. Beside the 871\n * migrations sit 385 `down.sql`, 2 `drop.sql` and one `donw.sql`: scripts a human runs by hand,\n * which Prisma never applies. Reading them would report every `DROP` they are made of, for a\n * file that never reaches production through a deploy. Naming the file Prisma applies, rather\n * than excluding the others, also keeps out the misspelling nobody would think to exclude.\n */\n include: string[]\n /** Migrations a module chooses not to read. Empty by default. */\n exclude: string[]\n /**\n * Other migrations directories, relative to the module, whose INDEXES this module's queries\n * use: never checked, only read for the trigram indexes `leading-wildcard-like` asks about.\n *\n * For a module that queries a database another module migrates. On the monorepo every trigram\n * index is in `libs/nest/db-main` (main 190dc8e3595), and `mission`, `network` or `institution`\n * query its tables: without this, the rule would say of their searches that no index serves\n * them. Empty by default: the module's own migrations only, and the message says so.\n */\n indexesFrom: string[]\n }\n /** What the `Unsafe` rule reads. */\n unsafe: {\n /**\n * Read the tests too. ON by default: 203 of the 226 `Unsafe` calls are in tests, and a call\n * that is safe today is not safe by construction tomorrow. A module that decides otherwise sets\n * it to false, and the report says so; the decision is the team's.\n */\n includeTests: boolean\n /**\n * What counts as a test, by PATH. Measured on the monorepo before it was written: the filter\n * `*.spec.*` alone let 149 test calls through as production, because `*.integration-spec.ts`,\n * `*.steps.ts`, `tests/database/` and `*.test-util.ts` are all test code and none of them\n * matches it.\n */\n tests: string[]\n }\n /** Directories never read, whatever the includes say. */\n ignore: string[]\n /**\n * The PostgreSQL version squawk checks against, since some rules depend on it.\n *\n * 16.8 is the version the monorepo pins (`postgres:16.8`, in its integration tests and its\n * database tooling). The role reads what the repository declares and never asks production.\n */\n pgVersion: string\n /**\n * Each rule's level, by name. Empty means every rule at its default, `warn` in this version: the\n * role refuses nothing while its adoption runs.\n */\n rules: Record<string, Severity>\n}\n\n/**\n * The role's values, read from `presets/shared.json`: preset data is JSON, never TypeScript, in every\n * role (AGENTS.md, and `tests/role-layout.test.ts` holds it). The reasons for each value are the\n * comments on `SqlDefaults` above, since JSON carries none.\n */\nexport const defaults: SqlDefaults = sharedData as SqlDefaults\n\n/**\n * squawk's rule names, for the version sentinel pins: what `rules` in `sql.config.mjs` may name.\n *\n * Data about the tool rather than a preset, kept beside the presets like `build`'s\n * `requirements.json`. Taken from squawk's own source at the pinned tag\n * (`crates/squawk_linter/src/lib.rs`, the names a finding carries and `is_opt_in`), and held to the\n * binary by a test: every name here is accepted by the installed squawk, and the version here is\n * the version installed, so a squawk bump that renames a rule fails the build instead of a module.\n *\n * - `rules`, the names findings carry. `unused-ignore` is left out: squawk reports it about its own\n * ignore comments, and refuses it in a config.\n * - `optIn`, the rules squawk runs only when a config names them.\n * - `aliases`, names squawk accepts that no finding carries. A level set on one would never meet a\n * finding, so the role refuses it and names the rule(s) it stands for.\n */\nexport interface SquawkRules {\n squawk: string\n rules: string[]\n optIn: string[]\n aliases: Record<string, string[]>\n}\n\nexport const squawkRules: SquawkRules = squawkRulesData\n","/**\n * `defineSql`: what a module imports to declare its own SQL settings.\n *\n * ```js\n * // sql.config.mjs\n * import { defineSql } from '@hublo/sentinel/sql'\n *\n * export default defineSql({\n * root: import.meta.dirname,\n * overrides: { unsafe: { includeTests: false } },\n * })\n * ```\n *\n * The shape of `defineStaticAnalysis`, for the two reasons stated there. A function receives the\n * module's INTENT, the keys it actually passed, and merges them here, so setting one key never\n * drops its siblings: a spread in the module's file would replace the whole `unsafe` block to set\n * `includeTests`, and lose `tests` with nobody seeing it. And the defaults are never copied into a\n * module, so a changed default reaches every module at the next release instead of leaving stale\n * copies behind.\n *\n * `overrides` REPLACES what it names, lists included, and every path it sets lands in `moved`, so\n * the report prints `(module)` beside it: two modules measured with different settings no longer\n * compare, and the reader has to be told.\n */\nimport { isPlainObject } from '../../../shared/deep-merge.js'\nimport { defaults, type SqlDefaults } from '../preset-data.js'\n\ntype DeepPartial<T> = { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K] }\n\n/** What a module's config file passes. Everything is optional, including the file itself. */\nexport interface SqlInput {\n /** The module's own directory, normally `import.meta.dirname`. */\n root?: string\n /** The seam. Replaces whatever it names, and every path it sets lands in `moved`. */\n overrides?: DeepPartial<SqlDefaults>\n}\n\nexport interface ResolvedSqlConfig extends SqlDefaults {\n /** The module's directory, or null when its config file did not say. */\n root: string | null\n /** Dotted paths the module set through `overrides`, sorted so two runs print the same report. */\n moved: string[]\n}\n\n/**\n * Merge `patch` into `base`, recursively, patch winning.\n *\n * Arrays are REPLACED, never concatenated: a merge that concatenates cannot express \"replace this\n * list\", which is exactly what a team reaching for `overrides` asks for. `rules` is a record, so\n * a module naming one rule keeps the levels of every other.\n */\nfunction mergeInto<T>(base: T, patch: unknown): T {\n if (!isPlainObject(patch)) return base\n if (!isPlainObject(base)) return patch as T\n const out: Record<string, unknown> = { ...base }\n for (const [key, value] of Object.entries(patch)) {\n out[key] = isPlainObject(value) ? mergeInto(out[key], value) : value\n }\n return out as T\n}\n\n/** Every leaf path in `patch`, dotted, so a reader can be told what the module decided. */\nfunction leafPaths(patch: unknown, prefix = ''): string[] {\n if (!isPlainObject(patch)) return prefix ? [prefix] : []\n return Object.entries(patch).flatMap(([key, value]) =>\n leafPaths(value, prefix ? `${prefix}.${key}` : key),\n )\n}\n\n/** The keys `defineSql` reads. */\nconst INPUT_KEYS: readonly string[] = ['root', 'overrides']\n\n/** Resolve a module's declaration against this role's defaults. */\nexport function defineSql(input: SqlInput = {}): ResolvedSqlConfig {\n // ⛔ `override:` for `overrides:` would otherwise resolve to the defaults in silence, the module\n // believing it moved a setting. Thrown, so the loader names the file and the key.\n const unknown = Object.keys(input).filter((key) => !INPUT_KEYS.includes(key))\n if (unknown.length > 0) {\n throw new Error(\n `defineSql takes ${INPUT_KEYS.map((key) => `\\`${key}\\``).join(' and ')}; ` +\n `${unknown.map((key) => `\\`${key}\\``).join(', ')} would be ignored.`,\n )\n }\n return {\n ...mergeInto(defaults, input.overrides),\n root: input.root ?? null,\n moved: leafPaths(input.overrides).sort(),\n }\n}\n\n/**\n * A config another version of `defineSql` resolved, with this version's defaults under it.\n *\n * ⛔ A module's `sql.config.mjs` imports `defineSql` from the sentinel its own `node_modules` holds,\n * which may be older than the one running: a setting added since (`migrations.indexesFrom`,\n * 1.7.0-alpha.34) is then missing, and the rule reading it threw, its findings dropped with nothing\n * said (found on the alpha.34 replay). Every value the module resolved is kept; only a missing\n * setting takes the default.\n */\nexport function withDefaults(resolved: ResolvedSqlConfig): ResolvedSqlConfig {\n return mergeInto<SqlDefaults>(defaults, resolved) as ResolvedSqlConfig\n}\n"],"mappings":";;;;;AAAA;AAAA,EACE,YAAc;AAAA,IACZ,SAAW,CAAC,+BAA+B;AAAA,IAC3C,SAAW,CAAC;AAAA,IACZ,aAAe,CAAC;AAAA,EAClB;AAAA,EACA,QAAU;AAAA,IACR,cAAgB;AAAA,IAChB,OAAS;AAAA,MACP;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EACA,QAAU,CAAC,sBAAsB,cAAc,gBAAgB;AAAA,EAC/D,WAAa;AAAA,EACb,OAAS,CAAC;AACZ;;;AChBA;AAAA,EACE,QAAU;AAAA,EACV,OAAS;AAAA,IACP;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AA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