mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-22 19:11:33 +03:00
Qfix: empty where clause (#6793)
Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
parent
09e3e21068
commit
7441fa146d
@ -140,6 +140,7 @@ export async function moveAccountDbFromMongoToPG (
|
|||||||
mongoDb: AccountDB,
|
mongoDb: AccountDB,
|
||||||
pgDb: AccountDB
|
pgDb: AccountDB
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
// [accountId, workspaceId]
|
||||||
const workspaceAssignments: [ObjectId, ObjectId][] = []
|
const workspaceAssignments: [ObjectId, ObjectId][] = []
|
||||||
const accounts = await listAccounts(mongoDb)
|
const accounts = await listAccounts(mongoDb)
|
||||||
const workspaces = await listWorkspacesPure(mongoDb)
|
const workspaces = await listWorkspacesPure(mongoDb)
|
||||||
@ -153,14 +154,18 @@ export async function moveAccountDbFromMongoToPG (
|
|||||||
|
|
||||||
delete (pgAccount as any).workspaces
|
delete (pgAccount as any).workspaces
|
||||||
|
|
||||||
const exists = await getAccount(pgDb, pgAccount.email)
|
if (pgAccount.createdOn === undefined) {
|
||||||
if (exists === null) {
|
pgAccount.createdOn = Date.now()
|
||||||
await pgDb.account.insertOne(pgAccount)
|
}
|
||||||
ctx.info('Moved account', { email: pgAccount.email })
|
|
||||||
|
|
||||||
for (const workspace of mongoAccount.workspaces) {
|
for (const workspace of mongoAccount.workspaces) {
|
||||||
workspaceAssignments.push([pgAccount._id, workspace.toString()])
|
workspaceAssignments.push([pgAccount._id, workspace.toString()])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const exists = await getAccount(pgDb, pgAccount.email)
|
||||||
|
if (exists === null) {
|
||||||
|
await pgDb.account.insertOne(pgAccount)
|
||||||
|
ctx.info('Moved account', { email: pgAccount.email })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,9 +207,19 @@ export async function moveAccountDbFromMongoToPG (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (workspaceAssignments.length > 0) {
|
const pgAssignments = (await listAccounts(pgDb)).reduce<Record<ObjectId, ObjectId[]>>((assignments, acc) => {
|
||||||
for (const [accountId, workspaceId] of workspaceAssignments) {
|
assignments[acc._id] = acc.workspaces
|
||||||
|
|
||||||
|
return assignments
|
||||||
|
}, {})
|
||||||
|
const assignmentsToInsert = workspaceAssignments.filter(
|
||||||
|
([accountId, workspaceId]) =>
|
||||||
|
pgAssignments[accountId] === undefined || !pgAssignments[accountId].includes(workspaceId)
|
||||||
|
)
|
||||||
|
|
||||||
|
for (const [accountId, workspaceId] of assignmentsToInsert) {
|
||||||
await pgDb.assignWorkspace(accountId, workspaceId)
|
await pgDb.assignWorkspace(accountId, workspaceId)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
ctx.info('Assignments made', { count: assignmentsToInsert.length })
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,10 @@ export abstract class PostgresDbCollection<T extends Record<string, any>> implem
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected buildWhereClause (query: Query<T>, lastRefIdx: number = 0): [string, any[]] {
|
protected buildWhereClause (query: Query<T>, lastRefIdx: number = 0): [string, any[]] {
|
||||||
|
if (Object.keys(query).length === 0) {
|
||||||
|
return ['', []]
|
||||||
|
}
|
||||||
|
|
||||||
const whereChunks: string[] = []
|
const whereChunks: string[] = []
|
||||||
const values: any[] = []
|
const values: any[] = []
|
||||||
let currIdx: number = lastRefIdx
|
let currIdx: number = lastRefIdx
|
||||||
@ -131,7 +135,9 @@ export abstract class PostgresDbCollection<T extends Record<string, any>> implem
|
|||||||
const sqlChunks: string[] = [this.buildSelectClause()]
|
const sqlChunks: string[] = [this.buildSelectClause()]
|
||||||
const [whereClause, whereValues] = this.buildWhereClause(query)
|
const [whereClause, whereValues] = this.buildWhereClause(query)
|
||||||
|
|
||||||
|
if (whereClause !== '') {
|
||||||
sqlChunks.push(whereClause)
|
sqlChunks.push(whereClause)
|
||||||
|
}
|
||||||
|
|
||||||
if (sort !== undefined) {
|
if (sort !== undefined) {
|
||||||
sqlChunks.push(this.buildSortClause(sort))
|
sqlChunks.push(this.buildSortClause(sort))
|
||||||
@ -200,7 +206,9 @@ export abstract class PostgresDbCollection<T extends Record<string, any>> implem
|
|||||||
const [whereClause, whereValues] = this.buildWhereClause(query, updateValues.length)
|
const [whereClause, whereValues] = this.buildWhereClause(query, updateValues.length)
|
||||||
|
|
||||||
sqlChunks.push(updateClause)
|
sqlChunks.push(updateClause)
|
||||||
|
if (whereClause !== '') {
|
||||||
sqlChunks.push(whereClause)
|
sqlChunks.push(whereClause)
|
||||||
|
}
|
||||||
|
|
||||||
const finalSql = sqlChunks.join(' ')
|
const finalSql = sqlChunks.join(' ')
|
||||||
await this.client.query(finalSql, [...updateValues, ...whereValues])
|
await this.client.query(finalSql, [...updateValues, ...whereValues])
|
||||||
@ -210,7 +218,9 @@ export abstract class PostgresDbCollection<T extends Record<string, any>> implem
|
|||||||
const sqlChunks: string[] = [`DELETE FROM ${this.name}`]
|
const sqlChunks: string[] = [`DELETE FROM ${this.name}`]
|
||||||
const [whereClause, whereValues] = this.buildWhereClause(query)
|
const [whereClause, whereValues] = this.buildWhereClause(query)
|
||||||
|
|
||||||
|
if (whereClause !== '') {
|
||||||
sqlChunks.push(whereClause)
|
sqlChunks.push(whereClause)
|
||||||
|
}
|
||||||
|
|
||||||
const finalSql = sqlChunks.join(' ')
|
const finalSql = sqlChunks.join(' ')
|
||||||
await this.client.query(finalSql, whereValues)
|
await this.client.query(finalSql, whereValues)
|
||||||
|
Loading…
Reference in New Issue
Block a user