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,3 +1,6 @@
import { DefaultNamingStrategy } from 'src/sql-tools/naming/default.naming';
import { HashNamingStrategy } from 'src/sql-tools/naming/hash.naming';
import { NamingInterface, NamingItem } from 'src/sql-tools/naming/naming.interface';
import {
BaseContextOptions,
DatabaseEnum,
@@ -11,6 +14,26 @@ import {
const asOverrideKey = (type: string, name: string) => `${type}:${name}`;
const isNamingInterface = (strategy: any): strategy is NamingInterface => {
return typeof strategy === 'object' && typeof strategy.getName === 'function';
};
const asNamingStrategy = (strategy: 'hash' | 'default' | NamingInterface): NamingInterface => {
if (isNamingInterface(strategy)) {
return strategy;
}
switch (strategy) {
case 'hash': {
return new HashNamingStrategy();
}
default: {
return new DefaultNamingStrategy();
}
}
};
export class BaseContext {
databaseName: string;
schemaName: string;
@@ -24,10 +47,17 @@ export class BaseContext {
overrides: DatabaseOverride[] = [];
warnings: string[] = [];
private namingStrategy: NamingInterface;
constructor(options: BaseContextOptions) {
this.databaseName = options.databaseName ?? 'postgres';
this.schemaName = options.schemaName ?? 'public';
this.overrideTableName = options.overrideTableName ?? 'migration_overrides';
this.namingStrategy = asNamingStrategy(options.namingStrategy ?? 'hash');
}
getNameFor(item: NamingItem) {
return this.namingStrategy.getName(item);
}
getTableByName(name: string) {