loadwise 0.6.2 → 0.8.0

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 (278) hide show
  1. package/README.md +175 -115
  2. package/dist/bin/loadwise.js +132 -67
  3. package/dist/lib/accessibility/analyze.js +5 -3
  4. package/dist/lib/accessibility/angular-inline.js +9 -11
  5. package/dist/lib/accessibility/angular-literal.js +39 -0
  6. package/dist/lib/accessibility/form-context.js +36 -0
  7. package/dist/lib/accessibility/frame-head.js +92 -0
  8. package/dist/lib/accessibility/frame-viewport.js +149 -0
  9. package/dist/lib/accessibility/frontend-detect.js +6 -5
  10. package/dist/lib/accessibility/jsx-elements.js +13 -3
  11. package/dist/lib/accessibility/markup-files.js +7 -2
  12. package/dist/lib/accessibility/rule-ids.js +3 -0
  13. package/dist/lib/accessibility/rules.js +128 -41
  14. package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
  15. package/dist/lib/accessibility/template-attributes.js +5 -3
  16. package/dist/lib/accessibility/template-elements.js +25 -6
  17. package/dist/lib/accessibility/template-file.js +40 -25
  18. package/dist/lib/accessibility/usage.js +10 -2
  19. package/dist/lib/editor/accessibility-examples.js +6 -1
  20. package/dist/lib/editor/actions.js +46 -7
  21. package/dist/lib/editor/apply-fix.js +69 -4
  22. package/dist/lib/editor/configured-setup.js +63 -0
  23. package/dist/lib/editor/diagnostics.js +206 -17
  24. package/dist/lib/editor/dismissals.js +85 -26
  25. package/dist/lib/editor/editor-document.js +5 -2
  26. package/dist/lib/editor/editor-launcher.js +170 -0
  27. package/dist/lib/editor/fix-edits.js +19 -0
  28. package/dist/lib/editor/jetbrains-setup.js +53 -0
  29. package/dist/lib/editor/language-server.js +80 -24
  30. package/dist/lib/editor/managed-config.js +37 -0
  31. package/dist/lib/editor/native-setup.js +86 -0
  32. package/dist/lib/editor/perf-watch.js +8 -6
  33. package/dist/lib/editor/release-vsix.js +61 -0
  34. package/dist/lib/editor/reliability-examples.js +15 -0
  35. package/dist/lib/editor/scan-session.js +29 -8
  36. package/dist/lib/editor/server-commands.js +38 -9
  37. package/dist/lib/editor/setup-server.js +87 -0
  38. package/dist/lib/editor/setup-vim.js +63 -0
  39. package/dist/lib/editor/setup.js +131 -0
  40. package/dist/lib/editor/stdio-setup.js +6 -0
  41. package/dist/lib/editor/workspace-files.js +61 -7
  42. package/dist/lib/editor/workspace-paths.js +41 -1
  43. package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
  44. package/dist/lib/maintainability/architecture/architecture.js +4 -4
  45. package/dist/lib/maintainability/cli/changed-lines.js +20 -2
  46. package/dist/lib/maintainability/cli/inputs.js +12 -3
  47. package/dist/lib/maintainability/cli.js +18 -5
  48. package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
  49. package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
  50. package/dist/lib/maintainability/examples.js +4 -0
  51. package/dist/lib/maintainability/quality/functions.js +9 -6
  52. package/dist/lib/maintainability/quality/limits.js +19 -0
  53. package/dist/lib/maintainability/quality/size-limits.js +8 -2
  54. package/dist/lib/maintainability/quality/stray-console.js +62 -1
  55. package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
  56. package/dist/lib/maintainability/rule-ids.js +3 -0
  57. package/dist/lib/maintainability/scan-changed.js +4 -1
  58. package/dist/lib/maintainability/scan-worker.js +6 -2
  59. package/dist/lib/maintainability/scan.js +21 -8
  60. package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
  61. package/dist/lib/maintainability/unused/module-usage.js +43 -21
  62. package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
  63. package/dist/lib/maintainability/unused/unused-modules.js +5 -10
  64. package/dist/lib/performance/analysis/source-inputs.js +7 -2
  65. package/dist/lib/performance/analysis/source-options.js +7 -6
  66. package/dist/lib/performance/analyze.js +16 -3
  67. package/dist/lib/performance/cli/changed-lines.js +8 -1
  68. package/dist/lib/performance/cli/cli-arguments.js +19 -8
  69. package/dist/lib/performance/cli/cli-output.js +4 -0
  70. package/dist/lib/performance/cli/cli-scan.js +21 -2
  71. package/dist/lib/performance/cli.js +16 -7
  72. package/dist/lib/performance/collections/memory-growth.js +55 -12
  73. package/dist/lib/performance/contracts/openapi.js +13 -4
  74. package/dist/lib/performance/datastores/pg-integrity.js +68 -22
  75. package/dist/lib/performance/datastores/pg-session-release.js +114 -0
  76. package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
  77. package/dist/lib/performance/datastores/pooling.js +62 -3
  78. package/dist/lib/performance/datastores/redis.js +94 -18
  79. package/dist/lib/performance/datastores/session-store.js +2 -1
  80. package/dist/lib/performance/db/checks.js +19 -8
  81. package/dist/lib/performance/db/gateway.js +3 -1
  82. package/dist/lib/performance/db/plan-estimates.js +4 -4
  83. package/dist/lib/performance/engine/native-syntax.js +3 -0
  84. package/dist/lib/performance/engine/native.js +11 -7
  85. package/dist/lib/performance/evidence/plans.js +7 -5
  86. package/dist/lib/performance/evidence/traces.js +21 -1
  87. package/dist/lib/performance/frontend/angular-teardown.js +96 -0
  88. package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
  89. package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
  90. package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
  91. package/dist/lib/performance/frontend/frontend-perf.js +149 -74
  92. package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
  93. package/dist/lib/performance/migrations/extract-code.js +13 -5
  94. package/dist/lib/performance/migrations/extract-sql.js +82 -38
  95. package/dist/lib/performance/migrations/operations.js +56 -13
  96. package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
  97. package/dist/lib/performance/migrations/postgres-registry.js +127 -26
  98. package/dist/lib/performance/migrations/postgres-settings.js +78 -0
  99. package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
  100. package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
  101. package/dist/lib/performance/migrations/rules-postgres.js +88 -39
  102. package/dist/lib/performance/orm/offset-pagination.js +6 -4
  103. package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
  104. package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
  105. package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
  106. package/dist/lib/performance/polyglot.js +111 -14
  107. package/dist/lib/performance/projects.js +3 -3
  108. package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
  109. package/dist/lib/performance/request/backend-mastery.js +151 -42
  110. package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
  111. package/dist/lib/performance/request/fastify-receivers.js +93 -0
  112. package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
  113. package/dist/lib/performance/request/mongoose-lean.js +298 -46
  114. package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
  115. package/dist/lib/performance/request/phase5-ast.js +6 -7
  116. package/dist/lib/performance/request/phase5-rules.js +204 -23
  117. package/dist/lib/performance/request/request-handlers.js +120 -23
  118. package/dist/lib/performance/request/sql-select-star.js +150 -0
  119. package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
  120. package/dist/lib/performance/request/upload-limits.js +5 -1
  121. package/dist/lib/performance/site-fix.js +4 -4
  122. package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
  123. package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
  124. package/dist/lib/performance/site-fixes/collections.js +1 -2
  125. package/dist/lib/performance/site-fixes/frontend-fixes.js +358 -14
  126. package/dist/lib/performance/site-fixes/helpers.js +3 -4
  127. package/dist/lib/performance/site-fixes/integrity.js +2 -4
  128. package/dist/lib/performance/site-fixes/migration-context.js +8 -1
  129. package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
  130. package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
  131. package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
  132. package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
  133. package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
  134. package/dist/lib/performance/sql/mysql-tokens.js +38 -12
  135. package/dist/lib/reliability/boundary-access.js +114 -57
  136. package/dist/lib/reliability/react-bugs.js +29 -7
  137. package/dist/lib/reliability/resource-leak-intervals.js +258 -0
  138. package/dist/lib/reliability/resource-leaks.js +209 -195
  139. package/dist/lib/reliability/rule-ids.js +10 -1
  140. package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
  141. package/dist/lib/reliability/swallowed-errors.js +107 -21
  142. package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
  143. package/dist/lib/reliability/usage.js +16 -5
  144. package/dist/lib/reliability/walk.js +3 -2
  145. package/dist/lib/security/auth-crypto/hashing.js +24 -8
  146. package/dist/lib/security/auth-crypto/jwt.js +21 -6
  147. package/dist/lib/security/auth-crypto/secrets.js +44 -1
  148. package/dist/lib/security/auth-crypto/shared.js +3 -1
  149. package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
  150. package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
  151. package/dist/lib/security/cli.js +21 -6
  152. package/dist/lib/security/cookie-credentials.js +115 -0
  153. package/dist/lib/security/cookie-csrf.js +241 -0
  154. package/dist/lib/security/deps/advisory.js +50 -109
  155. package/dist/lib/security/deps/cli.js +1 -1
  156. package/dist/lib/security/deps/cvss-score.js +14 -0
  157. package/dist/lib/security/deps/fixed-version.js +97 -0
  158. package/dist/lib/security/deps/lockfiles.js +135 -74
  159. package/dist/lib/security/deps/osv.js +11 -3
  160. package/dist/lib/security/deps/scan.js +47 -20
  161. package/dist/lib/security/deps/yarn-lock.js +158 -0
  162. package/dist/lib/security/examples/injection.cjs +4 -0
  163. package/dist/lib/security/exposure/cookies.js +17 -2
  164. package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
  165. package/dist/lib/security/exposure/frontend-security.js +21 -14
  166. package/dist/lib/security/exposure/jsx-url.js +18 -33
  167. package/dist/lib/security/exposure/logging.js +49 -10
  168. package/dist/lib/security/exposure/prototype-pollution.js +126 -56
  169. package/dist/lib/security/exposure/react-html.js +26 -0
  170. package/dist/lib/security/exposure/reflected-xss.js +34 -5
  171. package/dist/lib/security/files/compose.js +126 -1
  172. package/dist/lib/security/files/dockerfile.js +62 -4
  173. package/dist/lib/security/files/index.js +1 -1
  174. package/dist/lib/security/injection/command.js +46 -18
  175. package/dist/lib/security/injection/index.js +2 -0
  176. package/dist/lib/security/injection/ldap.js +86 -0
  177. package/dist/lib/security/injection/nosql.js +28 -4
  178. package/dist/lib/security/injection/redos.js +1 -1
  179. package/dist/lib/security/injection/rule-ids.js +1 -0
  180. package/dist/lib/security/injection/shared.js +5 -1
  181. package/dist/lib/security/injection/sql.js +28 -13
  182. package/dist/lib/security/lexical-declaration.js +113 -0
  183. package/dist/lib/security/pg-dynamic-query.js +28 -1
  184. package/dist/lib/security/pg-identifier-guards.js +107 -3
  185. package/dist/lib/security/phase5-rules.js +26 -135
  186. package/dist/lib/security/polyglot.js +2 -2
  187. package/dist/lib/security/redirect-settings.js +98 -0
  188. package/dist/lib/security/request-forgery/open-redirect.js +18 -5
  189. package/dist/lib/security/request-forgery/path-traversal.js +84 -6
  190. package/dist/lib/security/request-forgery/shared.js +1 -7
  191. package/dist/lib/security/request-forgery/ssrf.js +176 -14
  192. package/dist/lib/security/rule-descriptions.js +5 -1
  193. package/dist/lib/security/rule-ids.js +7 -0
  194. package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
  195. package/dist/lib/security/site-fixes/exposure.js +21 -3
  196. package/dist/lib/security/site-fixes/files.js +11 -13
  197. package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
  198. package/dist/lib/security/site-fixes/injection.js +17 -4
  199. package/dist/lib/security/site-fixes/jwt.js +1 -20
  200. package/dist/lib/security/site-fixes/request-forgery.js +37 -29
  201. package/dist/lib/security/taint/binding-inputs.js +236 -0
  202. package/dist/lib/security/taint/filename-guard.js +45 -45
  203. package/dist/lib/security/taint/guards.js +14 -8
  204. package/dist/lib/security/taint/index.js +126 -154
  205. package/dist/lib/security/taint/propagate.js +43 -9
  206. package/dist/lib/security/taint/record-writes.js +161 -0
  207. package/dist/lib/security/taint/resolution-cache.js +13 -7
  208. package/dist/lib/security/taint/sanitizers.js +89 -5
  209. package/dist/lib/security/taint/trace-nodes.js +61 -0
  210. package/dist/lib/security/taint/validators.js +89 -9
  211. package/dist/lib/shared/agent/agent-init.js +28 -5
  212. package/dist/lib/shared/agent/agent-prompt.js +74 -0
  213. package/dist/lib/shared/agent/agent-session.js +6 -1
  214. package/dist/lib/shared/agent/agent-verify.js +58 -11
  215. package/dist/lib/shared/agent/git-hook.js +17 -6
  216. package/dist/lib/shared/agent/review-delivery.js +1 -1
  217. package/dist/lib/shared/agent/review-mcp.js +8 -2
  218. package/dist/lib/shared/analysis/ast-semantics.js +2 -2
  219. package/dist/lib/shared/analysis/scan-cache.js +99 -14
  220. package/dist/lib/shared/check.js +172 -20
  221. package/dist/lib/shared/cli-dispatch.js +17 -0
  222. package/dist/lib/shared/config.js +27 -10
  223. package/dist/lib/shared/explain.js +1 -1
  224. package/dist/lib/shared/fix/fix-convergence.js +19 -31
  225. package/dist/lib/shared/fix/fix-handoff.js +16 -11
  226. package/dist/lib/shared/fix/fix-imports.js +4 -162
  227. package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
  228. package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
  229. package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
  230. package/dist/lib/shared/fix/fix-presentation.js +9 -6
  231. package/dist/lib/shared/fix/fix-safety.js +32 -1
  232. package/dist/lib/shared/fix/fix-scope.js +82 -0
  233. package/dist/lib/shared/fix/fix-terminal.js +28 -1
  234. package/dist/lib/shared/fix/fix-typecheck.js +8 -4
  235. package/dist/lib/shared/fix/import-text-edits.js +148 -0
  236. package/dist/lib/shared/fix/project-file-access.js +17 -26
  237. package/dist/lib/shared/fix/read-fix-text.js +20 -0
  238. package/dist/lib/shared/fix.js +241 -104
  239. package/dist/lib/shared/formats/finding-location.js +4 -1
  240. package/dist/lib/shared/formats/github.js +3 -3
  241. package/dist/lib/shared/formats/sarif.js +9 -3
  242. package/dist/lib/shared/report-fp.js +97 -0
  243. package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
  244. package/dist/lib/shared/reporting/baseline.js +7 -5
  245. package/dist/lib/shared/reporting/check-text.js +50 -0
  246. package/dist/lib/shared/reporting/pillars.js +54 -19
  247. package/dist/lib/shared/reporting/source-report.js +3 -0
  248. package/dist/lib/shared/system/atomic-write.js +5 -2
  249. package/dist/lib/shared/system/glob.js +34 -8
  250. package/dist/lib/shared/system/sentry.js +222 -0
  251. package/dist/lib/shared/system/shell-argument.js +5 -1
  252. package/dist/lib/shared/system/terminal-safe.js +3 -2
  253. package/dist/lib/shared/system/terminal-style.js +131 -0
  254. package/dist/lib/shared/workspaces.js +15 -3
  255. package/docs/assets/cli-workflow.gif +0 -0
  256. package/integrations/sublime/plugin.py +22 -0
  257. package/integrations/zed/Cargo.lock +845 -0
  258. package/integrations/zed/Cargo.toml +11 -0
  259. package/integrations/zed/extension.toml +11 -0
  260. package/integrations/zed/src/lib.rs +25 -0
  261. package/package.json +15 -3
  262. package/plugin/.claude-plugin/plugin.json +1 -1
  263. package/plugin/commands/loadwise.md +11 -6
  264. package/plugin/hooks/check-edited-file.mjs +27 -5
  265. package/polyglot/performance/rules/csharp.yml +34 -5
  266. package/polyglot/performance/rules/java.yml +24 -1
  267. package/polyglot/performance/rules/python.yml +74 -2
  268. package/polyglot/performance/sgconfig.yml +1 -0
  269. package/polyglot/reliability/rules/csharp.yml +12 -6
  270. package/polyglot/reliability/rules/go.yml +86 -6
  271. package/polyglot/reliability/rules/java.yml +22 -1
  272. package/polyglot/reliability/sgconfig.yml +1 -0
  273. package/polyglot/security/rules/go.yml +100 -22
  274. package/polyglot/security/rules/python.yml +74 -6
  275. package/polyglot/security/rules/rust.yml +65 -0
  276. package/polyglot/security/sgconfig.yml +1 -0
  277. package/schema/loadwise.config.schema.json +556 -76
  278. /package/polyglot/{performance/rules → shared-rules}/input.yml +0 -0
