mirror of
https://github.com/immich-app/immich.git
synced 2025-12-17 17:23:20 +03:00
feat: naming strategy (#19848)
* feat: naming strategy * feat: detect renames
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user