Fix delete photos to also delete faces and other foreign keys

Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
James Ketr 2021-02-01 17:28:51 -08:00
parent 132803eba3
commit 39069e660e

View File

@ -365,19 +365,22 @@ router.get("/status/:token", function(req, res) {
/**
* 1. Look if there are duplicates.
* 2. Update all other duplicates to be duplicates of the first image that remains
* 2. Update all other duplicates to be duplicates of the first image that
* remains
* 3. If no duplicates, DELETE the entry from referencing tables:
* photohashes, faces
* 4. If there are duplicates, update the HASH (and any FACE) entry to point to the first image that remains
* photohashes, faces, and any face{descriptor,distance} using that face
* 4. If there are duplicates, update the HASH (and any FACE) entry to point
* to the first image that remains
* 5. Delete the entry from photos
* 6. Delete the scaled, thumb, and original from disk
*/
const deletePhoto = function(photo) {
return photoDB.sequelize.transaction(function(transaction) {
return photoDB.sequelize.query("SELECT id FROM photos WHERE duplicate=:id", {
replacements: photo,
type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true
return photoDB.sequelize.query(
"SELECT id FROM photos WHERE duplicate=:id", {
replacements: photo,
type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true
}).then(function(duplicates) {
if (!duplicates.length) {
return null;
@ -393,7 +396,8 @@ const deletePhoto = function(photo) {
return first;
}
// 2. Update all other duplicates to be duplicates of the first image that remains
// 2. Update all other duplicates to be duplicates of the first image
// that remains
console.log("Updating " + needsUpdate + " to point to " + first.id);
return photoDB.sequelize.query(
"UPDATE photos SET duplicate=:first WHERE id IN (:needsUpdate)", {
@ -407,19 +411,53 @@ const deletePhoto = function(photo) {
});
}).then(function(first) {
if (!first) {
console.log("Deleting "+ photo.id + " from photohash.");
console.log("Deleting " + photo.id + " from photohash.");
// 3. If no duplicates, DELETE the entry from photohashes and faces
// and any face{descriptor,distance} using that face
return photoDB.sequelize.query(
"DELETE FROM photohashes WHERE photoId=:id", {
replacements: photo,
transaction: transaction
}).then(() => {
console.log("Deleting "+ photo.id + " from faces.");
// 3. If no duplicates, DELETE the entry from photohashes
return photoDB.sequelize.query(
"DELETE FROM faces WHERE photoId=:id", {
replacements: photo,
transaction: transaction
}).then(() => {
return photoDB.sequelize.query(
"SELECT id FROM faces WHERE photoId=:id", {
replacements: photo,
type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true,
transaction: transaction
}).then((faces) => {
if (faces.length == 0) {
return;
}
const ids = faces.map(face => face.id);
console.log(`Deleting faces: ${JSON.stringify(ids, null, 2)}`);
return photoDB.sequelize.query(
"DELETE FROM facedistances " +
"WHERE face1Id IN (:ids) " +
"OR face2Id IN (:ids)", {
replacements: { ids },
transaction: transaction
}).then(() => {
return photoDB.sequelize.query(
"DELETE FROM facedescriptors WHERE faceId IN (:ids)", {
replacements: { ids },
transaction: transaction
}).then(() => {
return photoDB.sequelize.query(
"DELETE FROM faces WHERE id IN (:ids)", {
replacements: { ids },
transaction: transaction
});
});
});
}).then(() => {
console.log("Deleting " + photo.id + " from faces.");
// 3. If no duplicates, DELETE the entry from photohashes
return photoDB.sequelize.query(
"DELETE FROM faces WHERE photoId=:id", {
replacements: photo,
transaction: transaction
});
});
});
}
@ -461,6 +499,8 @@ const deletePhoto = function(photo) {
});
});
});
}).then(() => {
console.log(`Completed deleting ${photo.id}`);
});
}
@ -538,7 +578,7 @@ router.delete("/:id?", function(req, res/*, next*/) {
});
});
}
/**
* Delete the asset from disk and the DB
*/