Skip to content

Commit 2f26bff

Browse files
committed
PEER-236: Add persistence for collab room updated at
Signed-off-by: SeeuSim <[email protected]>
1 parent 16d1fa3 commit 2f26bff

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

backend/chat/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"devDependencies": {
3838
"@types/express": "^4.17.21",
3939
"@types/node": "^22.5.5",
40+
"@types/pg": "^8.11.10",
4041
"drizzle-kit": "^0.24.2",
4142
"nodemon": "^3.1.4",
4243
"pino-pretty": "^11.2.2",

backend/collaboration/src/lib/y-postgres/persistence.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
1+
import { Pool } from 'pg';
12
import { PostgresqlPersistence } from 'y-postgresql';
23
import * as Y from 'yjs';
34

45
import { dbConfig } from '@/config';
6+
import { logger } from '@/lib/utils';
57
import type { IWSSharedDoc } from '@/types/interfaces';
68

79
import { setPersistence } from './utils';
810

11+
// From y-postgresql
12+
const defaultTableName = 'yjs-writings';
13+
14+
async function migrateTable() {
15+
// Custom logic to add `updated_at` column if purging is desired
16+
const p = new Pool(dbConfig);
17+
const conn = await p.connect().then((client) => {
18+
logger.info('Migration Client connected');
19+
return client;
20+
});
21+
await conn
22+
.query(
23+
`
24+
DO $$
25+
BEGIN
26+
IF NOT EXISTS (
27+
SELECT 1
28+
FROM information_schema.columns
29+
WHERE table_name = '${defaultTableName}'
30+
AND column_name = 'updated_at'
31+
) THEN
32+
ALTER TABLE "${defaultTableName}"
33+
ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
34+
END IF;
35+
END
36+
$$
37+
`
38+
)
39+
.then(() => {
40+
logger.info('Migration Complete');
41+
});
42+
p.end();
43+
logger.info('Migration Client disconnected');
44+
}
45+
946
export const setUpPersistence = async () => {
1047
const pgdb = await PostgresqlPersistence.build(dbConfig);
1148
setPersistence({
@@ -34,4 +71,6 @@ export const setUpPersistence = async () => {
3471
});
3572
},
3673
});
74+
75+
await migrateTable();
3776
};

package-lock.json

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)