File tree Expand file tree Collapse file tree 3 files changed +139
-0
lines changed
collaboration/src/lib/y-postgres Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change 37
37
"devDependencies" : {
38
38
"@types/express" : " ^4.17.21" ,
39
39
"@types/node" : " ^22.5.5" ,
40
+ "@types/pg" : " ^8.11.10" ,
40
41
"drizzle-kit" : " ^0.24.2" ,
41
42
"nodemon" : " ^3.1.4" ,
42
43
"pino-pretty" : " ^11.2.2" ,
Original file line number Diff line number Diff line change
1
+ import { Pool } from 'pg' ;
1
2
import { PostgresqlPersistence } from 'y-postgresql' ;
2
3
import * as Y from 'yjs' ;
3
4
4
5
import { dbConfig } from '@/config' ;
6
+ import { logger } from '@/lib/utils' ;
5
7
import type { IWSSharedDoc } from '@/types/interfaces' ;
6
8
7
9
import { setPersistence } from './utils' ;
8
10
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
+
9
46
export const setUpPersistence = async ( ) => {
10
47
const pgdb = await PostgresqlPersistence . build ( dbConfig ) ;
11
48
setPersistence ( {
@@ -34,4 +71,6 @@ export const setUpPersistence = async () => {
34
71
} ) ;
35
72
} ,
36
73
} ) ;
74
+
75
+ await migrateTable ( ) ;
37
76
} ;
You can’t perform that action at this time.
0 commit comments