Skip to content

Commit

Permalink
fix: upgrade knex from 2.4.0 to 2.4.2
Browse files Browse the repository at this point in the history
Also fixes new warnings with double rollbacks in transaction blocks
  • Loading branch information
owlas committed Jan 27, 2023
1 parent 141c86f commit a5b35d5
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 298 deletions.
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"graphql": "^16.6.0",
"graphql-request": "^5.0.0",
"js-yaml": "^4.1.0",
"knex": "^2.4.0",
"knex": "^2.4.2",
"moment": "^2.29.4",
"morgan": "^1.10.0",
"nanoid": "^3.1.31",
Expand Down
43 changes: 19 additions & 24 deletions packages/backend/src/models/DashboardModel/DashboardModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,30 +635,25 @@ export class DashboardModel {
dashboards: UpdateMultipleDashboards[],
): Promise<Dashboard[]> {
await this.database.transaction(async (trx) => {
try {
await Promise.all(
dashboards.map(async (dashboard) => {
const withSpaceId = dashboard.spaceUuid
? {
space_id: await getSpaceId(
this.database,
dashboard.spaceUuid,
),
}
: {};
await trx(DashboardsTableName)
.update({
name: dashboard.name,
description: dashboard.description,
...withSpaceId,
})
.where('dashboard_uuid', dashboard.uuid);
}),
);
} catch (e) {
trx.rollback(e);
throw e;
}
await Promise.all(
dashboards.map(async (dashboard) => {
const withSpaceId = dashboard.spaceUuid
? {
space_id: await getSpaceId(
this.database,
dashboard.spaceUuid,
),
}
: {};
await trx(DashboardsTableName)
.update({
name: dashboard.name,
description: dashboard.description,
...withSpaceId,
})
.where('dashboard_uuid', dashboard.uuid);
}),
);
});

return Promise.all(
Expand Down
114 changes: 49 additions & 65 deletions packages/backend/src/models/ProjectModel/ProjectModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,84 +197,68 @@ export class ProjectModel {
throw new NotExistsError('Cannot find organization');
}
return this.database.transaction(async (trx) => {
let encryptedCredentials: Buffer;
try {
let encryptedCredentials: Buffer;
try {
encryptedCredentials = this.encryptionService.encrypt(
JSON.stringify(data.dbtConnection),
);
} catch (e) {
throw new UnexpectedServerError(
'Could not save credentials.',
);
}
const [project] = await trx('projects')
.insert({
name: data.name,
project_type: data.type,
organization_id: orgs[0].organization_id,
dbt_connection_type: data.dbtConnection.type,
dbt_connection: encryptedCredentials,
})
.returning('*');

await this.upsertWarehouseConnection(
trx,
project.project_id,
data.warehouseConnection,
encryptedCredentials = this.encryptionService.encrypt(
JSON.stringify(data.dbtConnection),
);

await trx('spaces').insert({
project_id: project.project_id,
name: 'Shared',
is_private: false,
});

return project.project_uuid;
} catch (e) {
await trx.rollback(e);
throw e;
throw new UnexpectedServerError('Could not save credentials.');
}
const [project] = await trx('projects')
.insert({
name: data.name,
project_type: data.type,
organization_id: orgs[0].organization_id,
dbt_connection_type: data.dbtConnection.type,
dbt_connection: encryptedCredentials,
})
.returning('*');

await this.upsertWarehouseConnection(
trx,
project.project_id,
data.warehouseConnection,
);

await trx('spaces').insert({
project_id: project.project_id,
name: 'Shared',
is_private: false,
});

return project.project_uuid;
});
}

async update(projectUuid: string, data: UpdateProject): Promise<void> {
await this.database.transaction(async (trx) => {
let encryptedCredentials: Buffer;
try {
let encryptedCredentials: Buffer;
try {
encryptedCredentials = this.encryptionService.encrypt(
JSON.stringify(data.dbtConnection),
);
} catch (e) {
throw new UnexpectedServerError(
'Could not save credentials.',
);
}
const projects = await trx('projects')
.update({
name: data.name,
dbt_connection_type: data.dbtConnection.type,
dbt_connection: encryptedCredentials,
})
.where('project_uuid', projectUuid)
.returning('*');
if (projects.length === 0) {
throw new UnexpectedServerError(
'Could not update project.',
);
}
const [project] = projects;

await this.upsertWarehouseConnection(
trx,
project.project_id,
data.warehouseConnection,
encryptedCredentials = this.encryptionService.encrypt(
JSON.stringify(data.dbtConnection),
);
} catch (e) {
await trx.rollback(e);
throw e;
throw new UnexpectedServerError('Could not save credentials.');
}
const projects = await trx('projects')
.update({
name: data.name,
dbt_connection_type: data.dbtConnection.type,
dbt_connection: encryptedCredentials,
})
.where('project_uuid', projectUuid)
.returning('*');
if (projects.length === 0) {
throw new UnexpectedServerError('Could not update project.');
}
const [project] = projects;

await this.upsertWarehouseConnection(
trx,
project.project_id,
data.warehouseConnection,
);
});
}

Expand Down

0 comments on commit a5b35d5

Please sign in to comment.