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.
- package/README.md +175 -115
- package/dist/bin/loadwise.js +132 -67
- package/dist/lib/accessibility/analyze.js +5 -3
- package/dist/lib/accessibility/angular-inline.js +9 -11
- package/dist/lib/accessibility/angular-literal.js +39 -0
- package/dist/lib/accessibility/form-context.js +36 -0
- package/dist/lib/accessibility/frame-head.js +92 -0
- package/dist/lib/accessibility/frame-viewport.js +149 -0
- package/dist/lib/accessibility/frontend-detect.js +6 -5
- package/dist/lib/accessibility/jsx-elements.js +13 -3
- package/dist/lib/accessibility/markup-files.js +7 -2
- package/dist/lib/accessibility/rule-ids.js +3 -0
- package/dist/lib/accessibility/rules.js +128 -41
- package/dist/lib/accessibility/site-fixes/buttons.js +5 -1
- package/dist/lib/accessibility/template-attributes.js +5 -3
- package/dist/lib/accessibility/template-elements.js +25 -6
- package/dist/lib/accessibility/template-file.js +40 -25
- package/dist/lib/accessibility/usage.js +10 -2
- package/dist/lib/editor/accessibility-examples.js +6 -1
- package/dist/lib/editor/actions.js +46 -7
- package/dist/lib/editor/apply-fix.js +69 -4
- package/dist/lib/editor/configured-setup.js +63 -0
- package/dist/lib/editor/diagnostics.js +206 -17
- package/dist/lib/editor/dismissals.js +85 -26
- package/dist/lib/editor/editor-document.js +5 -2
- package/dist/lib/editor/editor-launcher.js +170 -0
- package/dist/lib/editor/fix-edits.js +19 -0
- package/dist/lib/editor/jetbrains-setup.js +53 -0
- package/dist/lib/editor/language-server.js +80 -24
- package/dist/lib/editor/managed-config.js +37 -0
- package/dist/lib/editor/native-setup.js +86 -0
- package/dist/lib/editor/perf-watch.js +8 -6
- package/dist/lib/editor/release-vsix.js +61 -0
- package/dist/lib/editor/reliability-examples.js +15 -0
- package/dist/lib/editor/scan-session.js +29 -8
- package/dist/lib/editor/server-commands.js +38 -9
- package/dist/lib/editor/setup-server.js +87 -0
- package/dist/lib/editor/setup-vim.js +63 -0
- package/dist/lib/editor/setup.js +131 -0
- package/dist/lib/editor/stdio-setup.js +6 -0
- package/dist/lib/editor/workspace-files.js +61 -7
- package/dist/lib/editor/workspace-paths.js +41 -1
- package/dist/lib/maintainability/analysis/analyze-source.js +1 -1
- package/dist/lib/maintainability/architecture/architecture.js +4 -4
- package/dist/lib/maintainability/cli/changed-lines.js +20 -2
- package/dist/lib/maintainability/cli/inputs.js +12 -3
- package/dist/lib/maintainability/cli.js +18 -5
- package/dist/lib/maintainability/entry-points/entry-points.js +44 -15
- package/dist/lib/maintainability/entry-points/html-entry-points.js +5 -1
- package/dist/lib/maintainability/examples.js +4 -0
- package/dist/lib/maintainability/quality/functions.js +9 -6
- package/dist/lib/maintainability/quality/limits.js +19 -0
- package/dist/lib/maintainability/quality/size-limits.js +8 -2
- package/dist/lib/maintainability/quality/stray-console.js +62 -1
- package/dist/lib/maintainability/quality/tsconfig-strict.js +2 -0
- package/dist/lib/maintainability/rule-ids.js +3 -0
- package/dist/lib/maintainability/scan-changed.js +4 -1
- package/dist/lib/maintainability/scan-worker.js +6 -2
- package/dist/lib/maintainability/scan.js +21 -8
- package/dist/lib/maintainability/site-fixes/unused-bindings.js +138 -3
- package/dist/lib/maintainability/unused/module-usage.js +43 -21
- package/dist/lib/maintainability/unused/unused-bindings.js +39 -4
- package/dist/lib/maintainability/unused/unused-modules.js +5 -10
- package/dist/lib/performance/analysis/source-inputs.js +7 -2
- package/dist/lib/performance/analysis/source-options.js +7 -6
- package/dist/lib/performance/analyze.js +16 -3
- package/dist/lib/performance/cli/changed-lines.js +8 -1
- package/dist/lib/performance/cli/cli-arguments.js +19 -8
- package/dist/lib/performance/cli/cli-output.js +4 -0
- package/dist/lib/performance/cli/cli-scan.js +21 -2
- package/dist/lib/performance/cli.js +16 -7
- package/dist/lib/performance/collections/memory-growth.js +55 -12
- package/dist/lib/performance/contracts/openapi.js +13 -4
- package/dist/lib/performance/datastores/pg-integrity.js +68 -22
- package/dist/lib/performance/datastores/pg-session-release.js +114 -0
- package/dist/lib/performance/datastores/pg-session-sql.js +144 -0
- package/dist/lib/performance/datastores/pooling.js +62 -3
- package/dist/lib/performance/datastores/redis.js +94 -18
- package/dist/lib/performance/datastores/session-store.js +2 -1
- package/dist/lib/performance/db/checks.js +19 -8
- package/dist/lib/performance/db/gateway.js +3 -1
- package/dist/lib/performance/db/plan-estimates.js +4 -4
- package/dist/lib/performance/engine/native-syntax.js +3 -0
- package/dist/lib/performance/engine/native.js +11 -7
- package/dist/lib/performance/evidence/plans.js +7 -5
- package/dist/lib/performance/evidence/traces.js +21 -1
- package/dist/lib/performance/frontend/angular-teardown.js +96 -0
- package/dist/lib/performance/frontend/effect-cleanup.js +148 -0
- package/dist/lib/performance/frontend/frontend-dom-perf.js +14 -2
- package/dist/lib/performance/frontend/frontend-helpers.js +1 -9
- package/dist/lib/performance/frontend/frontend-perf.js +149 -74
- package/dist/lib/performance/migrations/extract-code-sql.js +16 -4
- package/dist/lib/performance/migrations/extract-code.js +13 -5
- package/dist/lib/performance/migrations/extract-sql.js +82 -38
- package/dist/lib/performance/migrations/operations.js +56 -13
- package/dist/lib/performance/migrations/postgres-lock-timeout.js +52 -0
- package/dist/lib/performance/migrations/postgres-registry.js +127 -26
- package/dist/lib/performance/migrations/postgres-settings.js +78 -0
- package/dist/lib/performance/migrations/rules-postgres-add-column.js +3 -3
- package/dist/lib/performance/migrations/rules-postgres-shared.js +9 -0
- package/dist/lib/performance/migrations/rules-postgres.js +88 -39
- package/dist/lib/performance/orm/offset-pagination.js +6 -4
- package/dist/lib/performance/orm/prisma-aggregates.js +69 -18
- package/dist/lib/performance/orm/unbounded-list-chain.js +29 -0
- package/dist/lib/performance/orm/unbounded-list-queries.js +18 -13
- package/dist/lib/performance/polyglot.js +111 -14
- package/dist/lib/performance/projects.js +3 -3
- package/dist/lib/performance/query-loops/sequential-loops.js +1 -0
- package/dist/lib/performance/request/backend-mastery.js +151 -42
- package/dist/lib/performance/request/bulk-listener-registration.js +111 -0
- package/dist/lib/performance/request/fastify-receivers.js +93 -0
- package/dist/lib/performance/request/fresh-loop-socket.js +103 -0
- package/dist/lib/performance/request/mongoose-lean.js +298 -46
- package/dist/lib/performance/request/node-event-listener-leak.js +99 -5
- package/dist/lib/performance/request/phase5-ast.js +6 -7
- package/dist/lib/performance/request/phase5-rules.js +204 -23
- package/dist/lib/performance/request/request-handlers.js +120 -23
- package/dist/lib/performance/request/sql-select-star.js +150 -0
- package/dist/lib/performance/request/unbounded-promise-all.js +54 -29
- package/dist/lib/performance/request/upload-limits.js +5 -1
- package/dist/lib/performance/site-fix.js +4 -4
- package/dist/lib/performance/site-fixes/backend-mastery-fixes.js +298 -16
- package/dist/lib/performance/site-fixes/collection-existence.js +2 -1
- package/dist/lib/performance/site-fixes/collections.js +1 -2
- package/dist/lib/performance/site-fixes/frontend-fixes.js +358 -14
- package/dist/lib/performance/site-fixes/helpers.js +3 -4
- package/dist/lib/performance/site-fixes/integrity.js +2 -4
- package/dist/lib/performance/site-fixes/migration-context.js +8 -1
- package/dist/lib/performance/site-fixes/migration-lock-rules.js +66 -30
- package/dist/lib/performance/site-fixes/prisma-aggregates.js +29 -9
- package/dist/lib/performance/site-fixes/small-rewrites.js +168 -10
- package/dist/lib/performance/site-fixes/sync-calls.js +14 -12
- package/dist/lib/performance/site-fixes/upload-limits.js +13 -7
- package/dist/lib/performance/sql/mysql-tokens.js +38 -12
- package/dist/lib/reliability/boundary-access.js +114 -57
- package/dist/lib/reliability/react-bugs.js +29 -7
- package/dist/lib/reliability/resource-leak-intervals.js +258 -0
- package/dist/lib/reliability/resource-leaks.js +209 -195
- package/dist/lib/reliability/rule-ids.js +10 -1
- package/dist/lib/reliability/site-fixes/resource-leaks.js +16 -1
- package/dist/lib/reliability/swallowed-errors.js +107 -21
- package/dist/lib/reliability/test-cleanup-after-await.js +92 -18
- package/dist/lib/reliability/usage.js +16 -5
- package/dist/lib/reliability/walk.js +3 -2
- package/dist/lib/security/auth-crypto/hashing.js +24 -8
- package/dist/lib/security/auth-crypto/jwt.js +21 -6
- package/dist/lib/security/auth-crypto/secrets.js +44 -1
- package/dist/lib/security/auth-crypto/shared.js +3 -1
- package/dist/lib/security/auth-crypto/signing-algorithm.js +79 -0
- package/dist/lib/security/auth-crypto/transport-and-ciphers.js +85 -1
- package/dist/lib/security/cli.js +21 -6
- package/dist/lib/security/cookie-credentials.js +115 -0
- package/dist/lib/security/cookie-csrf.js +241 -0
- package/dist/lib/security/deps/advisory.js +50 -109
- package/dist/lib/security/deps/cli.js +1 -1
- package/dist/lib/security/deps/cvss-score.js +14 -0
- package/dist/lib/security/deps/fixed-version.js +97 -0
- package/dist/lib/security/deps/lockfiles.js +135 -74
- package/dist/lib/security/deps/osv.js +11 -3
- package/dist/lib/security/deps/scan.js +47 -20
- package/dist/lib/security/deps/yarn-lock.js +158 -0
- package/dist/lib/security/examples/injection.cjs +4 -0
- package/dist/lib/security/exposure/cookies.js +17 -2
- package/dist/lib/security/exposure/cors-origin-regex.js +2 -3
- package/dist/lib/security/exposure/frontend-security.js +21 -14
- package/dist/lib/security/exposure/jsx-url.js +18 -33
- package/dist/lib/security/exposure/logging.js +49 -10
- package/dist/lib/security/exposure/prototype-pollution.js +126 -56
- package/dist/lib/security/exposure/react-html.js +26 -0
- package/dist/lib/security/exposure/reflected-xss.js +34 -5
- package/dist/lib/security/files/compose.js +126 -1
- package/dist/lib/security/files/dockerfile.js +62 -4
- package/dist/lib/security/files/index.js +1 -1
- package/dist/lib/security/injection/command.js +46 -18
- package/dist/lib/security/injection/index.js +2 -0
- package/dist/lib/security/injection/ldap.js +86 -0
- package/dist/lib/security/injection/nosql.js +28 -4
- package/dist/lib/security/injection/redos.js +1 -1
- package/dist/lib/security/injection/rule-ids.js +1 -0
- package/dist/lib/security/injection/shared.js +5 -1
- package/dist/lib/security/injection/sql.js +28 -13
- package/dist/lib/security/lexical-declaration.js +113 -0
- package/dist/lib/security/pg-dynamic-query.js +28 -1
- package/dist/lib/security/pg-identifier-guards.js +107 -3
- package/dist/lib/security/phase5-rules.js +26 -135
- package/dist/lib/security/polyglot.js +2 -2
- package/dist/lib/security/redirect-settings.js +98 -0
- package/dist/lib/security/request-forgery/open-redirect.js +18 -5
- package/dist/lib/security/request-forgery/path-traversal.js +84 -6
- package/dist/lib/security/request-forgery/shared.js +1 -7
- package/dist/lib/security/request-forgery/ssrf.js +176 -14
- package/dist/lib/security/rule-descriptions.js +5 -1
- package/dist/lib/security/rule-ids.js +7 -0
- package/dist/lib/security/site-fixes/auth-crypto.js +5 -78
- package/dist/lib/security/site-fixes/exposure.js +21 -3
- package/dist/lib/security/site-fixes/files.js +11 -13
- package/dist/lib/security/site-fixes/frontend-security-fixes.js +2 -2
- package/dist/lib/security/site-fixes/injection.js +17 -4
- package/dist/lib/security/site-fixes/jwt.js +1 -20
- package/dist/lib/security/site-fixes/request-forgery.js +37 -29
- package/dist/lib/security/taint/binding-inputs.js +236 -0
- package/dist/lib/security/taint/filename-guard.js +45 -45
- package/dist/lib/security/taint/guards.js +14 -8
- package/dist/lib/security/taint/index.js +126 -154
- package/dist/lib/security/taint/propagate.js +43 -9
- package/dist/lib/security/taint/record-writes.js +161 -0
- package/dist/lib/security/taint/resolution-cache.js +13 -7
- package/dist/lib/security/taint/sanitizers.js +89 -5
- package/dist/lib/security/taint/trace-nodes.js +61 -0
- package/dist/lib/security/taint/validators.js +89 -9
- package/dist/lib/shared/agent/agent-init.js +28 -5
- package/dist/lib/shared/agent/agent-prompt.js +74 -0
- package/dist/lib/shared/agent/agent-session.js +6 -1
- package/dist/lib/shared/agent/agent-verify.js +58 -11
- package/dist/lib/shared/agent/git-hook.js +17 -6
- package/dist/lib/shared/agent/review-delivery.js +1 -1
- package/dist/lib/shared/agent/review-mcp.js +8 -2
- package/dist/lib/shared/analysis/ast-semantics.js +2 -2
- package/dist/lib/shared/analysis/scan-cache.js +99 -14
- package/dist/lib/shared/check.js +172 -20
- package/dist/lib/shared/cli-dispatch.js +17 -0
- package/dist/lib/shared/config.js +27 -10
- package/dist/lib/shared/explain.js +1 -1
- package/dist/lib/shared/fix/fix-convergence.js +19 -31
- package/dist/lib/shared/fix/fix-handoff.js +16 -11
- package/dist/lib/shared/fix/fix-imports.js +4 -162
- package/dist/lib/shared/fix/fix-presentation-comments.js +23 -9
- package/dist/lib/shared/fix/fix-presentation-prettier.js +1 -46
- package/dist/lib/shared/fix/fix-presentation-rows.js +45 -1
- package/dist/lib/shared/fix/fix-presentation.js +9 -6
- package/dist/lib/shared/fix/fix-safety.js +32 -1
- package/dist/lib/shared/fix/fix-scope.js +82 -0
- package/dist/lib/shared/fix/fix-terminal.js +28 -1
- package/dist/lib/shared/fix/fix-typecheck.js +8 -4
- package/dist/lib/shared/fix/import-text-edits.js +148 -0
- package/dist/lib/shared/fix/project-file-access.js +17 -26
- package/dist/lib/shared/fix/read-fix-text.js +20 -0
- package/dist/lib/shared/fix.js +241 -104
- package/dist/lib/shared/formats/finding-location.js +4 -1
- package/dist/lib/shared/formats/github.js +3 -3
- package/dist/lib/shared/formats/sarif.js +9 -3
- package/dist/lib/shared/report-fp.js +97 -0
- package/dist/lib/shared/reporting/automatic-fix-signal.js +10 -0
- package/dist/lib/shared/reporting/baseline.js +7 -5
- package/dist/lib/shared/reporting/check-text.js +50 -0
- package/dist/lib/shared/reporting/pillars.js +54 -19
- package/dist/lib/shared/reporting/source-report.js +3 -0
- package/dist/lib/shared/system/atomic-write.js +5 -2
- package/dist/lib/shared/system/glob.js +34 -8
- package/dist/lib/shared/system/sentry.js +222 -0
- package/dist/lib/shared/system/shell-argument.js +5 -1
- package/dist/lib/shared/system/terminal-safe.js +3 -2
- package/dist/lib/shared/system/terminal-style.js +131 -0
- package/dist/lib/shared/workspaces.js +15 -3
- package/docs/assets/cli-workflow.gif +0 -0
- package/integrations/sublime/plugin.py +22 -0
- package/integrations/zed/Cargo.lock +845 -0
- package/integrations/zed/Cargo.toml +11 -0
- package/integrations/zed/extension.toml +11 -0
- package/integrations/zed/src/lib.rs +25 -0
- package/package.json +15 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/commands/loadwise.md +11 -6
- package/plugin/hooks/check-edited-file.mjs +27 -5
- package/polyglot/performance/rules/csharp.yml +34 -5
- package/polyglot/performance/rules/java.yml +24 -1
- package/polyglot/performance/rules/python.yml +74 -2
- package/polyglot/performance/sgconfig.yml +1 -0
- package/polyglot/reliability/rules/csharp.yml +12 -6
- package/polyglot/reliability/rules/go.yml +86 -6
- package/polyglot/reliability/rules/java.yml +22 -1
- package/polyglot/reliability/sgconfig.yml +1 -0
- package/polyglot/security/rules/go.yml +100 -22
- package/polyglot/security/rules/python.yml +74 -6
- package/polyglot/security/rules/rust.yml +65 -0
- package/polyglot/security/sgconfig.yml +1 -0
- package/schema/loadwise.config.schema.json +556 -76
- /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
|
-
|
|
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 [
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
189
|
+
...relationFields(relation.RangeVar),
|
|
162
190
|
}));
|
|
163
191
|
case 'VariableSetStmt': {
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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',
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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',
|
|
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 {
|
|
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 =
|
|
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
|
-
|
|
107
|
-
|
|
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 (
|
|
118
|
-
|
|
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.
|
|
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
|
|
56
|
-
//
|
|
57
|
-
function
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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 {
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
|
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 '
|
|
106
|
-
|
|
107
|
-
|
|
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,
|
|
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 ===
|
|
234
|
+
return [...(checks?.values() ?? [])].some((check) => check.column === operation.columnName && check.validated);
|
|
134
235
|
}
|
|
135
236
|
export { createPostgresRegistry, hasValidatedNotNullCheck, resolveDroppedIndexTable, recordOperation };
|