@@ -2,6 +2,7 @@ import { createPositionIndex } from '../sql/locations.js';
2
2
  import * as pgParser from '../sql/pg-parser.js';
3
3
  import { parseMysqlStatements } from '../sql/mysql-tokens.js';
4
4
  import { createOperation, containsPlaceholder } from './operations.js';
5
+ import { selectedSettings, variableSettings } from './postgres-settings.js';
5
6
  const DATA_STATEMENTS = new Set(['InsertStmt', 'UpdateStmt', 'DeleteStmt', 'SelectStmt', 'CopyStmt', 'MergeStmt']);
6
7
  const CONSTRAINT_TYPES = {
7
8
  CONSTR_FOREIGN: 'foreign',
@@ -10,7 +11,6 @@ const CONSTRAINT_TYPES = {
10
11
  CONSTR_PRIMARY: 'primary',
11
12
  CONSTR_EXCLUSION: 'exclusion',
12
13
  };
13
- const SET_CONFIG_LOCK_TIMEOUT = /set_config\s*\(\s*'lock_timeout'/i;
14
14
  const MYSQL_CREATE_TABLE = /^\s*CREATE\s+(?:TEMPORARY\s+)?TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:[`"]?[\w$]+[`"]?\.)?[`"]?([\w$]+)[`"]?/i;
15
15
  const MYSQL_TRANSACTION_START = /^\s*(?:START\s+TRANSACTION|BEGIN)\b/i;
16
16
  const MYSQL_TRANSACTION_END = /^\s*(?:COMMIT|ROLLBACK)\b/i;
@@ -21,6 +21,14 @@ function stringValue(node) {
21
21
  function lastName(items) {
22
22
  return stringValue((items ?? []).at(-1));
23
23
  }
24
+ function relationFields(relation) {
25
+ return { table: relation?.relname ?? null, tableSchema: relation?.schemaname };
26
+ }
27
+ function objectNames(object) {
28
+ const items = object.List?.items ?? [];
29
+ const name = lastName(items);
30
+ return name ? [{ name, schema: stringValue(items.at(-2)) ?? undefined }] : [];
31
+ }
24
32
  function describeType(typeName) {
25
33
  if (!typeName)
26
34
  return null;
@@ -28,20 +36,6 @@ function describeType(typeName) {
28
36
  const modifiers = (typeName.typmods ?? []).map((modifier) => modifier.A_Const?.ival?.ival ?? null);
29
37
  return { name, modifiers, array: (typeName.arrayBounds ?? []).length > 0 };
30
38
  }
31
- function constantValue(node) {
32
- const constant = node?.A_Const;
33
- if (!constant)
34
- return null;
35
- if (constant.ival)
36
- return String(constant.ival.ival ?? 0);
37
- if (constant.sval)
38
- return constant.sval.sval;
39
- return null;
40
- }
41
- function isLockTimeoutDisabled(args) {
42
- const value = constantValue((args ?? [])[0]);
43
- return value === '0' || value === '0s' || value === '0ms';
44
- }
45
39
  function notNullColumnOfCheck(expression) {
46
40
  const test = expression?.NullTest;
47
41
  if (!test || test.nulltesttype !== 'IS_NOT_NULL')
@@ -50,10 +44,34 @@ function notNullColumnOfCheck(expression) {
50
44
  const fields = argument?.ColumnRef?.fields ?? [];
51
45
  return fields.length === 1 ? stringValue(fields[0]) : null;
52
46
  }
47
+ // Attribute nodes such as NOT ENFORCED trail the constraint they modify, up to the next real constraint.
48
+ function isNotEnforced(constraints, constraintIndex) {
49
+ for (const attribute of constraints.slice(constraintIndex + 1)) {
50
+ if (attribute.contype === 'CONSTR_ATTR_NOT_ENFORCED')
51
+ return true;
52
+ if (!attribute.contype?.startsWith('CONSTR_ATTR_'))
53
+ return false;
54
+ }
55
+ return false;
56
+ }
53
57
  function describeColumnDefinition(definition) {
54
58
  const constraints = (definition.constraints ?? []).flatMap((constraint) => constraint.Constraint ? [constraint.Constraint] : []);
55
59
  const defaultConstraint = constraints.find((constraint) => constraint.contype === 'CONSTR_DEFAULT');
56
60
  const type = describeType(definition.typeName);
61
+ const inlineConstraints = constraints.flatMap((constraint, index) => {
62
+ const constraintType = constraint.contype && CONSTRAINT_TYPES[constraint.contype];
63
+ if (constraintType !== 'foreign' && constraintType !== 'check')
64
+ return [];
65
+ if (isNotEnforced(constraints, index))
66
+ return [];
67
+ return [
68
+ {
69
+ constraintType,
70
+ constraintName: constraint.conname ?? null,
71
+ notValid: Boolean(constraint.skip_validation),
72
+ },
73
+ ];
74
+ });
57
75
  return {
58
76
  columnName: definition.colname,
59
77
  type,
@@ -63,6 +81,7 @@ function describeColumnDefinition(definition) {
63
81
  identity: constraints.some((constraint) => constraint.contype === 'CONSTR_IDENTITY'),
64
82
  generatedStored: constraints.some((constraint) => constraint.contype === 'CONSTR_GENERATED'),
65
83
  serial: /^(?:small|big)?serial[248]?$/.test(type?.name ?? ''),
84
+ ...(inlineConstraints.length > 0 ? { inlineConstraints } : {}),
66
85
  };
67
86
  }
68
87
  function describeConstraint(constraint) {
@@ -115,7 +134,8 @@ function postgresOperations(statement, base) {
115
134
  {
116
135
  ...base,
117
136
  kind: 'create-table',
118
- table: body.relation?.relname,
137
+ ...relationFields(body.relation),
138
+ createIfAbsent: body.if_not_exists,
119
139
  columns: (body.tableElts ?? []).flatMap((element) => element.ColumnDef
120
140
  ? [{ name: element.ColumnDef.colname ?? null, type: describeType(element.ColumnDef.typeName) }]
121
141
  : []),
@@ -123,15 +143,23 @@ function postgresOperations(statement, base) {
123
143
  ];
124
144
  }
125
145
  case 'CreateTableAsStmt':
126
- return [{ ...base, kind: 'create-table', table: node.CreateTableAsStmt?.into?.rel?.relname ?? null }];
146
+ return [
147
+ {
148
+ ...base,
149
+ kind: 'create-table',
150
+ ...relationFields(node.CreateTableAsStmt?.into?.rel),
151
+ createIfAbsent: node.CreateTableAsStmt?.if_not_exists,
152
+ },
153
+ ];
127
154
  case 'IndexStmt': {
128
155
  const body = node.IndexStmt ?? {};
129
156
  return [
130
157
  {
131
158
  ...base,
132
159
  kind: 'create-index',
133
- table: body.relation?.relname,
160
+ ...relationFields(body.relation),
134
161
  index: body.idxname ?? null,
162
+ indexSchema: body.relation?.schemaname,
135
163
  concurrent: Boolean(body.concurrent),
136
164
  unique: Boolean(body.unique),
137
165
  // `CREATE INDEX ... ON ONLY parent` cannot be concurrent; it is the documented
@@ -148,7 +176,7 @@ function postgresOperations(statement, base) {
148
176
  return [{ ...base, kind: 'other' }];
149
177
  return (body.cmds ?? []).map((entry) => ({
150
178
  ...base,
151
- table: body.relation?.relname,
179
+ ...relationFields(body.relation),
152
180
  ...alterCommandFields(entry.AlterTableCmd ?? {}),
153
181
  }));
154
182
  }
@@ -158,21 +186,22 @@ function postgresOperations(statement, base) {
158
186
  return (node.TruncateStmt?.relations ?? []).map((relation) => ({
159
187
  ...base,
160
188
  kind: 'truncate',
161
- table: relation.RangeVar?.relname,
189
+ ...relationFields(relation.RangeVar),
162
190
  }));
163
191
  case 'VariableSetStmt': {
164
- const body = node.VariableSetStmt ?? {};
165
- // RESET and SET ... TO DEFAULT restore the server default, which is no timeout.
166
- if (body.name === 'lock_timeout' && body.kind === 'VAR_SET_VALUE' && !isLockTimeoutDisabled(body.args))
167
- return [{ ...base, kind: 'set-lock-timeout' }];
168
- return [{ ...base, kind: 'other' }];
192
+ const settings = variableSettings(node.VariableSetStmt ?? {});
193
+ return [{ ...base, kind: settings.lockTimeout?.value === 'enabled' ? 'set-lock-timeout' : 'other', ...settings }];
194
+ }
195
+ case 'SelectStmt': {
196
+ const settings = selectedSettings(node.SelectStmt ?? {});
197
+ return [{ ...base, kind: settings.lockTimeout?.value === 'enabled' ? 'set-lock-timeout' : 'data', ...settings }];
169
198
  }
170
199
  case 'TransactionStmt':
171
200
  return [transactionOperation(node.TransactionStmt ?? {}, base)];
172
201
  case 'VacuumStmt': {
173
202
  const body = node.VacuumStmt ?? {};
174
203
  return (body.options ?? []).some((option) => option.DefElem?.defname === 'full')
175
- ? [{ ...base, kind: 'table-rewrite-command', command: 'VACUUM FULL', table: vacuumTable(body) }]
204
+ ? [{ ...base, kind: 'table-rewrite-command', command: 'VACUUM FULL', ...relationFields(vacuumTable(body)) }]
176
205
  : [{ ...base, kind: 'other' }];
177
206
  }
178
207
  case 'ClusterStmt':
@@ -181,7 +210,7 @@ function postgresOperations(statement, base) {
181
210
  ...base,
182
211
  kind: 'table-rewrite-command',
183
212
  command: 'CLUSTER',
184
- table: node.ClusterStmt?.relation?.relname ?? null,
213
+ ...relationFields(node.ClusterStmt?.relation),
185
214
  },
186
215
  ];
187
216
  case 'ReindexStmt': {
@@ -191,7 +220,7 @@ function postgresOperations(statement, base) {
191
220
  ...base,
192
221
  kind: 'table-rewrite-command',
193
222
  command: 'REINDEX',
194
- table: body.relation?.relname ?? null,
223
+ ...relationFields(body.relation),
195
224
  concurrent: (body.params ?? []).some((parameter) => parameter.DefElem?.defname === 'concurrently'),
196
225
  },
197
226
  ];
@@ -201,24 +230,23 @@ function postgresOperations(statement, base) {
201
230
  {
202
231
  ...base,
203
232
  kind: 'alter-table-other',
204
- table: node.CreateTrigStmt?.relation?.relname ?? null,
233
+ ...relationFields(node.CreateTrigStmt?.relation),
205
234
  subtype: 'trigger',
206
235
  },
207
236
  ];
208
237
  default:
209
- if (kind === 'SelectStmt' && SET_CONFIG_LOCK_TIMEOUT.test(statement.text))
210
- return [{ ...base, kind: 'set-lock-timeout' }];
211
238
  return [{ ...base, kind: DATA_STATEMENTS.has(kind) ? 'data' : 'other' }];
212
239
  }
213
240
  }
214
241
  function vacuumTable(body) {
215
242
  const [first] = body.rels ?? [];
216
- return first?.VacuumRelation?.relation?.relname ?? null;
243
+ return first?.VacuumRelation?.relation;
217
244
  }
218
245
  function dropOperations(body, base) {
219
246
  const objects = body.objects ?? [];
247
+ const names = objects.flatMap(objectNames);
220
248
  if (body.removeType === 'OBJECT_TABLE') {
221
- return objects.map((object) => ({ ...base, kind: 'drop-table', table: lastName(object.List?.items) }));
249
+ return names.map(({ name, schema }) => ({ ...base, kind: 'drop-table', table: name, tableSchema: schema }));
222
250
  }
223
251
  if (body.removeType === 'OBJECT_INDEX') {
224
252
  return [
@@ -226,7 +254,10 @@ function dropOperations(body, base) {
226
254
  ...base,
227
255
  kind: 'drop-index',
228
256
  concurrent: Boolean(body.concurrent),
229
- index: lastName(objects[0]?.List?.items),
257
+ index: names[0]?.name ?? null,
258
+ indexSchema: names[0]?.schema,
259
+ dropIndexes: names,
260
+ dropCascade: body.behavior === 'DROP_CASCADE',
230
261
  },
231
262
  ];
232
263
  }
@@ -237,7 +268,7 @@ function renameOperation(body, base) {
237
268
  return {
238
269
  ...base,
239
270
  kind: 'rename-constraint',
240
- table: body.relation?.relname,
271
+ ...relationFields(body.relation),
241
272
  constraintName: body.subname,
242
273
  newName: body.newname,
243
274
  };
@@ -246,13 +277,13 @@ function renameOperation(body, base) {
246
277
  return {
247
278
  ...base,
248
279
  kind: 'rename-column',
249
- table: body.relation?.relname,
280
+ ...relationFields(body.relation),
250
281
  columnName: body.subname,
251
282
  newName: body.newname,
252
283
  };
253
284
  }
254
285
  if (body.renameType === 'OBJECT_TABLE') {
255
- return { ...base, kind: 'rename-table', table: body.relation?.relname, newName: body.newname };
286
+ return { ...base, kind: 'rename-table', ...relationFields(body.relation), newName: body.newname };
256
287
  }
257
288
  return { ...base, kind: 'other' };
258
289
  }
@@ -260,9 +291,22 @@ function transactionOperation(body, base) {
260
291
  if (body.kind === 'TRANS_STMT_BEGIN' || body.kind === 'TRANS_STMT_START')
261
292
  return { ...base, kind: 'transaction-begin' };
262
293
  if (body.kind === 'TRANS_STMT_COMMIT' || body.kind === 'TRANS_STMT_ROLLBACK')
263
- return { ...base, kind: 'transaction-end' };
294
+ return {
295
+ ...base,
296
+ kind: 'transaction-end',
297
+ transactionRollback: body.kind === 'TRANS_STMT_ROLLBACK',
298
+ transactionChain: body.chain,
299
+ };
300
+ const savepointAction = SAVEPOINT_ACTIONS[body.kind ?? ''];
301
+ if (savepointAction && body.savepoint_name)
302
+ return { ...base, kind: 'other', savepointAction, savepointName: body.savepoint_name };
264
303
  return { ...base, kind: 'other' };
265
304
  }
305
+ const SAVEPOINT_ACTIONS = {
306
+ TRANS_STMT_SAVEPOINT: 'create',
307
+ TRANS_STMT_ROLLBACK_TO: 'rollback',
308
+ TRANS_STMT_RELEASE: 'release',
309
+ };
266
310
  function lineStartsOf(text) {
267
311
  const starts = [0];
268
312
  for (let index = 0; index < text.length; index++)
@@ -68,10 +68,10 @@ function containsPlaceholder(value) {
68
68
  }
69
69
  // Postgres folds unquoted names before they reach us, MySQL names are case-insensitive on common
70
70
  // setups.
71
- function tableKey(name, dialect) {
71
+ function tableKey(name, dialect, schema) {
72
72
  if (!name)
73
73
  return null;
74
- return dialect === 'mysql' ? name.toLowerCase() : name;
74
+ return dialect === 'mysql' ? name.toLowerCase() : JSON.stringify([schema ?? null, name]);
75
75
  }
76
76
  function quoteIdentifier(name, dialect = 'postgres') {
77
77
  if (!name)
@@ -96,26 +96,39 @@ function annotateUnit(operations, { inTransaction, dialect }) {
96
96
  const createdTables = new Set();
97
97
  const emptiedTables = new Set();
98
98
  const renamedIntoTables = new Set();
99
+ const savepoints = [];
99
100
  let explicitTransaction = false;
100
101
  for (const operation of operations) {
101
102
  if (operation.kind === 'transaction-begin')
102
103
  explicitTransaction = true;
103
- else if (operation.kind === 'transaction-end')
104
- explicitTransaction = false;
104
+ else if (operation.kind === 'transaction-end') {
105
+ explicitTransaction = Boolean(operation.transactionChain);
106
+ emptiedTables.clear();
107
+ savepoints.length = 0;
108
+ }
109
+ applySavepoint(operation, savepoints, emptiedTables);
105
110
  operation.inTransaction = Boolean(inTransaction || explicitTransaction || operation.explicitTransaction);
106
- const key = tableKey(operation.table, dialect);
107
- if (operation.kind === 'create-table' && key)
111
+ if (operation.changesSearchPath)
112
+ forgetUnqualifiedTables(createdTables, emptiedTables, renamedIntoTables);
113
+ const key = tableKey(operation.table, dialect, operation.tableSchema);
114
+ const renamedKey = operation.kind === 'rename-table' ? tableKey(operation.newName, dialect, operation.tableSchema) : null;
115
+ if (operation.kind === 'create-table' && key && !operation.createIfAbsent)
108
116
  createdTables.add(key);
109
- if (operation.kind === 'rename-table' && createdTables.has(key) && operation.newName) {
110
- createdTables.add(tableKey(operation.newName, dialect));
111
- }
112
117
  operation.createdInSameUnit = key !== null && createdTables.has(key);
113
118
  operation.emptiedInSameUnit = key !== null && emptiedTables.has(key);
119
+ if (operation.kind === 'drop-index')
120
+ operation.emptiedTables = new Set(emptiedTables);
114
121
  // A name that only exists because this unit renamed a table into it is unknown to running app
115
122
  // versions.
116
123
  operation.renamedInSameUnit = key !== null && renamedIntoTables.has(key);
117
- if (operation.kind === 'rename-table' && operation.newName)
118
- renamedIntoTables.add(tableKey(operation.newName, dialect));
124
+ if (renamedKey) {
125
+ if (createdTables.delete(key))
126
+ createdTables.add(renamedKey);
127
+ if (emptiedTables.delete(key))
128
+ emptiedTables.add(renamedKey);
129
+ renamedIntoTables.add(renamedKey);
130
+ renamedIntoTables.delete(key);
131
+ }
119
132
  // Inside a transaction the TRUNCATE lock is held, so the table stays empty for the rest of the
120
133
  // unit.
121
134
  if (operation.kind === 'truncate' && key && operation.inTransaction)
@@ -128,16 +141,46 @@ function annotateUnit(operations, { inTransaction, dialect }) {
128
141
  });
129
142
  return operations;
130
143
  }
144
+ // Rolling back to a savepoint brings back the rows of tables emptied after it. An unknown savepoint
145
+ // proves nothing.
146
+ function applySavepoint({ savepointAction, savepointName }, savepoints, emptiedTables) {
147
+ if (!savepointAction)
148
+ return;
149
+ if (savepointAction === 'create') {
150
+ savepoints.push({ name: savepointName, emptiedTables: new Set(emptiedTables) });
151
+ return;
152
+ }
153
+ const position = savepoints.findLastIndex(({ name }) => name === savepointName);
154
+ if (savepointAction === 'rollback') {
155
+ emptiedTables.clear();
156
+ for (const key of savepoints[position]?.emptiedTables ?? [])
157
+ emptiedTables.add(key);
158
+ savepoints.length = position + 1;
159
+ }
160
+ else if (position !== -1)
161
+ savepoints.length = position;
162
+ }
163
+ function forgetUnqualifiedTables(...tables) {
164
+ for (const names of tables) {
165
+ for (const key of names)
166
+ if (key?.startsWith('[null,'))
167
+ names.delete(key);
168
+ }
169
+ }
131
170
  // Rename-and-recreate in one transaction: readers never see the old name disappear.
132
171
  function isRecreatedInSameTransaction(operations, position, dialect) {
133
172
  const renamed = operations[position];
134
173
  if (!renamed.inTransaction)
135
174
  return false;
136
- const key = tableKey(renamed.table, dialect);
175
+ const key = tableKey(renamed.table, dialect, renamed.tableSchema);
137
176
  for (const later of operations.slice(position + 1)) {
138
177
  if (later.kind === 'transaction-end' || !later.inTransaction)
139
178
  return false;
140
- if (later.kind === 'create-table' && tableKey(later.table, dialect) === key)
179
+ if (later.changesSearchPath && !renamed.tableSchema)
180
+ return false;
181
+ if (later.kind === 'create-table' &&
182
+ !later.createIfAbsent &&
183
+ tableKey(later.table, dialect, later.tableSchema) === key)
141
184
  return true;
142
185
  }
143
186
  return false;
@@ -0,0 +1,52 @@
1
+ function applyTimeoutSetting(settings, operation) {
2
+ const setting = operation.lockTimeout;
3
+ if (!setting)
4
+ return settings;
5
+ if (setting.scope === 'local')
6
+ return operation.inTransaction ? { ...settings, local: setting.value } : settings;
7
+ if (setting.scope === 'session')
8
+ return { session: setting.value };
9
+ // Both interpretations must agree before a setting with unknown scope establishes protection.
10
+ const session = settings.session === setting.value ? settings.session : 'unknown';
11
+ return operation.inTransaction ? { session, local: setting.value } : { session };
12
+ }
13
+ function applySavepoint(transaction, settings, operation) {
14
+ const { savepointAction, savepointName } = operation;
15
+ if (!savepointName || !savepointAction)
16
+ return settings;
17
+ if (savepointAction === 'create') {
18
+ transaction.savepoints.push({ name: savepointName, settings });
19
+ return settings;
20
+ }
21
+ const position = transaction.savepoints.findLastIndex(({ name }) => name === savepointName);
22
+ if (position < 0)
23
+ return settings;
24
+ const saved = transaction.savepoints[position].settings;
25
+ transaction.savepoints.splice(savepointAction === 'release' ? position : position + 1);
26
+ return savepointAction === 'rollback' ? saved : settings;
27
+ }
28
+ export function* operationsWithoutLockTimeout(operations) {
29
+ let settings = { session: 'unknown' };
30
+ let transaction;
31
+ let activeCallback;
32
+ for (const operation of operations) {
33
+ const connection = operation.transactionConnectionScope ?? operation.transactionScope;
34
+ if (connection !== activeCallback) {
35
+ // A separate callback may check out a different connection.
36
+ settings = { session: 'unknown' };
37
+ transaction = undefined;
38
+ activeCallback = connection;
39
+ }
40
+ if (operation.kind === 'transaction-end' || !operation.inTransaction) {
41
+ settings = operation.transactionRollback && transaction ? transaction.initial : { session: settings.session };
42
+ transaction = undefined;
43
+ }
44
+ if (operation.inTransaction)
45
+ transaction ??= { initial: settings, savepoints: [] };
46
+ if (transaction)
47
+ settings = applySavepoint(transaction, settings, operation);
48
+ settings = applyTimeoutSetting(settings, operation);
49
+ if ((settings.local ?? settings.session) !== 'enabled')
50
+ yield operation;
51
+ }
52
+ }
@@ -1,3 +1,7 @@
1
+ import { tableKey } from './operations.js';
2
+ export function postgresTableIdentity({ table, tableSchema }) {
3
+ return tableKey(table, 'postgres', tableSchema);
4
+ }
1
5
  function createPostgresRegistry() {
2
6
  return { columnTypes: new Map(), notNullChecks: new Map(), indexTables: new Map() };
3
7
  }
@@ -44,7 +48,7 @@ function updateConstraintName(checks, oldName, newName) {
44
48
  // Repoints (or, with `to` null, forgets) the indexes of a renamed or dropped table.
45
49
  function renameIndexTable(indexTables, from, to) {
46
50
  for (const [index, table] of indexTables) {
47
- if (table !== from)
51
+ if (postgresTableIdentity(table) !== from)
48
52
  continue;
49
53
  if (to)
50
54
  indexTables.set(index, to);
@@ -52,24 +56,119 @@ function renameIndexTable(indexTables, from, to) {
52
56
  indexTables.delete(index);
53
57
  }
54
58
  }
55
- // A plain DROP INDEX gets the table of the scanned migration that created the index, and that
56
- // table's unit context.
57
- function resolveDroppedIndexTable(operation, unit, registry) {
58
- if (operation.kind !== 'drop-index' || operation.table || !operation.index)
59
- return;
60
- const table = registry.indexTables.get(operation.index);
59
+ // A rollback only reverts renames made after its savepoint was created. An unknown savepoint proves
60
+ // nothing, so it may revert any earlier rename.
61
+ function renameFollowsRolledBackSavepoint(earlier, renamePosition) {
62
+ const savepoints = [];
63
+ for (const [position, { savepointAction, savepointName }] of earlier.entries()) {
64
+ if (!savepointAction)
65
+ continue;
66
+ if (savepointAction === 'create') {
67
+ savepoints.push({ name: savepointName, position });
68
+ continue;
69
+ }
70
+ const index = savepoints.findLastIndex(({ name }) => name === savepointName);
71
+ if (savepointAction === 'release') {
72
+ if (index !== -1)
73
+ savepoints.length = index;
74
+ continue;
75
+ }
76
+ if (index === -1) {
77
+ if (position > renamePosition)
78
+ return true;
79
+ continue;
80
+ }
81
+ if (position > renamePosition && savepoints[index].position < renamePosition)
82
+ return true;
83
+ savepoints.length = index + 1;
84
+ }
85
+ return false;
86
+ }
87
+ function indexTargetContext({ name, schema }, earlier, registry, emptiedTables) {
88
+ const index = tableKey(name, 'postgres', schema);
89
+ // A bare name could resolve to a different index earlier in the server search path.
90
+ const table = index && registry.indexTables.get(index);
61
91
  if (!table)
62
92
  return;
93
+ const tableIdentity = postgresTableIdentity(table);
94
+ // An unqualified index lookup refers to the search path in effect at this statement.
95
+ const lastSearchPathChange = earlier.findLastIndex((operation) => operation.changesSearchPath);
96
+ const recent = schema ? earlier : earlier.slice(lastSearchPathChange + 1);
97
+ // Truncation evidence recorded before a search-path change may describe a different index's table.
98
+ const searchPathChanged = !schema && lastSearchPathChange >= 0;
99
+ // The registry cannot undo a rename that a savepoint rollback reverted, so it may map this index to
100
+ // a name that now belongs to another table. A bare name may match any schema, a qualified one only its own.
101
+ const renameMayBeRolledBack = earlier.some((operation, position) => operation.kind === 'rename-table' &&
102
+ (operation.table === table.table || operation.newName === table.table) &&
103
+ (!schema || !operation.tableSchema || operation.tableSchema === schema) &&
104
+ renameFollowsRolledBackSavepoint(earlier, position));
105
+ return {
106
+ table,
107
+ created: recent.some((previous) => (postgresTableIdentity(previous) === tableIdentity && previous.createdInSameUnit) ||
108
+ (previous.kind === 'create-index' && tableKey(previous.index, 'postgres', previous.indexSchema) === index)),
109
+ emptied: !renameMayBeRolledBack && !searchPathChanged && emptiedTables?.has(tableIdentity),
110
+ };
111
+ }
112
+ // For multiple indexes, every target must be proven safe before the statement can be exempted.
113
+ function resolveDroppedIndexTable(operation, unit, registry) {
114
+ if (operation.kind !== 'drop-index' || operation.table)
115
+ return;
63
116
  const earlier = unit.operations.slice(0, unit.operations.indexOf(operation));
64
- operation.table = table;
65
- operation.createdInSameUnit = earlier.some((previous) => (previous.table === table && (previous.kind === 'create-table' || previous.createdInSameUnit)) ||
66
- (previous.kind === 'create-index' && previous.index === operation.index));
67
- operation.emptiedInSameUnit = earlier.some((previous) => previous.kind === 'truncate' && previous.table === table && previous.inTransaction);
117
+ const targets = (operation.dropIndexes ?? [{ name: operation.index, schema: operation.indexSchema }]).map((target) => indexTargetContext(target, earlier, registry, operation.emptiedTables));
118
+ if (targets.length === 0)
119
+ return;
120
+ if (targets.length === 1 && targets[0])
121
+ Object.assign(operation, targets[0].table);
122
+ operation.createdInSameUnit = targets.every((target) => target?.created);
123
+ operation.emptiedInSameUnit = targets.every((target) => target?.created || target?.emptied);
124
+ }
125
+ // A qualified and an unqualified name may be the same table, so a change through one makes what is
126
+ // recorded under the other uncertain.
127
+ function forgetAliasRecords(records, operation, forgetEntry) {
128
+ const { table, tableSchema } = operation;
129
+ const tableChanged = operation.kind === 'rename-table' || operation.kind === 'drop-table';
130
+ for (const [key, entry] of records) {
131
+ const [schema, name] = JSON.parse(key);
132
+ const oneIsUnqualified = (schema === null) !== !tableSchema;
133
+ if (name !== table || !oneIsUnqualified)
134
+ continue;
135
+ if (tableChanged)
136
+ records.delete(key);
137
+ else
138
+ forgetEntry(entry, operation);
139
+ }
140
+ }
141
+ const TYPE_CHANGING_KINDS = new Set(['add-column', 'alter-column-type', 'drop-column', 'rename-column']);
142
+ function forgetColumnTypes(types, operation) {
143
+ if (!TYPE_CHANGING_KINDS.has(operation.kind))
144
+ return;
145
+ types.delete(operation.columnName);
146
+ if (operation.newName)
147
+ types.delete(operation.newName);
148
+ }
149
+ function forgetChecks(checks, operation) {
150
+ const { kind, columnName, constraintName } = operation;
151
+ if (kind === 'drop-column' || kind === 'rename-column')
152
+ updateColumnChecks(checks, columnName);
153
+ if (kind === 'drop-constraint' || kind === 'rename-constraint')
154
+ updateConstraintName(checks, constraintName, null);
68
155
  }
69
156
  function recordOperation(registry, operation) {
70
- const { table, columnName } = operation;
157
+ const { columnName } = operation;
158
+ if (operation.kind === 'drop-index') {
159
+ const objects = operation.dropIndexes ?? [{ name: operation.index, schema: operation.indexSchema }];
160
+ for (const { name, schema } of objects) {
161
+ const index = tableKey(name, 'postgres', schema);
162
+ if (index)
163
+ registry.indexTables.delete(index);
164
+ }
165
+ return;
166
+ }
167
+ const table = postgresTableIdentity(operation);
71
168
  if (!table)
72
169
  return;
170
+ forgetAliasRecords(registry.columnTypes, operation, forgetColumnTypes);
171
+ forgetAliasRecords(registry.notNullChecks, operation, forgetChecks);
73
172
  switch (operation.kind) {
74
173
  case 'create-table':
75
174
  for (const column of operation.columns ?? [])
@@ -88,24 +187,25 @@ function recordOperation(registry, operation) {
88
187
  renameEntry(registry.columnTypes.get(table), columnName, operation.newName);
89
188
  updateColumnChecks(registry.notNullChecks.get(table), columnName, operation.newName);
90
189
  break;
91
- case 'rename-table':
92
- renameEntry(registry.columnTypes, table, operation.newName);
93
- renameEntry(registry.notNullChecks, table, operation.newName);
94
- renameIndexTable(registry.indexTables, table, operation.newName);
190
+ case 'rename-table': {
191
+ const replacement = { table: operation.newName ?? null, tableSchema: operation.tableSchema };
192
+ const renamed = postgresTableIdentity(replacement);
193
+ renameEntry(registry.columnTypes, table, renamed);
194
+ renameEntry(registry.notNullChecks, table, renamed);
195
+ renameIndexTable(registry.indexTables, table, renamed ? replacement : undefined);
95
196
  break;
197
+ }
96
198
  case 'drop-table':
97
199
  registry.columnTypes.delete(table);
98
200
  registry.notNullChecks.delete(table);
99
- renameIndexTable(registry.indexTables, table, null);
100
- break;
101
- case 'create-index':
102
- if (operation.index)
103
- registry.indexTables.set(operation.index, table);
201
+ renameIndexTable(registry.indexTables, table);
104
202
  break;
105
- case 'drop-index':
106
- if (operation.index)
107
- registry.indexTables.delete(operation.index);
203
+ case 'create-index': {
204
+ const index = tableKey(operation.index, 'postgres', operation.indexSchema);
205
+ if (index)
206
+ registry.indexTables.set(index, { table: operation.table, tableSchema: operation.tableSchema });
108
207
  break;
208
+ }
109
209
  case 'add-constraint':
110
210
  if (operation.checkNotNullColumn) {
111
211
  tableEntry(registry.notNullChecks, table).set(operation.constraintName ?? Symbol(), {
@@ -128,8 +228,9 @@ function recordOperation(registry, operation) {
128
228
  break;
129
229
  }
130
230
  }
131
- function hasValidatedNotNullCheck(registry, table, column) {
231
+ function hasValidatedNotNullCheck(registry, operation) {
232
+ const table = postgresTableIdentity(operation);
132
233
  const checks = table === null ? undefined : registry.notNullChecks.get(table);
133
- return [...(checks?.values() ?? [])].some((check) => check.column === column && check.validated);
234
+ return [...(checks?.values() ?? [])].some((check) => check.column === operation.columnName && check.validated);
134
235
  }
135
236
  export { createPostgresRegistry, hasValidatedNotNullCheck, resolveDroppedIndexTable, recordOperation };