mirror of
https://github.com/immich-app/immich.git
synced 2025-11-25 05:50:43 +09:00
Some checks are pending
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Docker / pre-job (push) Waiting to run
Docker / Re-Tag ML () (push) Blocked by required conditions
Docker / Re-Tag ML (-armnn) (push) Blocked by required conditions
Docker / Re-Tag ML (-cuda) (push) Blocked by required conditions
Docker / Re-Tag ML (-openvino) (push) Blocked by required conditions
Docker / Re-Tag ML (-rknn) (push) Blocked by required conditions
Docker / Re-Tag ML (-rocm) (push) Blocked by required conditions
Docker / Re-Tag Server () (push) Blocked by required conditions
Docker / Build and Push ML (armnn, linux/arm64, -armnn) (push) Blocked by required conditions
Docker / Build and Push ML (cpu, ) (push) Blocked by required conditions
Docker / Build and Push ML (cuda, linux/amd64, -cuda) (push) Blocked by required conditions
Docker / Build and Push ML (openvino, linux/amd64, -openvino) (push) Blocked by required conditions
Docker / Build and Push ML (rknn, linux/arm64, -rknn) (push) Blocked by required conditions
Docker / Build and Push ML (rocm, linux/amd64, {"linux/amd64": "mich"}, -rocm) (push) Blocked by required conditions
Docker / Build and Push Server (push) Blocked by required conditions
Docker / Docker Build & Push Server Success (push) Blocked by required conditions
Docker / Docker Build & Push ML Success (push) Blocked by required conditions
Docs build / pre-job (push) Waiting to run
Docs build / Docs Build (push) Blocked by required conditions
Static Code Analysis / pre-job (push) Waiting to run
Static Code Analysis / Run Dart Code Analysis (push) Blocked by required conditions
Static Code Analysis / zizmor (push) Waiting to run
Test / pre-job (push) Waiting to run
Test / Test & Lint Server (push) Blocked by required conditions
Test / Unit Test CLI (push) Blocked by required conditions
Test / Unit Test CLI (Windows) (push) Blocked by required conditions
Test / Lint Web (push) Blocked by required conditions
Test / Test Web (push) Blocked by required conditions
Test / Test i18n (push) Blocked by required conditions
Test / End-to-End Lint (push) Blocked by required conditions
Test / Medium Tests (Server) (push) Blocked by required conditions
Test / End-to-End Tests (Server & CLI) (ubuntu-24.04-arm) (push) Blocked by required conditions
Test / End-to-End Tests (Server & CLI) (ubuntu-latest) (push) Blocked by required conditions
Test / End-to-End Tests (Web) (ubuntu-24.04-arm) (push) Blocked by required conditions
Test / End-to-End Tests (Web) (ubuntu-latest) (push) Blocked by required conditions
Test / End-to-End Tests Success (push) Blocked by required conditions
Test / Unit Test Mobile (push) Blocked by required conditions
Test / Unit Test ML (push) Blocked by required conditions
Test / .github Files Formatting (push) Blocked by required conditions
Test / ShellCheck (push) Waiting to run
Test / OpenAPI Clients (push) Waiting to run
Test / SQL Schema Checks (push) Waiting to run
* feat: naming strategy * feat: detect renames
165 lines
4.9 KiB
TypeScript
165 lines
4.9 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: {
|
|
return asRenameKey([constraint.type, constraint.tableName, constraint.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 },
|
|
];
|
|
};
|