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.

perf(albums): avoid write transactions when slug backfill is complete

Ahmet Ömere22d4f07f338
diff --git a/packages/database/src/album-slugs.ts b/packages/database/src/album-slugs.ts
index 4a2d4375..577a7e6f 100644
--- a/packages/database/src/album-slugs.ts
+++ b/packages/database/src/album-slugs.ts
@@ -4,7 +4,7 @@ import {
isNumericAlbumPathSegment,
isValidAlbumSlug,
} from '@chronoframe/contracts/album-slug'
-import { and, asc, eq } from 'drizzle-orm'
+import { and, asc, eq, isNull } from 'drizzle-orm'
import { alias } from 'drizzle-orm/sqlite-core'
import { type AppDatabase } from './connection.ts'
import * as tables from './schema.ts'
@@ -86,22 +86,33 @@ export function claimAlbumSlug(
throw new Error('Album slug namespace exhausted')
}
-/** Repeatable repair for old installations and rollback-created albums. */
+function albumsWithoutSlugs(db: AlbumSlugDb) {
+ return db
+ .select({
+ id: tables.albums.id,
+ siteId: tables.albums.siteId,
+ title: tables.albums.title,
+ })
+ .from(tables.albums)
+ .leftJoin(
+ tables.albumSlugs,
+ and(
+ eq(tables.albumSlugs.siteId, tables.albums.siteId),
+ eq(tables.albumSlugs.albumId, tables.albums.id),
+ eq(tables.albumSlugs.isCurrent, true),
+ ),
+ )
+ .where(isNull(tables.albumSlugs.slug))
+}
+
export function backfillAlbumSlugs(db: AppDatabase) {
+ if (!albumsWithoutSlugs(db).limit(1).get()) return 0
return db.transaction(
(tx) => {
- const albums = tx
- .select({
- id: tables.albums.id,
- siteId: tables.albums.siteId,
- title: tables.albums.title,
- })
- .from(tables.albums)
- .orderBy(asc(tables.albums.siteId), asc(tables.albums.id))
- .all()
let created = 0
- for (const album of albums) {
- if (currentSlug(tx, album.siteId, album.id)) continue
+ for (const album of albumsWithoutSlugs(tx)
+ .orderBy(asc(tables.albums.siteId), asc(tables.albums.id))
+ .all()) {
claimAlbumSlug(tx, { id: album.siteId }, album.id, album.title, '')
created++
}
diff --git a/tests/album-slug-persistence.test.ts b/tests/album-slug-persistence.test.ts
index 7a339445..1005e44f 100644
--- a/tests/album-slug-persistence.test.ts
+++ b/tests/album-slug-persistence.test.ts
@@ -140,7 +140,10 @@ test('album slugs remain unique, redirectable, and reserved after deletion', asy
})
test('backfill assigns deterministic slugs once for existing rows', () => {
- const sqlite = new Database(':memory:')
+ const statements: string[] = []
+ const sqlite = new Database(':memory:', {
+ verbose: (statement) => statements.push(statement),
+ })
const db = drizzle(sqlite, { schema })
migrate(db, { migrationsFolder: './packages/database/migrations' })
try {
@@ -155,7 +158,10 @@ test('backfill assigns deterministic slugs once for existing rows', () => {
isHidden: false,
})
assert.equal(backfillAlbumSlugs(db), 2)
+ statements.length = 0
assert.equal(backfillAlbumSlugs(db), 0)
+ assert.equal(statements.length, 1)
+ assert.match(statements[0]!, /^select /i)
assert.deepEqual(
db
.select()
@@ -164,6 +170,9 @@ test('backfill assigns deterministic slugs once for existing rows', () => {
.map((row) => row.slug),
['ni-hao', 'ni-hao-1'],
)
+ db.insert(schema.albums).values({ title: '你好' }).run()
+ assert.equal(backfillAlbumSlugs(db), 1)
+ assert.equal(getCurrentAlbumSlug(db, DEFAULT_SITE, 3), 'ni-hao-2')
} finally {
sqlite.close()
}