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

[OTE-308] Update upsert querybuilder and compliance table upsert function (backport #1477) #1478

Merged
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
5 changes: 2 additions & 3 deletions indexer/packages/postgres/src/models/compliance-data-model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Model } from 'objection';

import { NumericPattern } from '../lib/validators';
import UpsertQueryBuilder from '../query-builders/upsert';
import { ComplianceProvider, IsoString } from '../types';
import BaseModel from './base-model';

export default class ComplianceDataModel extends Model {
export default class ComplianceDataModel extends BaseModel {
static get tableName() {
return 'compliance_data';
}
Expand Down
15 changes: 9 additions & 6 deletions indexer/packages/postgres/src/query-builders/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ export default class UpsertQueryBuilder<M extends Model, R = M[]> extends QueryB
upsert(object: any) {
const modelClass = this.modelClass();

const idColumn: string = modelClass.idColumn as string;

const tableDefinedId = `${modelClass.tableName}.${idColumn}`;
const idColumn = modelClass.idColumn;
const idColumns: string[] = Array.isArray(idColumn) ? idColumn : [idColumn];

const knex = modelClass.knex();

Expand All @@ -26,11 +25,16 @@ export default class UpsertQueryBuilder<M extends Model, R = M[]> extends QueryB
const colBindings = cols.map(() => '??').join(', ');
const valBindings = cols.map(() => '?').join(', ');
const setBindings = cols.map(() => '?? = ?').join(', ');
const idConditionBindings = idColumns.map(() => '?? = ?').join(' AND ');

const setValues: string[] = [];
for (let i = 0; i < cols.length; ++i) {
setValues.push(cols[i], values[i]);
}
const idValues: string[] = [];
for (let i = 0; i < idColumns.length; ++i) {
idValues.push(`${modelClass.tableName}.${idColumns[i]}`, object[idColumns[i]]);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return this.onBuildKnex((query: any) => {
Expand All @@ -40,15 +44,14 @@ export default class UpsertQueryBuilder<M extends Model, R = M[]> extends QueryB
`(${colBindings}) VALUES (${valBindings})`,
'ON CONFLICT (??) DO',
`UPDATE SET ${setBindings}`,
'WHERE ?? = ?',
`WHERE ${idConditionBindings}`,
].join(' '),
[
...cols,
...values,
modelClass.idColumn,
...setValues,
tableDefinedId,
object[idColumn],
...idValues,
],
),
);
Expand Down
22 changes: 4 additions & 18 deletions indexer/packages/postgres/src/stores/compliance-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,11 @@ export async function upsert(
complianceDataToUpsert: ComplianceDataCreateObject,
options: Options = { txId: undefined },
): Promise<ComplianceDataFromDatabase> {
const complianceData: ComplianceDataFromDatabase | undefined = await findByAddressAndProvider(
complianceDataToUpsert.address,
complianceDataToUpsert.provider,
);
if (complianceData === undefined) {
return create({
...complianceDataToUpsert,
}, options);
}

const updatedComplianceData: ComplianceDataFromDatabase | undefined = await update({
...complianceDataToUpsert,
}, options);

if (updatedComplianceData === undefined) {
throw Error('order must exist after update');
}
const updatedComplianceData: ComplianceDataModel[] = await ComplianceDataModel.query(
Transaction.get(options.txId),
).upsert(complianceDataToUpsert).returning('*');

return updatedComplianceData;
return updatedComplianceData[0];
}

export async function findByAddressAndProvider(
Expand Down
Loading