Skip to content

Commit 2f4f15e

Browse files
authored
New pg driver (#7049)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
1 parent be8903a commit 2f4f15e

File tree

11 files changed

+426
-461
lines changed

11 files changed

+426
-461
lines changed

common/config/rush/pnpm-lock.yaml

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

dev/tool/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
"@types/request": "~2.48.8",
5353
"jest": "^29.7.0",
5454
"ts-jest": "^29.1.1",
55-
"@types/jest": "^29.5.5",
56-
"@types/pg": "^8.11.6"
55+
"@types/jest": "^29.5.5"
5756
},
5857
"dependencies": {
5958
"@elastic/elasticsearch": "^7.14.0",
@@ -157,7 +156,7 @@
157156
"libphonenumber-js": "^1.9.46",
158157
"mime-types": "~2.1.34",
159158
"mongodb": "6.9.0-dev.20241016.sha.3d5bd513",
160-
"pg": "8.12.0",
159+
"postgres": "^3.4.4",
161160
"ws": "^8.18.0"
162161
}
163162
}

dev/tool/src/db.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import {
2626
retryTxn,
2727
translateDomain
2828
} from '@hcengineering/postgres'
29+
import { type DBDoc } from '@hcengineering/postgres/types/utils'
2930
import { getTransactorEndpoint } from '@hcengineering/server-client'
3031
import { generateToken } from '@hcengineering/server-token'
3132
import { connect } from '@hcengineering/server-tool'
3233
import { type MongoClient } from 'mongodb'
33-
import { type Pool } from 'pg'
34+
import type postgres from 'postgres'
3435

3536
export async function moveFromMongoToPG (
3637
accountDb: AccountDB,
@@ -64,7 +65,7 @@ export async function moveFromMongoToPG (
6465
async 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
}

server/account/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535
"jest": "^29.7.0",
3636
"ts-jest": "^29.1.1",
3737
"@types/jest": "^29.5.5",
38-
"@types/otp-generator": "^4.0.2",
39-
"@types/pg": "^8.11.6"
38+
"@types/otp-generator": "^4.0.2"
4039
},
4140
"dependencies": {
4241
"@hcengineering/mongo": "^0.6.1",
4342
"@hcengineering/postgres": "^0.6.0",
4443
"mongodb": "6.9.0-dev.20241016.sha.3d5bd513",
45-
"pg": "8.12.0",
44+
"postgres": "^3.4.4",
4645
"@hcengineering/platform": "^0.6.11",
4746
"@hcengineering/core": "^0.6.32",
4847
"@hcengineering/contact": "^0.6.24",

0 commit comments

Comments
 (0)