@@ -26,11 +26,12 @@ import {
2626 retryTxn ,
2727 translateDomain
2828} from '@hcengineering/postgres'
29+ import { type DBDoc } from '@hcengineering/postgres/types/utils'
2930import { getTransactorEndpoint } from '@hcengineering/server-client'
3031import { generateToken } from '@hcengineering/server-token'
3132import { connect } from '@hcengineering/server-tool'
3233import { type MongoClient } from 'mongodb'
33- import { type Pool } from 'pg '
34+ import type postgres from 'postgres '
3435
3536export async function moveFromMongoToPG (
3637 accountDb : AccountDB ,
@@ -64,7 +65,7 @@ export async function moveFromMongoToPG (
6465async function moveWorkspace (
6566 accountDb : AccountDB ,
6667 mongo : MongoClient ,
67- pgClient : Pool ,
68+ pgClient : postgres . Sql ,
6869 ws : Workspace ,
6970 region : string
7071) : Promise < void > {
@@ -84,17 +85,16 @@ async function moveWorkspace (
8485 for ( const collection of collections ) {
8586 const cursor = collection . find ( )
8687 const domain = translateDomain ( collection . collectionName )
87- const current = await pgClient . query ( `SELECT _id FROM ${ domain } WHERE "workspaceId" = $1` , [ ws . workspace ] )
88- const currentIds = new Set ( current . rows . map ( ( r ) => r . _id ) )
88+ const current = await pgClient `SELECT _id FROM ${ pgClient ( domain ) } WHERE "workspaceId" = ${ ws . workspace } `
89+ const currentIds = new Set ( current . map ( ( r ) => r . _id ) )
8990 console . log ( 'move domain' , domain )
9091 const docs : Doc [ ] = [ ]
9192 const fields = getDocFieldsByDomains ( domain )
9293 const filedsWithData = [ ...fields , 'data' ]
93- const insertFields : string [ ] = [ ]
94+ const insertFields : string [ ] = [ 'workspaceId' ]
9495 for ( const field of filedsWithData ) {
95- insertFields . push ( `" ${ field } "` )
96+ insertFields . push ( field )
9697 }
97- const insertStr = insertFields . join ( ', ' )
9898 while ( true ) {
9999 while ( docs . length < 50000 ) {
100100 const doc = ( await cursor . next ( ) ) as Doc | null
@@ -105,35 +105,15 @@ async function moveWorkspace (
105105 if ( docs . length === 0 ) break
106106 while ( docs . length > 0 ) {
107107 const part = docs . splice ( 0 , 500 )
108- const values : any [ ] = [ ]
109- const vars : string [ ] = [ ]
110- let index = 1
108+ const values : DBDoc [ ] = [ ]
111109 for ( let i = 0 ; i < part . length ; i ++ ) {
112110 const doc = part [ i ]
113- const variables : string [ ] = [ ]
114111 const d = convertDoc ( domain , doc , ws . workspace )
115- values . push ( d . workspaceId )
116- variables . push ( `$${ index ++ } ` )
117- for ( const field of fields ) {
118- values . push ( d [ field ] )
119- variables . push ( `$${ index ++ } ` )
120- }
121- values . push ( d . data )
122- variables . push ( `$${ index ++ } ` )
123- vars . push ( `(${ variables . join ( ', ' ) } )` )
124- }
125- const vals = vars . join ( ',' )
126- try {
127- await retryTxn ( pgClient , async ( client ) => {
128- await client . query (
129- `INSERT INTO ${ translateDomain ( domain ) } ("workspaceId", ${ insertStr } ) VALUES ${ vals } ` ,
130- values
131- )
132- } )
133- } catch ( err ) {
134- console . log ( 'error when move doc to' , domain , err )
135- continue
112+ values . push ( d )
136113 }
114+ await retryTxn ( pgClient , async ( client ) => {
115+ await client `INSERT INTO ${ client ( translateDomain ( domain ) ) } ${ client ( values , insertFields ) } `
116+ } )
137117 }
138118 }
139119 }
0 commit comments