feat: naming strategy (#19848)

* feat: naming strategy

* feat: detect renames
This commit is contained in:
Jason Rasmussen
2025-07-11 11:35:10 -04:00
committed by GitHub
parent 1d19d308e2
commit 9e48ae3052
35 changed files with 517 additions and 127 deletions

View File

@@ -1,4 +1,3 @@
import { asKey } from 'src/sql-tools/helpers';
import { ConstraintType, Processor } from 'src/sql-tools/types';
export const processCheckConstraints: Processor = (ctx, items) => {
@@ -15,12 +14,10 @@ export const processCheckConstraints: Processor = (ctx, items) => {
table.constraints.push({
type: ConstraintType.CHECK,
name: options.name || asCheckConstraintName(tableName, options.expression),
name: options.name || ctx.getNameFor({ type: 'check', tableName, expression: options.expression }),
tableName,
expression: options.expression,
synchronize: options.synchronize ?? true,
});
}
};
const asCheckConstraintName = (table: string, expression: string) => asKey('CHK_', table, [expression]);

View File

@@ -13,7 +13,7 @@ export const processColumns: Processor = (ctx, items) => {
continue;
}
const columnName = options.name ?? String(propertyName);
const columnName = options.name ?? ctx.getNameFor({ type: 'column', name: String(propertyName) });
const existingColumn = table.columns.find((column) => column.name === columnName);
if (existingColumn) {
// TODO log warnings if column name is not unique

View File

@@ -1,10 +1,9 @@
import { asSnakeCase } from 'src/sql-tools/helpers';
import { Processor } from 'src/sql-tools/types';
export const processDatabases: Processor = (ctx, items) => {
for (const {
item: { object, options },
} of items.filter((item) => item.type === 'database')) {
ctx.databaseName = options.name || asSnakeCase(object.name);
ctx.databaseName = options.name || ctx.getNameFor({ type: 'database', name: object.name });
}
};

View File

@@ -1,4 +1,3 @@
import { asForeignKeyConstraintName, asKey } from 'src/sql-tools/helpers';
import { ActionType, ConstraintType, Processor } from 'src/sql-tools/types';
export const processForeignKeyColumns: Processor = (ctx, items) => {
@@ -31,15 +30,24 @@ export const processForeignKeyColumns: Processor = (ctx, items) => {
column.type = referenceColumns[0].type;
}
const referenceTableName = referenceTable.name;
const referenceColumnNames = referenceColumns.map((column) => column.name);
const name = options.constraintName || asForeignKeyConstraintName(table.name, columnNames);
const name =
options.constraintName ||
ctx.getNameFor({
type: 'foreignKey',
tableName: table.name,
columnNames,
referenceTableName,
referenceColumnNames,
});
table.constraints.push({
name,
tableName: table.name,
columnNames,
type: ConstraintType.FOREIGN_KEY,
referenceTableName: referenceTable.name,
referenceTableName,
referenceColumnNames,
onUpdate: options.onUpdate as ActionType,
onDelete: options.onDelete as ActionType,
@@ -48,7 +56,7 @@ export const processForeignKeyColumns: Processor = (ctx, items) => {
if (options.unique || options.uniqueConstraintName) {
table.constraints.push({
name: options.uniqueConstraintName || asRelationKeyConstraintName(table.name, columnNames),
name: options.uniqueConstraintName || ctx.getNameFor({ type: 'unique', tableName: table.name, columnNames }),
tableName: table.name,
columnNames,
type: ConstraintType.UNIQUE,
@@ -57,5 +65,3 @@ export const processForeignKeyColumns: Processor = (ctx, items) => {
}
}
};
const asRelationKeyConstraintName = (table: string, columns: string[]) => asKey('REL_', table, columns);

View File

@@ -1,4 +1,3 @@
import { asForeignKeyConstraintName } from 'src/sql-tools/helpers';
import { ActionType, ConstraintType, Processor } from 'src/sql-tools/types';
export const processForeignKeyConstraints: Processor = (ctx, items) => {
@@ -46,18 +45,27 @@ export const processForeignKeyConstraints: Processor = (ctx, items) => {
continue;
}
const referenceColumns =
const referenceTableName = referenceTable.name;
const referenceColumnNames =
options.referenceColumns || referenceTable.columns.filter(({ primary }) => primary).map(({ name }) => name);
const name = options.name || asForeignKeyConstraintName(table.name, options.columns);
const name =
options.name ||
ctx.getNameFor({
type: 'foreignKey',
tableName: table.name,
columnNames: options.columns,
referenceTableName,
referenceColumnNames,
});
table.constraints.push({
type: ConstraintType.FOREIGN_KEY,
name,
tableName: table.name,
columnNames: options.columns,
referenceTableName: referenceTable.name,
referenceColumnNames: referenceColumns,
referenceTableName,
referenceColumnNames,
onUpdate: options.onUpdate as ActionType,
onDelete: options.onDelete as ActionType,
synchronize: options.synchronize ?? true,
@@ -68,8 +76,15 @@ export const processForeignKeyConstraints: Processor = (ctx, items) => {
}
if (options.index || options.indexName || ctx.options.createForeignKeyIndexes) {
const indexName =
options.indexName ||
ctx.getNameFor({
type: 'index',
tableName: table.name,
columnNames: options.columns,
});
table.indexes.push({
name: options.indexName || ctx.asIndexName(table.name, options.columns),
name: indexName,
tableName: table.name,
columnNames: options.columns,
unique: false,

View File

@@ -10,8 +10,17 @@ export const processIndexes: Processor = (ctx, items) => {
continue;
}
const indexName =
options.name ||
ctx.getNameFor({
type: 'index',
tableName: table.name,
columnNames: options.columns,
where: options.where,
});
table.indexes.push({
name: options.name || ctx.asIndexName(table.name, options.columns, options.where),
name: indexName,
tableName: table.name,
unique: options.unique ?? false,
expression: options.expression,
@@ -50,7 +59,13 @@ export const processIndexes: Processor = (ctx, items) => {
continue;
}
const indexName = options.indexName || ctx.asIndexName(table.name, [column.name]);
const indexName =
options.indexName ||
ctx.getNameFor({
type: 'index',
tableName: table.name,
columnNames: [column.name],
});
const isIndexPresent = table.indexes.some((index) => index.name === indexName);
if (isIndexPresent) {

View File

@@ -1,4 +1,3 @@
import { asKey } from 'src/sql-tools/helpers';
import { ConstraintType, Processor } from 'src/sql-tools/types';
export const processPrimaryKeyConstraints: Processor = (ctx) => {
@@ -15,7 +14,13 @@ export const processPrimaryKeyConstraints: Processor = (ctx) => {
const tableMetadata = ctx.getTableMetadata(table);
table.constraints.push({
type: ConstraintType.PRIMARY_KEY,
name: tableMetadata.options.primaryConstraintName || asPrimaryKeyConstraintName(table.name, columnNames),
name:
tableMetadata.options.primaryConstraintName ||
ctx.getNameFor({
type: 'primaryKey',
tableName: table.name,
columnNames,
}),
tableName: table.name,
columnNames,
synchronize: tableMetadata.options.synchronize ?? true,
@@ -23,5 +28,3 @@ export const processPrimaryKeyConstraints: Processor = (ctx) => {
}
}
};
const asPrimaryKeyConstraintName = (table: string, columns: string[]) => asKey('PK_', table, columns);

View File

@@ -1,4 +1,3 @@
import { asSnakeCase } from 'src/sql-tools/helpers';
import { Processor } from 'src/sql-tools/types';
export const processTables: Processor = (ctx, items) => {
@@ -14,7 +13,7 @@ export const processTables: Processor = (ctx, items) => {
ctx.addTable(
{
name: options.name || asSnakeCase(object.name),
name: options.name || ctx.getNameFor({ type: 'table', name: object.name }),
columns: [],
constraints: [],
indexes: [],

View File

@@ -1,5 +1,3 @@
import { TriggerOptions } from 'src/sql-tools/decorators/trigger.decorator';
import { asKey } from 'src/sql-tools/helpers';
import { Processor } from 'src/sql-tools/types';
export const processTriggers: Processor = (ctx, items) => {
@@ -12,8 +10,19 @@ export const processTriggers: Processor = (ctx, items) => {
continue;
}
const triggerName =
options.name ||
ctx.getNameFor({
type: 'trigger',
tableName: table.name,
actions: options.actions,
scope: options.scope,
timing: options.timing,
functionName: options.functionName,
});
table.triggers.push({
name: options.name || asTriggerName(table.name, options),
name: triggerName,
tableName: table.name,
timing: options.timing,
actions: options.actions,
@@ -26,6 +35,3 @@ export const processTriggers: Processor = (ctx, items) => {
});
}
};
const asTriggerName = (table: string, trigger: TriggerOptions) =>
asKey('TR_', table, [...trigger.actions, trigger.scope, trigger.timing, trigger.functionName]);

View File

@@ -1,4 +1,3 @@
import { asKey } from 'src/sql-tools/helpers';
import { ConstraintType, Processor } from 'src/sql-tools/types';
export const processUniqueConstraints: Processor = (ctx, items) => {
@@ -16,7 +15,7 @@ export const processUniqueConstraints: Processor = (ctx, items) => {
table.constraints.push({
type: ConstraintType.UNIQUE,
name: options.name || asUniqueConstraintName(tableName, columnNames),
name: options.name || ctx.getNameFor({ type: 'unique', tableName, columnNames }),
tableName,
columnNames,
synchronize: options.synchronize ?? true,
@@ -41,9 +40,17 @@ export const processUniqueConstraints: Processor = (ctx, items) => {
}
if (type === 'column' && !options.primary && (options.unique || options.uniqueConstraintName)) {
const uniqueConstraintName =
options.uniqueConstraintName ||
ctx.getNameFor({
type: 'unique',
tableName: table.name,
columnNames: [column.name],
});
table.constraints.push({
type: ConstraintType.UNIQUE,
name: options.uniqueConstraintName || asUniqueConstraintName(table.name, [column.name]),
name: uniqueConstraintName,
tableName: table.name,
columnNames: [column.name],
synchronize: options.synchronize ?? true,
@@ -51,5 +58,3 @@ export const processUniqueConstraints: Processor = (ctx, items) => {
}
}
};
const asUniqueConstraintName = (table: string, columns: string[]) => asKey('UQ_', table, columns);