mirror of
https://github.com/immich-app/immich.git
synced 2025-12-25 09:14:58 +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) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
|
||||
import { BaseContext } from 'src/sql-tools/contexts/base-context';
|
||||
import { ColumnOptions, TableOptions } from 'src/sql-tools/decorators';
|
||||
import { asKey } from 'src/sql-tools/helpers';
|
||||
import { ColumnOptions } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { TableOptions } from 'src/sql-tools/decorators/table.decorator';
|
||||
import { DatabaseColumn, DatabaseTable, SchemaFromCodeOptions } from 'src/sql-tools/types';
|
||||
|
||||
type TableMetadata = { options: TableOptions; object: Function; methodToColumn: Map<string | symbol, DatabaseColumn> };
|
||||
@@ -59,19 +59,6 @@ export class ProcessorContext extends BaseContext {
|
||||
tableMetadata.methodToColumn.set(propertyName, column);
|
||||
}
|
||||
|
||||
asIndexName(table: string, columns?: string[], where?: string) {
|
||||
const items: string[] = [];
|
||||
for (const columnName of columns ?? []) {
|
||||
items.push(columnName);
|
||||
}
|
||||
|
||||
if (where) {
|
||||
items.push(where);
|
||||
}
|
||||
|
||||
return asKey('IDX_', table, items);
|
||||
}
|
||||
|
||||
warnMissingTable(context: string, object: object, propertyName?: symbol | string) {
|
||||
const label = object.constructor.name + (propertyName ? '.' + String(propertyName) : '');
|
||||
this.warn(context, `Unable to find table (${label})`);
|
||||
|
||||
Reference in New Issue
Block a user