Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix: keep published photos editable after cancelled uploads

HoshinoSuzumiBot7ab6652539f7
diff --git a/packages/database/README.md b/packages/database/README.md
index cccc8686..72863461 100644
--- a/packages/database/README.md
+++ b/packages/database/README.md
@@ -11,3 +11,5 @@ The default self-hosted Site is fixed by its entry, while every site repository
`pnpm db:migrate` and application startup use the same journal-compatible runner. Existing files with pending migrations receive a consistent `*.pre-migration-*.sqlite3` backup before changes. Schema and journal updates share a transaction, foreign keys are checked before commit, and enforcement is restored afterwards. Keep backups with their matching application versions; restoring only an older binary does not reverse schema constraints.
Jobs use source generations, retained deletion tombstones, leases, attempt tokens and input digests. Resource mutation and follow-up task creation share one transaction. Publication guards live only inside the publication transaction and roll back with it; workers never hold database transactions during native processing or storage I/O.
+
+Upload reservations advance the resource generation before transport plans are issued. Their incoming storage reference may differ from the source still published in the photo row, even after a plan is cancelled. Maintenance snapshots therefore read the published photo reference while retaining the current resource generation as their compare-and-swap token. A later reservation, mutation, or publication invalidates that token; cancellation never rolls the generation back or revives older workers.
diff --git a/packages/database/src/site-repository.ts b/packages/database/src/site-repository.ts
index 49c67e53..9ee2e849 100644
--- a/packages/database/src/site-repository.ts
+++ b/packages/database/src/site-repository.ts
@@ -43,13 +43,12 @@ export function createSiteRepository(db: AppDatabase, site: SiteContext) {
),
)
.get()
- if (
- !version ||
- version.deleted ||
- version.storageConnectionId !== photo.storageConnectionId ||
- version.storageRevision !== photo.storageRevision ||
- version.storageKey !== photo.storageKey
- )
+ // Upload preparation fences older work before issuing a transport plan.
+ // Its reference describes the incoming source, not the published photo;
+ // cancelling that plan must not make the still-published source unusable.
+ // Read from the photo's own reference and fence publication with the
+ // current epoch, so later reservations/writes still invalidate this read.
+ if (!version || version.deleted)
throw new CoreError(409, 'Photo source changed; retry the operation')
return { photo, version: version.version }
})
diff --git a/tests/job-leases.test.ts b/tests/job-leases.test.ts
index 1493a09b..e0687cd9 100644
--- a/tests/job-leases.test.ts
+++ b/tests/job-leases.test.ts
@@ -485,3 +485,129 @@ test('temporary heartbeat failures abort processing but remain retryable', async
connection.close()
}
})
+
+test('cancelled upload reservations leave the published photo editable and reindexable without reviving old jobs', async () => {
+ const connection = openDatabase({ filename: ':memory:', migrationsFolder })
+ const { db } = connection
+ try {
+ const { createSiteRepository } = await import('@chronoframe/database')
+ db.insert(tables.photos)
+ .values({
+ id: 'photo',
+ storageKey: 'library/.generated/metadata/edited/photo.jpg',
+ originalUrl: '/image/published.jpg',
+ thumbnailKey: 'library/.generated/thumb.webp',
+ title: 'Published',
+ })
+ .run()
+ const jobs = createSqliteJobRepository(db, DEFAULT_SITE, resolveId)
+ jobs.enqueue({ type: 'photo', storageKey: 'library/photo.jpg' })
+ const oldLease = (await jobs.claim('old-worker', clock, 60000))!
+ const reserved = reserveResource(db, DEFAULT_SITE, 'photo', {
+ storageConnectionId: null,
+ storageRevision: 1,
+ storageKey: 'library/photo.jpg',
+ })
+ // Cancelling an uncompleted upload removes its transport session; no bytes or
+ // photo row were published, and its resource epoch must not be rolled back.
+ const repository = createSiteRepository(db, DEFAULT_SITE)
+ const snapshot = (await repository.capturePhoto('photo'))!
+ assert.equal(snapshot.version, reserved)
+ assert.equal(
+ snapshot.photo.storageKey,
+ 'library/.generated/metadata/edited/photo.jpg',
+ )
+ await repository.updatePhotoSnapshot('photo', snapshot.version, {
+ exif: { Title: 'Reindexed' },
+ })
+ const writer = createSqlitePhotoWriteRepository(db, DEFAULT_SITE)
+ const photo = (await writer.get('photo'))!
+ await writer.update(
+ 'photo',
+ { title: 'Edited after cancellation' },
+ undefined,
+ photo.mutationVersion,
+ )
+ const row = db.select().from(tables.photos).get()!
+ assert.equal(row.title, 'Edited after cancellation')
+ assert.equal(row.exif?.Title, 'Reindexed')
+ assert.equal(row.thumbnailKey, 'library/.generated/thumb.webp')
+ assert.equal(row.storageKey, snapshot.photo.storageKey)
+ assert.ok(row.sourceVersion > reserved)
+ assert.throws(
+ () =>
+ jobs.commit(
+ oldLease,
+ () => {
+ throw new Error('must not publish')
+ },
+ { now: clock },
+ ),
+ LostLeaseError,
+ )
+ } finally {
+ connection.close()
+ }
+})
+
+test('maintenance snapshots still lose to newer upload reservations and publications', async () => {
+ const connection = openDatabase({ filename: ':memory:', migrationsFolder })
+ const { db } = connection
+ try {
+ const { createSiteRepository } = await import('@chronoframe/database')
+ db.insert(tables.photos)
+ .values({ id: 'photo', storageKey: 'published.jpg', title: 'Published' })
+ .run()
+ const repository = createSiteRepository(db, DEFAULT_SITE)
+ reserveResource(db, DEFAULT_SITE, 'photo', {
+ storageConnectionId: null,
+ storageRevision: 1,
+ storageKey: 'first-upload.jpg',
+ })
+ const snapshot = (await repository.capturePhoto('photo'))!
+ const writer = createSqlitePhotoWriteRepository(db, DEFAULT_SITE)
+ const mutation = (await writer.get('photo'))!
+ reserveResource(db, DEFAULT_SITE, 'photo', {
+ storageConnectionId: null,
+ storageRevision: 1,
+ storageKey: 'second-upload.jpg',
+ })
+ await assert.rejects(
+ repository.updatePhotoSnapshot('photo', snapshot.version, {
+ title: 'Stale reindex',
+ }),
+ )
+ await assert.rejects(
+ writer.update(
+ 'photo',
+ { title: 'Stale edit' },
+ undefined,
+ mutation.mutationVersion,
+ ),
+ )
+ const current = (await repository.capturePhoto('photo'))!
+ const jobs = createSqliteJobRepository(db, DEFAULT_SITE, resolveId)
+ jobs.enqueue({ type: 'photo', storageKey: 'second-upload.jpg' })
+ const lease = (await jobs.claim('new-worker', clock, 60000))!
+ jobs.commit(
+ lease,
+ () =>
+ db
+ .update(tables.photos)
+ .set({ title: 'New upload', storageKey: 'second-upload.jpg' })
+ .where(eq(tables.photos.id, 'photo'))
+ .run(),
+ { now: clock, resourceUpdated: true },
+ )
+ await assert.rejects(
+ repository.updatePhotoSnapshot('photo', current.version, {
+ title: 'Stale after publication',
+ }),
+ )
+ const latest = (await repository.capturePhoto('photo'))!
+ assert.equal(latest.photo.title, 'New upload')
+ assert.equal(latest.photo.storageKey, 'second-upload.jpg')
+ } finally {
+ connection.close()
+ }
+})