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 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" ,
Original file line number Diff line number Diff line change 1+ import { Pool } from 'pg' ;
12import { PostgresqlPersistence } from 'y-postgresql' ;
23import * as Y from 'yjs' ;
34
45import { dbConfig } from '@/config' ;
6+ import { logger } from '@/lib/utils' ;
57import type { IWSSharedDoc } from '@/types/interfaces' ;
68
79import { 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+
946export 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} ;
You can’t perform that action at this time.
0 commit comments