fix(generate): dedupe unique-constraint columns by default
This commit is contained in:
5 files changed
+188
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { uniqueConstraintColumns } from "@/lib/table/uniqueConstraintColumns";
|
||||
|
||||
describe("uniqueConstraintColumns", () => {
|
||||
it("keeps columns of full single-column unique indexes and primary keys", () => {
|
||||
expect(
|
||||
uniqueConstraintColumns([
|
||||
{ columns: ["id"], is_unique: true, filter: null },
|
||||
{ columns: ["code"], is_unique: true, filter: null },
|
||||
]),
|
||||
).toEqual(new Set(["id", "code"]));
|
||||
});
|
||||
|
||||
it("ignores non-unique indexes", () => {
|
||||
expect(uniqueConstraintColumns([{ columns: ["code"], is_unique: false, filter: null }]).size).toBe(0);
|
||||
});
|
||||
|
||||
it("ignores composite unique indexes", () => {
|
||||
expect(uniqueConstraintColumns([{ columns: ["tenant_id", "code"], is_unique: true, filter: null }]).size).toBe(0);
|
||||
});
|
||||
|
||||
it("ignores partial unique indexes", () => {
|
||||
expect(uniqueConstraintColumns([{ columns: ["code"], is_unique: true, filter: "(deleted_at IS NULL)" }]).size).toBe(0);
|
||||
});
|
||||
|
||||
it("ignores expression keys", () => {
|
||||
expect(uniqueConstraintColumns([{ columns: ["lower(code)"], is_unique: true, filter: null, key_is_expression: [true] }]).size).toBe(0);
|
||||
});
|
||||
|
||||
it("tolerates missing index metadata", () => {
|
||||
expect(uniqueConstraintColumns(undefined).size).toBe(0);
|
||||
expect(uniqueConstraintColumns(null).size).toBe(0);
|
||||
expect(uniqueConstraintColumns([]).size).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Index metadata shape needed to decide whether a column is uniquely
|
||||
* constrained on its own. Mirrors the fields DBX returns from `listIndexes`.
|
||||
*/
|
||||
export interface UniqueConstraintIndex {
|
||||
columns: string[];
|
||||
is_unique: boolean;
|
||||
filter?: string | null;
|
||||
key_is_expression?: boolean[] | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Columns that a full, single-column unique index (or single-column primary
|
||||
* key) constrains, so the data generator knows which columns cannot repeat a
|
||||
* value (#5958).
|
||||
*
|
||||
* Only indexes that guarantee per-column uniqueness qualify:
|
||||
* - Composite indexes are skipped: the table still accepts duplicate values in
|
||||
* each participating column, so deduplicating a single column would be
|
||||
* stricter than the real constraint and could exhaust a small value space.
|
||||
* - Partial indexes (`filter` set) are skipped for the same reason.
|
||||
* - Expression keys are skipped because they have no column name to match.
|
||||
*/
|
||||
export function uniqueConstraintColumns(indexes: readonly UniqueConstraintIndex[] | null | undefined): Set<string> {
|
||||
const columns = new Set<string>();
|
||||
for (const index of indexes ?? []) {
|
||||
if (!index.is_unique || index.filter) continue;
|
||||
if (index.columns.length !== 1) continue;
|
||||
const [column] = index.columns;
|
||||
if (!column || index.key_is_expression?.[0]) continue;
|
||||
columns.add(column);
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
Reference in new issue
Block a user