Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: bind catalog media library JSONB as a $n parameter on peer sync

Adam Eivy6dc651e48b0b
diff --git a/server/services/catalogDB/sync.js b/server/services/catalogDB/sync.js
index 5f8df52edb..12ee3eb6d4 100644
--- a/server/services/catalogDB/sync.js
+++ b/server/services/catalogDB/sync.js
@@ -127,7 +127,7 @@ export async function upsertMediaFromPeer(media) {
const hasLibrary = Object.prototype.hasOwnProperty.call(media, 'library')
&& media.library && typeof media.library === 'object' && !Array.isArray(media.library);
const libraryColumn = hasLibrary ? ', library' : '';
- const libraryPlaceholder = hasLibrary ? `, ${(hasTombstoneFields ? 10 : 7) + (hasMetadata ? 1 : 0)}` : '';
+ const libraryPlaceholder = hasLibrary ? `, $${(hasTombstoneFields ? 10 : 7) + (hasMetadata ? 1 : 0)}` : '';
const libraryUpdate = hasLibrary ? ', library = EXCLUDED.library' : '';
const libraryParam = hasLibrary ? [JSON.stringify(normalizeLibrary(media.library))] : [];
if (hasTombstoneFields) {
diff --git a/server/services/catalogDBMedia.test.js b/server/services/catalogDBMedia.test.js
index aedc9709a8..8765bb3f32 100644
--- a/server/services/catalogDBMedia.test.js
+++ b/server/services/catalogDBMedia.test.js
@@ -158,6 +158,25 @@ describe('upsertMediaFromPeer', () => {
expect(params).toEqual(['i1', 'a.png', 'portrait', 'r', 'c', 't', false, null, 't', JSON.stringify(metadata)]);
});
+ it('binds the Casting library JSONB as a $n parameter, not an integer literal', async () => {
+ const library = { guide: true };
+ await catalogDB.upsertMediaFromPeer({
+ ingredientId: 'i1', mediaKey: 'a.png', kind: 'reference',
+ createdAt: 't', deleted: false, deletedAt: null, library,
+ });
+ const { sql, params } = calls[0];
+ expect(sql.replace(/\s+/g, ' ')).toMatch(/\$9, \$10\)/);
+ expect(params).toHaveLength(10);
+ expect(JSON.parse(params[9])).toMatchObject(library);
+
+ calls.length = 0;
+ await catalogDB.upsertMediaFromPeer({
+ ingredientId: 'i1', mediaKey: 'b.png', kind: 'reference', createdAt: 't', library,
+ });
+ expect(calls[0].sql.replace(/\s+/g, ' ')).toMatch(/\$6, \$7\)/);
+ expect(calls[0].params).toHaveLength(7);
+ });
+
it('uses the tombstone-less INSERT (preserve local state) when the keys are absent', async () => {
await catalogDB.upsertMediaFromPeer({
ingredientId: 'i1', mediaKey: 'a.png', kind: 'portrait', createdAt: 't',