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

fix: upgrade knex from 2.4.0 to 2.4.2 #4309

Merged
merged 1 commit into from
Jan 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we want those rollbacks ? are they applied automatically ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, knex handles that for us. This was a bad practice when we started using knex.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right! About half way down this page: https://knexjs.org/guide/transactions.html

And in dev when the error happened it was inside this transaction block. And the knex threw another error (hiding the real one) saying "you're trying to exit a transaction that has already been closed" (or something like that).

This fixed the dev experience. It doesn't seem to happen in production though, which is curious

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