06da1b9f7e
* fix(external-media): one row per external file per event (#1162) Two overlapping import-external runs against the same event inserted every file twice. The route checked for an existing external_relpath and then inserted, with an fs.stat and a sharp().metadata() read sitting in between — a window wide enough for both runs to see "not there". A reporter's event held 8004 rows for 6012 distinct paths. Nothing at the storage layer stopped it: migration 041 created only a NON-unique (event_id, source_origin) index. - migration 186 removes the duplicates that already exist and adds a partial unique index on (event_id, external_relpath). The survivor is the lowest id that has a thumbnail, so a half-finished import does not cost a grid tile, and hero references are repointed first because the FK is SET NULL. - the route treats a unique violation as a skip and carries on, so a second writer this process cannot see (another replica) converges instead of duplicating or 500ing. - a second import while one is already running now gets a 409 rather than walking the whole tree to have every insert bounce. The duplicates' thumbnail files are left behind as unreferenced bytes — a migration is the wrong place to reach into storage, which may be S3. * fix(external-media): keep dependent rows and legacy restores intact (#1162) External review found two real defects in the dedupe half of this fix. Dangling rows on SQLite. Every FK into photos declares ON DELETE CASCADE, but PicPeak never sets `PRAGMA foreign_keys = ON` — the codebase says so where it deletes an event (adminEvents/helpers.js:245) — so on every SQLite install the cascade is inert and deleting a duplicate photo left its face embeddings, guest feedback and admin marks behind, pointing at an id that no longer exists. Biometric data outliving its photo is exactly the invariant the event delete goes out of its way to hold. Dependents are now handled explicitly, and moved rather than discarded where they can be: the duplicates were separate tiles in the grid, so a guest's comment or an admin's rating could legitimately be on either, and dropping it inside a fix for silent data loss would be its own bug. Where the target already holds an equivalent row — the same guest's like, the same admin's mark, the same transfer's entry — the loser is dropped, because those tables mean one row per (photo, actor). photo_faces is the deliberate exception: both rows were scanned, so moving would duplicate every embedding and split the person clusters built from them. Legacy restores. Suspending FK enforcement does not suspend a UNIQUE index on either engine, so a .picpeak backup taken before migration 186 — carrying exactly the duplicates it removes — would hit the new index mid-batchInsert and roll the whole restore back, after every table had already been emptied. The restore now drops the index for the load and rebuilds it after running the same dedupe. Also: a failed CREATE INDEX is no longer swallowed. Recording the migration as applied without it leaves the install permanently racy, with nothing to trigger a retry. The shared work moves to services/externalPhotoDedupe.js, which the migration and the restore both call. * fix(external-media): reconcile derived state around the dedupe (#1162) Second review round, four more real findings. The index throw did not actually stop anything. run-migrations-safe.js treats 23505 as "schema already exists" and marks the migration applied (run-migrations-safe.js:138) — and a CREATE UNIQUE INDEX that finds duplicate rows raises exactly 23505 on Postgres. A replica inserting one between the dedupe and the index lock is a real rolling-deploy shape, and the outcome was the thing the throw was added to prevent. The index is now verified against the catalog afterwards, and failure raises a code-less error the runner cannot mistake for idempotence. Two people sharing a device were treated as one. photo_feedback carries both guest_identifier (per device) and guest_id (per person, migration 078), and feedbackService scopes by guest_id when present. Keying equivalence on the identifier alone deleted one of two different people's ratings. It now uses the same COALESCE rule the service does. Deleting faces raw left ghost people. event_people counts and centroids are derived from the photo_faces rows being removed, and #1132's separation snapshots hold a copy of each side's centroid — which is why faceProcessor exposes purgePhotoFaces and says it is "called from every photo-deletion path". The dedupe now goes through it. Reparenting feedback left the survivor's totals stale. photos carries denormalized feedback_count / like_count / average_rating / favorite_count and the later reaction and colour counts, so a survivor that now owns feedback kept rendering zero. updatePhotoFeedbackStats takes a trx so the dedupe can recompute on its own connection. Also: the equivalence-key delimiter was a literal NUL byte, which made git classify the whole file as binary and hide its diff. Escaped. * fix(external-media): stop the dedupe discarding half-states (#1162) Third review round. Five findings, four applied. - is_hidden joins the feedback equivalence key. feedbackService lets a moderator-hidden row coexist with the guest's visible replacement and counts only the visible one, so ignoring it deleted the visible row as redundant. - admin marks merge instead of dropping. rating and color_label are written independently, so the same admin can have rated one tile and coloured the other; the loser now hands over any field the winner has no value for. - a survivor that loses the only completed scan is requeued. Otherwise the purge takes the sole embeddings and nothing re-queues it — the photo just silently stops having a face. - view_count and download_count are carried over. Those are real interactions recorded per row, and dropping them quietly lowered the engagement the admin grid shows. Not applied: repointing a category hero can in principle land on a survivor in another category. It needs the two duplicate rows to have been re-categorised apart after the racing import, and the result is a cosmetic hero mismatch that the admin category routes already guard on write. Not worth the extra branch in a data migration. * fix(external-media): invalidate the download zip when duplicates are removed (#1162) External review of the stable twin. Applies to both branches. The pre-built "download everything" archive still contained the duplicate rows the dedupe had just deleted, so guests kept receiving them until something else happened to invalidate it. Every ordinary photo-deletion path calls downloadZipService.invalidate for exactly this reason. The columns are cleared rather than the service being called: that service carries debounce timers and a regeneration queue, which is not something a migration should start. getZipInfo already treats a cleared record as a cache miss and rebuilds on the next request, so this is the durable half of what invalidate does. The stale object is left in storage for the same reason the duplicates' thumbnails are — a migration is the wrong place to reach into a backend that may be S3. --------- Co-authored-by: Paul Nothaft <paul@MacStudio-von-Paul.local>
555 lines
22 KiB
JavaScript
555 lines
22 KiB
JavaScript
/**
|
|
* One row per external file per event (#1162).
|
|
*
|
|
* The migration has two halves and they fail differently: the cleanup can take
|
|
* out the wrong row of a pair (losing a thumbnail, orphaning an event's hero),
|
|
* and the index can fail to be created at all — leaving an install that looks
|
|
* migrated and is still racing. Both are pinned here.
|
|
*/
|
|
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
|
|
const migration = require('../../migrations/core/186_external_relpath_unique');
|
|
|
|
describe('migration 186 — unique (event_id, external_relpath) (#1162)', () => {
|
|
let knex; let tmpDir;
|
|
|
|
beforeAll(async () => {
|
|
tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'picpeak-mig186-'));
|
|
knex = require('knex')({
|
|
client: 'sqlite3',
|
|
connection: { filename: path.join(tmpDir, 'db.sqlite') },
|
|
useNullAsDefault: true,
|
|
});
|
|
});
|
|
|
|
afterAll(async () => {
|
|
if (knex) await knex.destroy();
|
|
await fs.promises.rm(tmpDir, { recursive: true, force: true }).catch(() => {});
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
for (const table of [
|
|
'photos', 'events', 'photo_categories', 'photo_feedback',
|
|
'photo_admin_marks', 'photo_faces', 'image_access_logs', 'transfer_files',
|
|
'event_people', 'event_people_merge_dismissals',
|
|
]) {
|
|
await knex.schema.dropTableIfExists(table);
|
|
}
|
|
await knex.schema.createTable('events', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('hero_photo_id');
|
|
t.string('download_zip_path');
|
|
t.string('download_zip_generated_at');
|
|
});
|
|
await knex.schema.createTable('photo_categories', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('hero_photo_id');
|
|
});
|
|
await knex.schema.createTable('photos', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('event_id');
|
|
t.string('external_relpath');
|
|
t.string('thumbnail_path');
|
|
t.string('source_origin').defaultTo('managed');
|
|
t.integer('feedback_count').defaultTo(0);
|
|
t.integer('like_count').defaultTo(0);
|
|
t.decimal('average_rating', 3, 2).defaultTo(0);
|
|
t.integer('favorite_count').defaultTo(0);
|
|
t.integer('reaction_count').defaultTo(0);
|
|
t.integer('color_label_count').defaultTo(0);
|
|
t.string('face_status');
|
|
t.integer('view_count').defaultTo(0);
|
|
t.integer('download_count').defaultTo(0);
|
|
t.integer('face_count');
|
|
t.string('face_started_at');
|
|
t.text('face_error');
|
|
});
|
|
// Declared exactly as the real schema declares them — CASCADE and all.
|
|
// The point of these tables here is that SQLite does NOT enforce any of
|
|
// it (PicPeak never sets `PRAGMA foreign_keys = ON`), so a bare delete of
|
|
// the photo row leaves every one of them dangling.
|
|
await knex.schema.createTable('photo_feedback', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('photo_id').references('id').inTable('photos').onDelete('CASCADE');
|
|
t.integer('event_id');
|
|
t.string('feedback_type');
|
|
t.text('comment_text');
|
|
t.string('guest_identifier');
|
|
// Per-person guest identity (migration 078). Nullable: galleries without
|
|
// guest identity leave it NULL and fall back to guest_identifier.
|
|
t.integer('guest_id');
|
|
t.integer('rating');
|
|
t.boolean('is_hidden').defaultTo(false);
|
|
t.boolean('is_approved').defaultTo(true);
|
|
});
|
|
await knex.schema.createTable('photo_admin_marks', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('photo_id').notNullable().references('id').inTable('photos').onDelete('CASCADE');
|
|
t.integer('event_id');
|
|
t.integer('admin_id');
|
|
t.integer('rating');
|
|
// Independently writable alongside rating, per photoAdminMarksService.
|
|
t.string('color_label', 16);
|
|
t.unique(['photo_id', 'admin_id'], 'photo_admin_marks_photo_admin_uniq');
|
|
});
|
|
await knex.schema.createTable('photo_faces', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('photo_id').references('id').inTable('photos').onDelete('CASCADE');
|
|
t.integer('event_id');
|
|
// purgePhotoFaces rebuilds the people that lose members, so the cluster
|
|
// link and the vectors recomputeCentroid reads have to be here for this
|
|
// to exercise the real path rather than a stub.
|
|
t.integer('person_id');
|
|
t.binary('embedding');
|
|
t.float('det_score');
|
|
});
|
|
await knex.schema.createTable('event_people', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('event_id');
|
|
t.binary('centroid');
|
|
t.integer('face_count').defaultTo(0);
|
|
});
|
|
await knex.schema.createTable('event_people_merge_dismissals', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('event_id');
|
|
t.binary('centroid_a');
|
|
t.binary('centroid_b');
|
|
});
|
|
await knex.schema.createTable('image_access_logs', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('photo_id');
|
|
});
|
|
await knex.schema.createTable('transfer_files', (t) => {
|
|
t.increments('id').primary();
|
|
t.integer('transfer_id');
|
|
t.integer('photo_id');
|
|
t.unique(['transfer_id', 'photo_id'], 'transfer_files_unique');
|
|
});
|
|
});
|
|
|
|
/** Two duplicate rows for the same file: id 1 survives, id 2 is doomed. */
|
|
const seedPair = async () => {
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't', source_origin: 'external' },
|
|
]);
|
|
};
|
|
|
|
const rows = () => knex('photos').orderBy('id', 'asc').select('*');
|
|
|
|
it('collapses a duplicated pair to one row and leaves distinct paths alone', async () => {
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't1', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't2', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/y.jpg', thumbnail_path: 't3', source_origin: 'external' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const after = await rows();
|
|
expect(after.map((r) => r.external_relpath)).toEqual(['a/x.jpg', 'a/y.jpg']);
|
|
// Lowest id survives when both sides are equally complete.
|
|
expect(after[0].id).toBe(1);
|
|
});
|
|
|
|
it('does not collapse the same path across different events', async () => {
|
|
// The constraint is per event. Two events referencing the same NAS folder
|
|
// is a supported setup, and treating those as duplicates would delete one
|
|
// event's entire library.
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' },
|
|
{ event_id: 2, external_relpath: 'a/x.jpg', source_origin: 'external' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photos').count('* as c').first()).toEqual({ c: 2 });
|
|
});
|
|
|
|
it('never touches managed rows, however many carry NULL', async () => {
|
|
// Every managed photo has external_relpath NULL. Grouping on it without
|
|
// the NOT NULL filter would make them all one enormous "duplicate" group
|
|
// and delete the entire library bar one row.
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: null, source_origin: 'managed' },
|
|
{ event_id: 1, external_relpath: null, source_origin: 'managed' },
|
|
{ event_id: 1, external_relpath: null, source_origin: 'managed' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photos').count('* as c').first()).toEqual({ c: 3 });
|
|
});
|
|
|
|
it('keeps the row that has a thumbnail, not merely the lowest id', async () => {
|
|
// An import killed mid-flight leaves rows without a thumbnail. Dropping
|
|
// the completed one would blank a tile in the grid for no reason.
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: null, source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 'thumb.jpg', source_origin: 'external' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const after = await rows();
|
|
expect(after).toHaveLength(1);
|
|
expect(after[0].thumbnail_path).toBe('thumb.jpg');
|
|
});
|
|
|
|
it('repoints a hero that pointed at the row being removed', async () => {
|
|
// events.hero_photo_id is ON DELETE SET NULL, so without this the cleanup
|
|
// silently strips the event's hero image — a visible regression caused
|
|
// entirely by the fix.
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't', source_origin: 'external' },
|
|
]);
|
|
await knex('events').insert({ id: 1, hero_photo_id: 2 });
|
|
await knex('photo_categories').insert({ id: 1, hero_photo_id: 2 });
|
|
|
|
await migration.up(knex);
|
|
|
|
expect((await knex('events').where({ id: 1 }).first()).hero_photo_id).toBe(1);
|
|
expect((await knex('photo_categories').where({ id: 1 }).first()).hero_photo_id).toBe(1);
|
|
});
|
|
|
|
it('leaves a hero that pointed at the survivor untouched', async () => {
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', thumbnail_path: 't', source_origin: 'external' },
|
|
]);
|
|
await knex('events').insert({ id: 1, hero_photo_id: 1 });
|
|
|
|
await migration.up(knex);
|
|
|
|
expect((await knex('events').where({ id: 1 }).first()).hero_photo_id).toBe(1);
|
|
});
|
|
|
|
it('makes a second insert of the same path impossible afterwards', async () => {
|
|
// The whole point. Without this the route is still racing, and the
|
|
// migration is recorded as applied.
|
|
await knex('photos').insert({ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' });
|
|
|
|
await migration.up(knex);
|
|
|
|
await expect(
|
|
knex('photos').insert({ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' })
|
|
).rejects.toThrow(/unique/i);
|
|
});
|
|
|
|
it('still admits managed rows once the index exists', async () => {
|
|
await migration.up(knex);
|
|
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: null, source_origin: 'managed' },
|
|
{ event_id: 1, external_relpath: null, source_origin: 'managed' },
|
|
]);
|
|
|
|
expect(await knex('photos').count('* as c').first()).toEqual({ c: 2 });
|
|
});
|
|
|
|
it('leaves nothing dangling behind the deleted row', async () => {
|
|
// SQLite never enforces the ON DELETE CASCADE these tables declare, so a
|
|
// bare delete strands biometric embeddings, feedback and marks pointing at
|
|
// a photo id that no longer exists — on every SQLite install.
|
|
await seedPair();
|
|
await knex('photo_faces').insert({ photo_id: 2, event_id: 1 });
|
|
await knex('image_access_logs').insert({ photo_id: 2 });
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photo_faces').where('photo_id', 2).first()).toBeUndefined();
|
|
expect(await knex('image_access_logs').where('photo_id', 2).first()).toBeUndefined();
|
|
});
|
|
|
|
it('does not carry the duplicate\'s faces over to the survivor', async () => {
|
|
// Both rows were scanned independently, so the survivor already holds its
|
|
// own embeddings. Moving these would fabricate a second copy of every face
|
|
// and split the person clusters built from them.
|
|
await seedPair();
|
|
await knex('photo_faces').insert([{ photo_id: 1, event_id: 1 }, { photo_id: 2, event_id: 1 }]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photo_faces').count('* as c').first()).toEqual({ c: 1 });
|
|
});
|
|
|
|
it('moves a guest comment to the survivor rather than deleting it', async () => {
|
|
// The duplicates were separate tiles in the grid, so a guest could have
|
|
// commented on either. Silently dropping that inside a fix for silent data
|
|
// loss would be its own bug.
|
|
await seedPair();
|
|
await knex('photo_feedback').insert({
|
|
photo_id: 2, event_id: 1, feedback_type: 'comment',
|
|
comment_text: 'lovely shot', guest_identifier: 'guest-a',
|
|
});
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('photo_feedback');
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0].photo_id).toBe(1);
|
|
expect(rows[0].comment_text).toBe('lovely shot');
|
|
});
|
|
|
|
it('keeps both comments when the same guest commented on both tiles', async () => {
|
|
await seedPair();
|
|
await knex('photo_feedback').insert([
|
|
{ photo_id: 1, event_id: 1, feedback_type: 'comment', comment_text: 'one', guest_identifier: 'g' },
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'comment', comment_text: 'two', guest_identifier: 'g' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('photo_feedback').orderBy('id');
|
|
expect(rows.map((r) => r.comment_text)).toEqual(['one', 'two']);
|
|
expect(rows.every((r) => r.photo_id === 1)).toBe(true);
|
|
});
|
|
|
|
it('does not double-count a like the same guest left on both tiles', async () => {
|
|
// Unlike comments, a like is a per-guest toggle: moving it would show two
|
|
// likes from one person.
|
|
await seedPair();
|
|
await knex('photo_feedback').insert([
|
|
{ photo_id: 1, event_id: 1, feedback_type: 'like', guest_identifier: 'g' },
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'like', guest_identifier: 'g' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photo_feedback').count('* as c').first()).toEqual({ c: 1 });
|
|
});
|
|
|
|
it('moves a like from a guest the survivor has never seen', async () => {
|
|
await seedPair();
|
|
await knex('photo_feedback').insert({
|
|
photo_id: 2, event_id: 1, feedback_type: 'like', guest_identifier: 'other',
|
|
});
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('photo_feedback');
|
|
expect(rows).toHaveLength(1);
|
|
expect(rows[0].photo_id).toBe(1);
|
|
});
|
|
|
|
it('moves an admin mark, and drops it when that admin already marked the survivor', async () => {
|
|
// photo_admin_marks is UNIQUE(photo_id, admin_id), so a blind move would
|
|
// throw and abort the migration.
|
|
await seedPair();
|
|
await knex('photo_admin_marks').insert([
|
|
{ photo_id: 1, event_id: 1, admin_id: 7, rating: 5 },
|
|
{ photo_id: 2, event_id: 1, admin_id: 7, rating: 2 },
|
|
{ photo_id: 2, event_id: 1, admin_id: 9, rating: 4 },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('photo_admin_marks').orderBy('admin_id');
|
|
expect(rows.map((r) => [r.admin_id, r.rating])).toEqual([[7, 5], [9, 4]]);
|
|
expect(rows.every((r) => r.photo_id === 1)).toBe(true);
|
|
});
|
|
|
|
it('respects the transfer_files uniqueness when moving membership', async () => {
|
|
await seedPair();
|
|
await knex('transfer_files').insert([
|
|
{ transfer_id: 3, photo_id: 1 },
|
|
{ transfer_id: 3, photo_id: 2 },
|
|
{ transfer_id: 4, photo_id: 2 },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('transfer_files').orderBy('transfer_id');
|
|
expect(rows.map((r) => r.transfer_id)).toEqual([3, 4]);
|
|
expect(rows.every((r) => r.photo_id === 1)).toBe(true);
|
|
});
|
|
|
|
it('recomputes the survivor\'s feedback totals after reparenting rows', async () => {
|
|
// photos carries denormalized counters (migration 033). A survivor that
|
|
// now OWNS the feedback but still renders zero is the visible half of
|
|
// getting this wrong.
|
|
await seedPair();
|
|
await knex('photo_feedback').insert([
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'like', guest_identifier: 'g1' },
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'rating', rating: 4, guest_identifier: 'g1' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const survivor = await knex('photos').where('id', 1).first();
|
|
expect(survivor.like_count).toBe(1);
|
|
expect(Number(survivor.average_rating)).toBe(4);
|
|
expect(survivor.feedback_count).toBe(1);
|
|
});
|
|
|
|
it('keeps two people who share a device apart', async () => {
|
|
// guest_identifier is per-device; guest_id is per-person (migration 078),
|
|
// and feedbackService scopes by guest_id when it is present. Keying on the
|
|
// identifier alone would read these as one person and delete a rating.
|
|
await seedPair();
|
|
await knex('photo_feedback').insert([
|
|
{ photo_id: 1, event_id: 1, feedback_type: 'rating', rating: 5, guest_identifier: 'shared', guest_id: 10 },
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'rating', rating: 2, guest_identifier: 'shared', guest_id: 11 },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('photo_feedback').orderBy('guest_id');
|
|
expect(rows.map((r) => [r.guest_id, r.rating])).toEqual([[10, 5], [11, 2]]);
|
|
});
|
|
|
|
it('still dedupes one person voting on both tiles', async () => {
|
|
await seedPair();
|
|
await knex('photo_feedback').insert([
|
|
{ photo_id: 1, event_id: 1, feedback_type: 'like', guest_identifier: 'shared', guest_id: 10 },
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'like', guest_identifier: 'shared', guest_id: 10 },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photo_feedback').count('* as c').first()).toEqual({ c: 1 });
|
|
});
|
|
|
|
it('rebuilds the people that lose members, rather than deleting faces raw', async () => {
|
|
// purgePhotoFaces is "called from every photo-deletion path" precisely
|
|
// because event_people counts and centroids are derived from the rows
|
|
// being removed. A bare delete leaves a ghost person behind.
|
|
await seedPair();
|
|
await knex('event_people').insert({ id: 5, event_id: 1, face_count: 1 });
|
|
await knex('photo_faces').insert({ photo_id: 2, event_id: 1, person_id: 5 });
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photo_faces').count('* as c').first()).toEqual({ c: 0 });
|
|
// The person had exactly one member and loses it, so it goes with it.
|
|
expect(await knex('event_people').where('id', 5).first()).toBeUndefined();
|
|
});
|
|
|
|
it('keeps a hidden moderation record from swallowing the visible replacement', async () => {
|
|
// feedbackService lets both coexist and counts only the visible one.
|
|
await seedPair();
|
|
await knex('photo_feedback').insert([
|
|
{ photo_id: 1, event_id: 1, feedback_type: 'like', guest_identifier: 'g', is_hidden: true },
|
|
{ photo_id: 2, event_id: 1, feedback_type: 'like', guest_identifier: 'g', is_hidden: false },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect(await knex('photo_feedback').count('* as c').first()).toEqual({ c: 2 });
|
|
});
|
|
|
|
it('merges the independent halves of one admin\'s mark', async () => {
|
|
// rating and color_label are written independently, so the same admin can
|
|
// have rated one tile and coloured the other.
|
|
await seedPair();
|
|
await knex('photo_admin_marks').insert([
|
|
{ photo_id: 1, event_id: 1, admin_id: 7, rating: 5, color_label: null },
|
|
{ photo_id: 2, event_id: 1, admin_id: 7, rating: null, color_label: 'red' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
const rows = await knex('photo_admin_marks');
|
|
expect(rows).toHaveLength(1);
|
|
expect([rows[0].rating, rows[0].color_label]).toEqual([5, 'red']);
|
|
});
|
|
|
|
it('requeues the survivor when the duplicate held the only scan', async () => {
|
|
// Otherwise the sole embeddings go with the purge and nothing re-queues:
|
|
// the photo just silently stops having a face.
|
|
await seedPair();
|
|
await knex('photo_faces').insert({ photo_id: 2, event_id: 1 });
|
|
|
|
await migration.up(knex);
|
|
|
|
expect((await knex('photos').where('id', 1).first()).face_status).toBe('pending');
|
|
});
|
|
|
|
it('carries the duplicate\'s views and downloads over', async () => {
|
|
await seedPair();
|
|
await knex('photos').where('id', 1).update({ view_count: 2, download_count: 1 });
|
|
await knex('photos').where('id', 2).update({ view_count: 5, download_count: 3 });
|
|
|
|
await migration.up(knex);
|
|
|
|
const survivor = await knex('photos').where('id', 1).first();
|
|
expect([survivor.view_count, survivor.download_count]).toEqual([7, 4]);
|
|
});
|
|
|
|
it('fails loudly rather than recording itself applied without the index', async () => {
|
|
// Swallowing a failed CREATE INDEX would leave the install permanently
|
|
// racy — the in-flight guard only covers one process — with nothing to
|
|
// trigger a retry. Driven through the helper the migration calls, against
|
|
// a table that still holds duplicates — i.e. what it would face if the
|
|
// dedupe above had not achieved uniqueness.
|
|
await seedPair();
|
|
const { createExternalRelpathIndex } = require('../../src/services/externalPhotoDedupe');
|
|
|
|
await expect(createExternalRelpathIndex(knex)).rejects.toThrow(/unique/i);
|
|
});
|
|
|
|
it('invalidates the pre-built download zip for the affected event', async () => {
|
|
// The cached archive still contains the rows just removed, and every
|
|
// ordinary photo-deletion path invalidates it for exactly that reason.
|
|
// getZipInfo treats a cleared record as a miss and rebuilds on request.
|
|
await seedPair();
|
|
await knex('events').insert({
|
|
id: 1, download_zip_path: 'events/active/x/.download-cache/all.zip',
|
|
download_zip_generated_at: '2026-01-01',
|
|
});
|
|
|
|
await migration.up(knex);
|
|
|
|
const ev = await knex('events').where('id', 1).first();
|
|
expect(ev.download_zip_path).toBeNull();
|
|
expect(ev.download_zip_generated_at).toBeNull();
|
|
});
|
|
|
|
it('leaves an untouched event\'s zip alone', async () => {
|
|
await seedPair();
|
|
await knex('events').insert([
|
|
{ id: 1, download_zip_path: 'a.zip', download_zip_generated_at: '2026-01-01' },
|
|
{ id: 2, download_zip_path: 'b.zip', download_zip_generated_at: '2026-01-01' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
|
|
expect((await knex('events').where('id', 2).first()).download_zip_path).toBe('b.zip');
|
|
});
|
|
|
|
it('is idempotent', async () => {
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' },
|
|
]);
|
|
|
|
await migration.up(knex);
|
|
const once = await rows();
|
|
await migration.up(knex);
|
|
|
|
expect(await rows()).toEqual(once);
|
|
});
|
|
|
|
it('rolls back to an unconstrained table', async () => {
|
|
await migration.up(knex);
|
|
await migration.down(knex);
|
|
|
|
await knex('photos').insert([
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' },
|
|
{ event_id: 1, external_relpath: 'a/x.jpg', source_origin: 'external' },
|
|
]);
|
|
expect(await knex('photos').count('* as c').first()).toEqual({ c: 2 });
|
|
});
|
|
|
|
it('no-ops before 041 has added the column', async () => {
|
|
await knex.schema.dropTableIfExists('photos');
|
|
await knex.schema.createTable('photos', (t) => { t.increments('id').primary(); });
|
|
|
|
await expect(migration.up(knex)).resolves.toBeUndefined();
|
|
});
|
|
});
|