Skip to content

Commit 366c185

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/issue-31696-pr3-skills-inbound-hook
2 parents 775b07f + 48d6b47 commit 366c185

3 files changed

Lines changed: 510 additions & 11 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@objectstack/driver-sql': patch
3+
---
4+
5+
fix(driver-sql): the differ's widening arm keeps, or safely retires, a narrower unique an 18.0 database built in the NULL-safe form, and keeps one a hash shadow carries or whose gained column is not yet physical, instead of offering any of them as an orphan drop (#32164)
6+
7+
Clause-②: no
8+
- The widening arm (the rule that retires a narrower unique key superseded by a declared global key that gained columns), on a narrower an 18.0 database built with its organization key part in the NULL-safe form (`COALESCE(organization_id, '__global__')`, under the same generated name): FROM an orphan, `drop_index` categorized `destructive` TO the widening arm's answer. On a certified table (ADR-0131 D1: the tenant column holds no NULL), the narrower is kept until a partial guard on its columns stands, then offered as the one `replace_unique_index`, categorized `safe`. On a table that is not certified, it is kept and nothing is offered, because the COALESCE form also keeps NULL-organization rows to one, which neither the wider key nor the guard does.
9+
- A plan read before the sync that adds a gained column (`os migrate plan` over an upgraded database): FROM the narrower an orphan, `drop_index` categorized `destructive` TO kept, nothing offered.
10+
- A narrower carried by a #11627 hash shadow (MySQL, MariaDB: a key over a long TEXT column), whose resolved key is exactly the superseded columns, plain or with the organization part NULL-safe: FROM an orphan, `drop_index` categorized `destructive` TO kept, nothing offered, certified or not and with or without the guard. `replace_unique_index` drops the legacy name and retires no shadow, so this arm never offers it.
11+
- A hash-shadow carrier under the superseded key's generated name whose key could not be read (its generation expression did not resolve): FROM an orphan, `drop_index` categorized `destructive` TO kept, nothing offered, certified or not and with or without the guard; read before the sync that adds a gained column, it is kept and nothing is offered: the wider key over a column not yet physical is not expected yet. What it hashes is unknown, so it is never dropped on a guess.
12+
13+
Metadata-protocol: none
14+
15+
<!-- adr-0087: not-required (no-migration-prescription) No key of any authorable schema is added, removed, renamed or re-shaped. The change is in the SQL driver's schema-drift plan: which physical index it keeps or offers to replace. No stored metadata row is rewritten, so there is nothing for `objectstack migrate meta` to rewrite. -->
16+
17+
Part of #32164 (ADR-0150 D8 R1). It lands ahead of the grant key's widening, where the restored 18.0 store's `uniq_sys_user_permission_set_ac248331` is the first narrower in this form. The grant key itself is still `unique: 'organization'`, which the widening arm does not take, so this change alone leaves the grant table's plan as it is. On `main`, 17 first-party declarations are unnamed `unique: true` / `'global'` keys of 2 to 8 fields, the ones this arm reads; it acts on one only where a physical unique stands under the generated name of a narrower subsequence of its fields, which is the activation ledger's `(metadata_type, name)` on an upgraded database (respelled to `(metadata_type, package_id, name)` by ADR-0150). Any other declared global key that gained columns reaches the new answers only in the four shapes above. In each, a destructive offer becomes a kept index, or a safe replace once a guard stands. No answer is added that a sync would apply to drop a key that is still needed.
18+
19+
Unchanged: a plain narrower is matched as before. A narrower whose key parts (read through a hash shadow's resolved key where one carries it) are not the superseded columns is still the orphan arm's. Pinned at the differ with pure functions: no live MySQL or MariaDB has run this arm, and the live suites run on `main` only.

‎packages/drivers/driver-sql/src/schema-drift.ts‎

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,11 +2453,15 @@ const WIDENED_UNIQUE_MAX_KEY_PARTS = 8;
24532453
* - the declared index is UNNAMED (a named index keeps its name, so there is
24542454
* no second name to retire) and `unique: true`/`'global'` — never
24552455
* `'organization'`, whose materialized key is the tenant arms' to judge;
2456-
* - every listed column is physical;
2456+
* - every column of the superseded key is physical (a GAINED column not yet
2457+
* physical keeps the index, below — #32164);
24572458
* - the superseded index's columns are an in-order PROPER subsequence of the
24582459
* listed ones, and its name is the one {@link buildIndexName} gives that
24592460
* subsequence (`diffManagedIndexes` then also requires it to be a plain
2460-
* UNIQUE on exactly those columns);
2461+
* UNIQUE on exactly those columns — or, #32164, the same with the tenant
2462+
* column's key part in the NULL-safe organization form an earlier release
2463+
* built, which it offers to retire only on a certified table and otherwise
2464+
* keeps, see {@link isNullSafeTenantNarrower});
24612465
* - no index metadata asks for today has that name — field-level uniques
24622466
* included, so a live single-column unique is never read as superseded;
24632467
* - an index by that name exists physically (`args.physical`), so the arm
@@ -2494,12 +2498,19 @@ function widenedUniqueReplacements(args: {
24942498
? idx.fields.filter((f): f is string => typeof f === 'string' && f.length > 0)
24952499
: [];
24962500
if (listed.length < 2 || listed.length > WIDENED_UNIQUE_MAX_KEY_PARTS) continue;
2497-
if (!listed.every((c) => physicalColumns.has(c))) continue;
2501+
// [#32164] A gained column not yet physical (the plan read before the sync
2502+
// that adds it — `os migrate plan` over an upgraded database) does not
2503+
// disqualify the arm: the narrower index is then plainly still the only
2504+
// rule, so it is KEPT below rather than left to the orphan arm, which would
2505+
// offer to drop it. The narrower key's own columns must be physical.
2506+
const absent = new Set(listed.filter((c) => !physicalColumns.has(c)));
2507+
if (absent.size === listed.length) continue;
24982508
const replacement = normalizeDeclaredIndex(table, idx, args.tenantField);
24992509
if (!replacement) continue;
25002510
const full = (1 << listed.length) - 1;
25012511
for (let mask = 1; mask < full; mask++) {
25022512
const narrower = listed.filter((_, i) => (mask & (1 << i)) !== 0);
2513+
if (narrower.some((c) => absent.has(c))) continue;
25032514
const legacyName = buildIndexName(table, narrower, true);
25042515
if (current.has(legacyName) || !physicalNames.has(legacyName)) continue;
25052516
const gained = listed.filter((c) => !narrower.includes(c));
@@ -2510,7 +2521,11 @@ function widenedUniqueReplacements(args: {
25102521
// exactly `narrower`, scoped by a WHERE, already stands beside it (the
25112522
// activation ledger's runtime-built package-less index). Until then it is
25122523
// KEPT: accounted for, so the orphan arm never offers to drop it either.
2513-
const kept = nullable.length > 0 && !hasPartialUniqueGuard(args.physical ?? [], narrower);
2524+
// A gained column the table does not have yet keeps it the same way: the
2525+
// wider index cannot exist before its column does.
2526+
const kept =
2527+
gained.some((c) => absent.has(c)) ||
2528+
(nullable.length > 0 && !hasPartialUniqueGuard(args.physical ?? [], narrower));
25142529
out.push({
25152530
column: listed.find((c) => !narrower.includes(c))!,
25162531
legacyColumns: narrower,
@@ -2543,6 +2558,35 @@ function hasPartialUniqueGuard(physical: ReadonlyArray<PhysicalIndex>, columns:
25432558
});
25442559
}
25452560

2561+
/** The same column identities, in the same key order. */
2562+
function sameColumns(a: readonly string[], b: readonly string[]): boolean {
2563+
return a.length === b.length && a.every((c, i) => c === b[i]);
2564+
}
2565+
2566+
/**
2567+
* [#32164] Is this physical index a narrower key whose ONLY non-plain key part
2568+
* is the tenant column in the NULL-safe organization form (ADR-0120 D3,
2569+
* `COALESCE(<tenantField>, '__global__')`)? The shape an earlier release gave
2570+
* every organization-scoped unique, and so the shape of a narrower key the
2571+
* widening arm supersedes when the declaration it retires was
2572+
* `unique: 'organization'` (`sys_user_permission_set`'s
2573+
* `(user_id, permission_set, organization_id)` before it gained
2574+
* `permission_set_package`). Exactly one expression key part, attributed to the
2575+
* tenant column and to nothing else; the caller has already checked the column
2576+
* identities and the index's plainness otherwise (unique, not partial, no hash
2577+
* shadow).
2578+
*/
2579+
function isNullSafeTenantNarrower(p: PhysicalIndex, tenantField: string | null | undefined): boolean {
2580+
if (!tenantField) return false;
2581+
const nullSafe = p.nullSafeColumns ?? [];
2582+
return (
2583+
nullSafe.length === 1 &&
2584+
nullSafe[0] === tenantField &&
2585+
(p.expressions?.length ?? 0) === 1 &&
2586+
p.columns.includes(tenantField)
2587+
);
2588+
}
2589+
25462590
/** The tenant arms of {@link legacyUniqueReplacements}: a unique now scoped per organization. */
25472591
function tenantScopedLegacyReplacements(args: {
25482592
table: string;
@@ -2812,31 +2856,76 @@ export function diffManagedIndexes(args: {
28122856
const p = byName.get(n);
28132857
if (!p || p.primary || isRuntimeManagedIndex(p, runtimeCreated, tenantField)) return false;
28142858
if (!p.unique || p.partial === true) return false;
2815-
if ((p.expressions?.length ?? 0) > 0 || (p.nullSafeColumns?.length ?? 0) > 0) return false;
28162859
// Commit cd1348802: nor is a hash-shadow carrier. Its stored key is one generated
28172860
// column, so the identity comparison below already excludes it — stated
28182861
// outright because the exclusion must survive that comparison changing,
28192862
// and because `replace_unique_index` DROPS the legacy name.
28202863
if (isHashShadowCarrier(p)) return false;
2821-
return (
2822-
p.columns.length === l.legacyColumns.length &&
2823-
p.columns.every((c, i) => c === l.legacyColumns[i])
2824-
);
2864+
if (!sameColumns(p.columns, l.legacyColumns)) return false;
2865+
if ((p.expressions?.length ?? 0) === 0 && (p.nullSafeColumns?.length ?? 0) === 0) return true;
2866+
// [#32164] The widening arm alone also names the narrower key an earlier
2867+
// release built with its organization key part in the NULL-safe form
2868+
// (ADR-0120 D3: `COALESCE(<tenantField>, '__global__')`, every 18.0
2869+
// organization unique). Same columns, one expression key part, and that
2870+
// part is the tenant column's — nothing else the plainness rule refuses.
2871+
return l.widenedBy !== undefined && isNullSafeTenantNarrower(p, tenantField);
28252872
});
2873+
// [#32164] A narrower key a #11627 hash shadow carries (MySQL, MariaDB: the
2874+
// shape a key over a long TEXT column takes) is excluded from `present`
2875+
// above, because `replace_unique_index` drops the legacy name and nothing
2876+
// here retires a shadow with it. On the widening arm it is still the
2877+
// narrower key the declaration superseded — before the respelling, the same
2878+
// physical index was the declared one and matched by name — so it is KEPT:
2879+
// accounted for, nothing offered, never left to the orphan arm, which would
2880+
// offer to drop it (destructive). The disposition the uncertified NULL-safe
2881+
// case below gets. A carrier whose resolved key is exactly the narrower
2882+
// columns (any NULL-safe part being the tenant column's) is claimed. So is a
2883+
// carrier whose generation expression could not be read (no `shadowKey`):
2884+
// it sits under the narrower key's generated name and we do not know what
2885+
// it hashes, so it is kept, not guessed at — the orphan arm below does NOT
2886+
// skip an unresolved carrier, and would offer it as a destructive
2887+
// `drop_index` (contract review must-fix 1 on #32356). A carrier whose
2888+
// resolved key is some other set of columns is still the orphan arm's.
2889+
if (l.widenedBy !== undefined) {
2890+
for (const n of l.legacyNames) {
2891+
const p = byName.get(n);
2892+
if (!p || !p.unique || !isHashShadowCarrier(p)) continue;
2893+
if (!p.shadowKey) {
2894+
explained.add(n);
2895+
continue;
2896+
}
2897+
if (p.partial === true) continue;
2898+
const key = enforcedIndexKey(p);
2899+
if (!sameColumns(key.columns, l.legacyColumns)) continue;
2900+
if (!(key.nullSafeColumns ?? []).every((c) => c === tenantField)) continue;
2901+
explained.add(n);
2902+
}
2903+
}
28262904
if (present.length === 0) continue;
28272905
for (const n of present) explained.add(n);
28282906
// Kept, not offered: the widening arm found the narrower index still the
28292907
// only rule over rows a nullable gained column exempts (see
28302908
// `widenedUniqueReplacements`). It is accounted for — never an orphan to
28312909
// drop — and the plan says nothing about it until the guard stands.
28322910
if (l.kept) continue;
2911+
// [#32164] A NULL-safe narrower is the same constraint as the plain one
2912+
// only where the tenant column holds no NULL (`tenantKeyCertified`,
2913+
// ADR-0131 D1). Elsewhere it also keeps NULL-organization rows to one per
2914+
// key, which neither the wider key nor the partial guard does (both are
2915+
// NULL-distinct on the organization), so retiring it would admit rows it
2916+
// refuses. It is KEPT there too: accounted for, never offered, never an
2917+
// orphan — the ownership ceremony that certifies the table is what lets
2918+
// the next plan offer the replace.
2919+
const nullSafeLegacy = present.some((n) => (byName.get(n)?.nullSafeColumns?.length ?? 0) > 0);
2920+
if (nullSafeLegacy && tenantKeyCertified !== true) continue;
2921+
const legacyNullSafe = nullSafeLegacy && tenantField ? [tenantField] : undefined;
28332922
out.push({
28342923
kind: 'index_mismatch',
28352924
remoteName: table,
28362925
table,
28372926
column: l.column,
28382927
expected: indexSignature(l.replacement.columns, true, l.replacement.nullSafeColumns),
2839-
actual: indexSignature(l.legacyColumns, true),
2928+
actual: indexSignature(l.legacyColumns, true, legacyNullSafe),
28402929
severity: 'warning',
28412930
category: 'safe',
28422931
op: {
@@ -2850,7 +2939,7 @@ export function diffManagedIndexes(args: {
28502939
},
28512940
message: l.widenedBy
28522941
? `${table}.${l.legacyColumns.join('+')}: a UNIQUE index (${present.join(', ')}) on ` +
2853-
`${indexSignature(l.legacyColumns, true)} is superseded by the declared ` +
2942+
`${indexSignature(l.legacyColumns, true, legacyNullSafe)} is superseded by the declared ` +
28542943
`${indexSignature(l.replacement.columns, true, l.replacement.nullSafeColumns)}, which adds ` +
28552944
`${l.widenedBy.join(', ')} to the key and admits rows the old index refuses. Replacing it is a pure ` +
28562945
`relaxation: run "os migrate apply".` +

0 commit comments

Comments
 (0)