Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/abstract-sql-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ const compileSchema = (
): SqlModel => {
abstractSqlModel = optimizeSchema(abstractSqlModel, {
createCheckConstraints: false,
createPartialUniqueIndexes: false,
});

let ifNotExistsStr = '';
Expand Down
14 changes: 13 additions & 1 deletion src/abstract-sql-schema-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
AbstractSqlType,
} from './abstract-sql-compiler.js';
import { convertRuleToCheckConstraint } from './schema-optimizations/check-constraint.js';
import { convertRuleToPartialUniqueIndex } from './schema-optimizations/partial-unique-index.js';

export const generateRuleSlug = (
tableName: string,
Expand All @@ -21,7 +22,11 @@ export const generateRuleSlug = (

export const optimizeSchema = (
abstractSqlModel: AbstractSqlModel,
{ createCheckConstraints = true } = {},
{
createCheckConstraints = true,
// TODO: default this to true in the next major
createPartialUniqueIndexes = false,
} = {},
): AbstractSqlModel => {
for (const resourceName of Object.keys(abstractSqlModel.tables)) {
const table = abstractSqlModel.tables[resourceName];
Expand Down Expand Up @@ -95,6 +100,13 @@ export const optimizeSchema = (
return;
}

if (
createPartialUniqueIndexes &&
convertRuleToPartialUniqueIndex(abstractSqlModel, ruleSE, ruleBody)
) {
return;
}

return rule;
})
.filter((v): v is NonNullable<typeof v> => v != null);
Expand Down
25 changes: 1 addition & 24 deletions src/schema-optimizations/check-constraint.ts
Copy link
Copy Markdown
Contributor Author

@thgreasi thgreasi Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted mostly as is from src/abstract-sql-schema-optimizer.ts

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isFromNode,
isSelectQueryNode,
} from '../abstract-sql-compiler.js';
import { convertReferencedFieldsToFields } from './utils.js';
import { generateRuleSlug } from '../abstract-sql-schema-optimizer.js';

const countFroms = (n: AbstractSqlType[]) => {
Expand All @@ -27,30 +28,6 @@ const countFroms = (n: AbstractSqlType[]) => {
return count;
};

const convertReferencedFieldsToFields = (
tableNameOrAlias: string,
nodes: AbstractSqlType[],
) => {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (Array.isArray(node)) {
if (node[0] === 'ReferencedField') {
if (node[1] !== tableNameOrAlias) {
throw new Error(
`Found ReferencedField of unexpected resource '${node[1]}' while converting ReferencedFields of '${tableNameOrAlias}' to Fields`,
);
}
nodes[i] = ['Field', node[2]];
} else {
convertReferencedFieldsToFields(
tableNameOrAlias,
node as AbstractSqlType[],
);
}
}
}
};

export function convertRuleToCheckConstraint(
abstractSqlModel: AbstractSqlModel,
ruleSE: string,
Expand Down
Loading
Loading