Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New pg driver #7049

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions dev/tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"@types/request": "~2.48.8",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"@types/jest": "^29.5.5",
"@types/pg": "^8.11.6"
"@types/jest": "^29.5.5"
},
"dependencies": {
"@elastic/elasticsearch": "^7.14.0",
Expand Down Expand Up @@ -157,7 +156,7 @@
"libphonenumber-js": "^1.9.46",
"mime-types": "~2.1.34",
"mongodb": "6.9.0-dev.20241016.sha.3d5bd513",
"pg": "8.12.0",
"postgres": "^3.4.4",
"ws": "^8.18.0"
}
}
44 changes: 12 additions & 32 deletions dev/tool/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import {
retryTxn,
translateDomain
} from '@hcengineering/postgres'
import { type DBDoc } from '@hcengineering/postgres/types/utils'
import { getTransactorEndpoint } from '@hcengineering/server-client'
import { generateToken } from '@hcengineering/server-token'
import { connect } from '@hcengineering/server-tool'
import { type MongoClient } from 'mongodb'
import { type Pool } from 'pg'
import type postgres from 'postgres'

export async function moveFromMongoToPG (
accountDb: AccountDB,
Expand Down Expand Up @@ -64,7 +65,7 @@ export async function moveFromMongoToPG (
async function moveWorkspace (
accountDb: AccountDB,
mongo: MongoClient,
pgClient: Pool,
pgClient: postgres.Sql,
ws: Workspace,
region: string
): Promise<void> {
Expand All @@ -84,17 +85,16 @@ async function moveWorkspace (
for (const collection of collections) {
const cursor = collection.find()
const domain = translateDomain(collection.collectionName)
const current = await pgClient.query(`SELECT _id FROM ${domain} WHERE "workspaceId" = $1`, [ws.workspace])
const currentIds = new Set(current.rows.map((r) => r._id))
const current = await pgClient`SELECT _id FROM ${pgClient(domain)} WHERE "workspaceId" = ${ws.workspace}`
const currentIds = new Set(current.map((r) => r._id))
console.log('move domain', domain)
const docs: Doc[] = []
const fields = getDocFieldsByDomains(domain)
const filedsWithData = [...fields, 'data']
const insertFields: string[] = []
const insertFields: string[] = ['workspaceId']
for (const field of filedsWithData) {
insertFields.push(`"${field}"`)
insertFields.push(field)
}
const insertStr = insertFields.join(', ')
while (true) {
while (docs.length < 50000) {
const doc = (await cursor.next()) as Doc | null
Expand All @@ -105,35 +105,15 @@ async function moveWorkspace (
if (docs.length === 0) break
while (docs.length > 0) {
const part = docs.splice(0, 500)
const values: any[] = []
const vars: string[] = []
let index = 1
const values: DBDoc[] = []
for (let i = 0; i < part.length; i++) {
const doc = part[i]
const variables: string[] = []
const d = convertDoc(domain, doc, ws.workspace)
values.push(d.workspaceId)
variables.push(`$${index++}`)
for (const field of fields) {
values.push(d[field])
variables.push(`$${index++}`)
}
values.push(d.data)
variables.push(`$${index++}`)
vars.push(`(${variables.join(', ')})`)
}
const vals = vars.join(',')
try {
await retryTxn(pgClient, async (client) => {
await client.query(
`INSERT INTO ${translateDomain(domain)} ("workspaceId", ${insertStr}) VALUES ${vals}`,
values
)
})
} catch (err) {
console.log('error when move doc to', domain, err)
continue
values.push(d)
}
await retryTxn(pgClient, async (client) => {
await client`INSERT INTO ${client(translateDomain(domain))} ${client(values, insertFields)}`
})
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions server/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"@types/jest": "^29.5.5",
"@types/otp-generator": "^4.0.2",
"@types/pg": "^8.11.6"
"@types/otp-generator": "^4.0.2"
},
"dependencies": {
"@hcengineering/mongo": "^0.6.1",
"@hcengineering/postgres": "^0.6.0",
"mongodb": "6.9.0-dev.20241016.sha.3d5bd513",
"pg": "8.12.0",
"postgres": "^3.4.4",
"@hcengineering/platform": "^0.6.11",
"@hcengineering/core": "^0.6.32",
"@hcengineering/contact": "^0.6.24",
Expand Down
Loading