mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor: sql-tools (#19717)
This commit is contained in:
parent
484529e61e
commit
6044663e26
160 changed files with 1120 additions and 1186 deletions
|
|
@ -1,15 +1,6 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import { ColumnValue } from 'src/sql-tools/from-code/decorators/column.decorator';
|
||||
import { SchemaBuilder } from 'src/sql-tools/from-code/processors/type';
|
||||
import {
|
||||
Comparer,
|
||||
DatabaseColumn,
|
||||
DiffOptions,
|
||||
SchemaDiff,
|
||||
TriggerAction,
|
||||
TriggerScope,
|
||||
TriggerTiming,
|
||||
} from 'src/sql-tools/types';
|
||||
import { ColumnValue } from 'src/sql-tools/decorators/column.decorator';
|
||||
import { Comparer, DatabaseColumn, IgnoreOptions, SchemaDiff } from 'src/sql-tools/types';
|
||||
|
||||
export const asMetadataKey = (name: string) => `sql-tools:${name}`;
|
||||
|
||||
|
|
@ -27,46 +18,6 @@ export const asOptions = <T extends { name?: string }>(options: string | T): T =
|
|||
};
|
||||
|
||||
export const sha1 = (value: string) => createHash('sha1').update(value).digest('hex');
|
||||
export const hasMask = (input: number, mask: number) => (input & mask) === mask;
|
||||
|
||||
export const parseTriggerType = (type: number) => {
|
||||
// eslint-disable-next-line unicorn/prefer-math-trunc
|
||||
const scope: TriggerScope = hasMask(type, 1 << 0) ? 'row' : 'statement';
|
||||
|
||||
let timing: TriggerTiming = 'after';
|
||||
const timingMasks: Array<{ mask: number; value: TriggerTiming }> = [
|
||||
{ mask: 1 << 1, value: 'before' },
|
||||
{ mask: 1 << 6, value: 'instead of' },
|
||||
];
|
||||
|
||||
for (const { mask, value } of timingMasks) {
|
||||
if (hasMask(type, mask)) {
|
||||
timing = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const actions: TriggerAction[] = [];
|
||||
const actionMasks: Array<{ mask: number; value: TriggerAction }> = [
|
||||
{ mask: 1 << 2, value: 'insert' },
|
||||
{ mask: 1 << 3, value: 'delete' },
|
||||
{ mask: 1 << 4, value: 'update' },
|
||||
{ mask: 1 << 5, value: 'truncate' },
|
||||
];
|
||||
|
||||
for (const { mask, value } of actionMasks) {
|
||||
if (hasMask(type, mask)) {
|
||||
actions.push(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (actions.length === 0) {
|
||||
throw new Error(`Unable to parse trigger type ${type}`);
|
||||
}
|
||||
|
||||
return { actions, timing, scope };
|
||||
};
|
||||
|
||||
export const fromColumnValue = (columnValue?: ColumnValue) => {
|
||||
if (columnValue === undefined) {
|
||||
|
|
@ -108,7 +59,7 @@ export const haveEqualColumns = (sourceColumns?: string[], targetColumns?: strin
|
|||
export const compare = <T extends { name: string; synchronize: boolean }>(
|
||||
sources: T[],
|
||||
targets: T[],
|
||||
options: DiffOptions | undefined,
|
||||
options: IgnoreOptions | undefined,
|
||||
comparer: Comparer<T>,
|
||||
) => {
|
||||
options = options || {};
|
||||
|
|
@ -144,7 +95,7 @@ export const compare = <T extends { name: string; synchronize: boolean }>(
|
|||
const isIgnored = (
|
||||
source: { synchronize?: boolean } | undefined,
|
||||
target: { synchronize?: boolean } | undefined,
|
||||
options: DiffOptions,
|
||||
options: IgnoreOptions,
|
||||
) => {
|
||||
return (options.ignoreExtra && !source) || (options.ignoreMissing && !target);
|
||||
};
|
||||
|
|
@ -214,20 +165,3 @@ export const asColumnComment = (tableName: string, columnName: string, comment:
|
|||
export const asColumnList = (columns: string[]) => columns.map((column) => `"${column}"`).join(', ');
|
||||
|
||||
export const asForeignKeyConstraintName = (table: string, columns: string[]) => asKey('FK_', table, [...columns]);
|
||||
|
||||
export const 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);
|
||||
};
|
||||
|
||||
export const addWarning = (builder: SchemaBuilder, context: string, message: string) => {
|
||||
builder.warnings.push(`[${context}] ${message}`);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue