From a05e2a31d9ccf5bc37e7cc2b609418c54c63ff4a Mon Sep 17 00:00:00 2001
From: Denis Bykhov <bykhov.denis@gmail.com>
Date: Fri, 8 Nov 2024 15:13:18 +0500
Subject: [PATCH] Fix backup (#7134)

---
 server/postgres/src/storage.ts | 8 ++++----
 server/postgres/src/utils.ts   | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/server/postgres/src/storage.ts b/server/postgres/src/storage.ts
index 69c0321613..fa3f5b7d72 100644
--- a/server/postgres/src/storage.ts
+++ b/server/postgres/src/storage.ts
@@ -1085,17 +1085,17 @@ abstract class PostgresAdapterBase implements DbAdapter {
         if (!initialized) {
           if (recheck === true) {
             await this.retryTxn(client, async (client) => {
-              await client`UPDATE ${client(translateDomain(domain))} SET jsonb_set(data, '{%hash%}', 'NULL', true) WHERE "workspaceId" = ${this.workspaceId.name} AND data ->> '%hash%' IS NOT NULL`
+              await client`UPDATE ${client(translateDomain(domain))} SET '%hash%' = NULL WHERE "workspaceId" = ${this.workspaceId.name} AND '%hash%' IS NOT NULL`
             })
           }
-          await init('_id, data', "data ->> '%hash%' IS NOT NULL AND data ->> '%hash%' <> ''")
+          await init('_id, data', "'%hash%' IS NOT NULL AND '%hash%' <> ''")
           initialized = true
         }
         let docs = await ctx.with('next', { mode }, async () => await next(50))
         if (docs.length === 0 && mode === 'hashed') {
           await close(cursorName)
           mode = 'non_hashed'
-          await init('*', "data ->> '%hash%' IS NULL OR data ->> '%hash%' = ''")
+          await init('*', "'%hash%' IS NULL OR '%hash%' = ''")
           docs = await ctx.with('next', { mode }, async () => await next(50))
         }
         if (docs.length === 0) {
@@ -1155,7 +1155,7 @@ abstract class PostgresAdapterBase implements DbAdapter {
       const connection = (await this.getConnection(ctx)) ?? this.client
       const res =
         await connection`SELECT * FROM ${connection(translateDomain(domain))} WHERE _id = ANY(${docs}) AND "workspaceId" = ${this.workspaceId.name}`
-      return res as any as Doc[]
+      return res.map((p) => parseDocWithProjection(p as any))
     })
   }
 
diff --git a/server/postgres/src/utils.ts b/server/postgres/src/utils.ts
index 3ee6566adc..239aff7cf5 100644
--- a/server/postgres/src/utils.ts
+++ b/server/postgres/src/utils.ts
@@ -388,7 +388,7 @@ export class DBCollectionHelper implements DomainHelperOperations {
   }
 }
 
-export function parseDocWithProjection<T extends Doc> (doc: DBDoc, projection: Projection<T> | undefined): T {
+export function parseDocWithProjection<T extends Doc> (doc: DBDoc, projection?: Projection<T> | undefined): T {
   const { workspaceId, data, ...rest } = doc
   for (const key in rest) {
     if ((rest as any)[key] === 'NULL' || (rest as any)[key] === null) {