mirror of
https://github.com/immich-app/immich.git
synced 2026-02-21 16:20:34 +09:00
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Docker / pre-job (push) Has been cancelled
Docker / Re-Tag ML () (push) Has been cancelled
Docker / Re-Tag ML (-armnn) (push) Has been cancelled
Docker / Re-Tag ML (-cuda) (push) Has been cancelled
Docker / Re-Tag ML (-openvino) (push) Has been cancelled
Docker / Re-Tag ML (-rknn) (push) Has been cancelled
Docker / Re-Tag ML (-rocm) (push) Has been cancelled
Docker / Re-Tag Server () (push) Has been cancelled
Docker / Build and Push ML (armnn, linux/arm64, -armnn) (push) Has been cancelled
Docker / Build and Push ML (cpu) (push) Has been cancelled
Docker / Build and Push ML (cuda, linux/amd64, -cuda) (push) Has been cancelled
Docker / Build and Push ML (openvino, linux/amd64, -openvino) (push) Has been cancelled
Docker / Build and Push ML (rknn, linux/arm64, -rknn) (push) Has been cancelled
Docker / Build and Push ML (rocm, linux/amd64, {"linux/amd64": "pokedex-giant"}, -rocm) (push) Has been cancelled
Docker / Build and Push Server (push) Has been cancelled
Docker / Docker Build & Push Server Success (push) Has been cancelled
Docker / Docker Build & Push ML Success (push) Has been cancelled
Docs build / pre-job (push) Has been cancelled
Docs build / Docs Build (push) Has been cancelled
Zizmor / Zizmor (push) Has been cancelled
Manage release PR / bump (push) Has been cancelled
Static Code Analysis / pre-job (push) Has been cancelled
Static Code Analysis / Run Dart Code Analysis (push) Has been cancelled
Test / pre-job (push) Has been cancelled
Test / Test & Lint Server (push) Has been cancelled
Test / Unit Test CLI (push) Has been cancelled
Test / Unit Test CLI (Windows) (push) Has been cancelled
Test / Lint Web (push) Has been cancelled
Test / Test Web (push) Has been cancelled
Test / Test i18n (push) Has been cancelled
Test / End-to-End Lint (push) Has been cancelled
Test / Medium Tests (Server) (push) Has been cancelled
Test / End-to-End Tests (Server & CLI) (ubuntu-24.04-arm) (push) Has been cancelled
Test / End-to-End Tests (Server & CLI) (ubuntu-latest) (push) Has been cancelled
Test / End-to-End Tests (Web) (ubuntu-24.04-arm) (push) Has been cancelled
Test / End-to-End Tests (Web) (ubuntu-latest) (push) Has been cancelled
Test / End-to-End Tests Success (push) Has been cancelled
Test / Unit Test Mobile (push) Has been cancelled
Test / Unit Test ML (push) Has been cancelled
Test / .github Files Formatting (push) Has been cancelled
Test / ShellCheck (push) Has been cancelled
Test / OpenAPI Clients (push) Has been cancelled
Test / SQL Schema Checks (push) Has been cancelled
166 lines
5.0 KiB
TypeScript
166 lines
5.0 KiB
TypeScript
import { asRenameKey, haveEqualColumns } from 'src/sql-tools/helpers';
|
|
import {
|
|
CompareFunction,
|
|
Comparer,
|
|
ConstraintType,
|
|
DatabaseCheckConstraint,
|
|
DatabaseConstraint,
|
|
DatabaseForeignKeyConstraint,
|
|
DatabasePrimaryKeyConstraint,
|
|
DatabaseUniqueConstraint,
|
|
Reason,
|
|
SchemaDiff,
|
|
} from 'src/sql-tools/types';
|
|
|
|
export const compareConstraints = (): Comparer<DatabaseConstraint> => ({
|
|
getRenameKey: (constraint) => {
|
|
switch (constraint.type) {
|
|
case ConstraintType.PRIMARY_KEY:
|
|
case ConstraintType.UNIQUE: {
|
|
return asRenameKey([constraint.type, constraint.tableName, ...constraint.columnNames.toSorted()]);
|
|
}
|
|
|
|
case ConstraintType.FOREIGN_KEY: {
|
|
return asRenameKey([
|
|
constraint.type,
|
|
constraint.tableName,
|
|
...constraint.columnNames.toSorted(),
|
|
constraint.referenceTableName,
|
|
...constraint.referenceColumnNames.toSorted(),
|
|
]);
|
|
}
|
|
|
|
case ConstraintType.CHECK: {
|
|
const expression = constraint.expression.replaceAll('(', '').replaceAll(')', '');
|
|
return asRenameKey([constraint.type, constraint.tableName, expression]);
|
|
}
|
|
}
|
|
},
|
|
onRename: (source, target) => [
|
|
{
|
|
type: 'ConstraintRename',
|
|
tableName: target.tableName,
|
|
oldName: target.name,
|
|
newName: source.name,
|
|
reason: Reason.Rename,
|
|
},
|
|
],
|
|
onMissing: (source) => [
|
|
{
|
|
type: 'ConstraintAdd',
|
|
constraint: source,
|
|
reason: Reason.MissingInTarget,
|
|
},
|
|
],
|
|
onExtra: (target) => [
|
|
{
|
|
type: 'ConstraintDrop',
|
|
tableName: target.tableName,
|
|
constraintName: target.name,
|
|
reason: Reason.MissingInSource,
|
|
},
|
|
],
|
|
onCompare: (source, target) => {
|
|
switch (source.type) {
|
|
case ConstraintType.PRIMARY_KEY: {
|
|
return comparePrimaryKeyConstraint(source, target as DatabasePrimaryKeyConstraint);
|
|
}
|
|
|
|
case ConstraintType.FOREIGN_KEY: {
|
|
return compareForeignKeyConstraint(source, target as DatabaseForeignKeyConstraint);
|
|
}
|
|
|
|
case ConstraintType.UNIQUE: {
|
|
return compareUniqueConstraint(source, target as DatabaseUniqueConstraint);
|
|
}
|
|
|
|
case ConstraintType.CHECK: {
|
|
return compareCheckConstraint(source, target as DatabaseCheckConstraint);
|
|
}
|
|
|
|
default: {
|
|
return [];
|
|
}
|
|
}
|
|
},
|
|
});
|
|
|
|
const comparePrimaryKeyConstraint: CompareFunction<DatabasePrimaryKeyConstraint> = (source, target) => {
|
|
if (!haveEqualColumns(source.columnNames, target.columnNames)) {
|
|
return dropAndRecreateConstraint(
|
|
source,
|
|
target,
|
|
`Primary key columns are different: (${source.columnNames} vs ${target.columnNames})`,
|
|
);
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
const compareForeignKeyConstraint: CompareFunction<DatabaseForeignKeyConstraint> = (source, target) => {
|
|
let reason = '';
|
|
|
|
const sourceDeleteAction = source.onDelete ?? 'NO ACTION';
|
|
const targetDeleteAction = target.onDelete ?? 'NO ACTION';
|
|
|
|
const sourceUpdateAction = source.onUpdate ?? 'NO ACTION';
|
|
const targetUpdateAction = target.onUpdate ?? 'NO ACTION';
|
|
|
|
if (!haveEqualColumns(source.columnNames, target.columnNames)) {
|
|
reason = `columns are different (${source.columnNames} vs ${target.columnNames})`;
|
|
} else if (!haveEqualColumns(source.referenceColumnNames, target.referenceColumnNames)) {
|
|
reason = `reference columns are different (${source.referenceColumnNames} vs ${target.referenceColumnNames})`;
|
|
} else if (source.referenceTableName !== target.referenceTableName) {
|
|
reason = `reference table is different (${source.referenceTableName} vs ${target.referenceTableName})`;
|
|
} else if (sourceDeleteAction !== targetDeleteAction) {
|
|
reason = `ON DELETE action is different (${sourceDeleteAction} vs ${targetDeleteAction})`;
|
|
} else if (sourceUpdateAction !== targetUpdateAction) {
|
|
reason = `ON UPDATE action is different (${sourceUpdateAction} vs ${targetUpdateAction})`;
|
|
}
|
|
|
|
if (reason) {
|
|
return dropAndRecreateConstraint(source, target, reason);
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
const compareUniqueConstraint: CompareFunction<DatabaseUniqueConstraint> = (source, target) => {
|
|
let reason = '';
|
|
|
|
if (!haveEqualColumns(source.columnNames, target.columnNames)) {
|
|
reason = `columns are different (${source.columnNames} vs ${target.columnNames})`;
|
|
}
|
|
|
|
if (reason) {
|
|
return dropAndRecreateConstraint(source, target, reason);
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
const compareCheckConstraint: CompareFunction<DatabaseCheckConstraint> = (source, target) => {
|
|
if (source.expression !== target.expression) {
|
|
// comparing expressions is hard because postgres reconstructs it with different formatting
|
|
// for now if the constraint exists with the same name, we will just skip it
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
const dropAndRecreateConstraint = (
|
|
source: DatabaseConstraint,
|
|
target: DatabaseConstraint,
|
|
reason: string,
|
|
): SchemaDiff[] => {
|
|
return [
|
|
{
|
|
type: 'ConstraintDrop',
|
|
tableName: target.tableName,
|
|
constraintName: target.name,
|
|
reason,
|
|
},
|
|
{ type: 'ConstraintAdd', constraint: source, reason },
|
|
];
|
|
};
|