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

chores(deps): upgrade sequelize to 6.35.2 #982

Merged
merged 2 commits into from
Dec 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
"test:quick": "npm run test:typecheck && npm run test:lint && npm run test:unit -- --testPathIgnorePatterns=\"cli.test.js\"",
"test:typecheck": "tsc -p .",
"test:lint": "npm run test:lint:js && npm run test:lint:jsx && npm run test:lint:tests",
"test:lint:fix": "npm run test:lint:js --fix && npm run test:lint:jsx --fix && npm run test:lint:tests --fix",
"test:lint:fix": "npm run test:lint:js -- --fix && npm run test:lint:jsx -- --fix && npm run test:lint:tests -- --fix",
"test:lint:js": "eslint ./packages/*/src",
"test:lint:jsx": "eslint ./packages/server/src/ui ./packages/viewer/src/ui",
"test:lint:tests": "eslint ./packages/*/test",
"test:docker": "bash scripts/test-docker.sh",
"test:unit": "jest --maxWorkers=2"
"test:unit": "jest"
},
"devDependencies": {
"@chialab/esbuild-plugin-html": "^0.17.2",
Expand All @@ -49,7 +49,6 @@
"@types/morgan": "^1.7.35",
"@types/node": "^11.13.2",
"@types/plotly.js": "^1.44.9",
"@types/sequelize": "^4.27.34",
"@types/tmp": "^0.1.0",
"@types/umzug": "^2.2.2",
"@types/uuid": "^8.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"express": "^4.16.4",
"express-basic-auth": "^1.2.0",
"morgan": "^1.9.1",
"sequelize": "^4.44.3",
"sequelize": "^6.35.2",
"umzug": "^2.2.0",
"uuid": "^8.3.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = {
buildId: {type: Sequelize.UUID()},
url: {type: Sequelize.STRING({length: 256})},
name: {type: Sequelize.STRING({length: 100})},
value: {type: Sequelize.NUMERIC(12, 4)},
value: {type: Sequelize.DECIMAL(12, 4)},
createdAt: {type: Sequelize.DATE(6)},
updatedAt: {type: Sequelize.DATE(6)},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
* @param {typeof import('sequelize')} Sequelize
*/
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('statistics', 'version', {type: Sequelize.NUMERIC(8, 2)});
await queryInterface.addColumn('statistics', 'version', {type: Sequelize.DECIMAL(8, 2)});
await queryInterface.bulkUpdate(
'statistics',
{version: 1},
Expand Down
108 changes: 68 additions & 40 deletions packages/server/src/api/storage/sql/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const logVerbose = require('debug')('lhci:server:sql:verbose');
const path = require('path');
const uuid = require('uuid');
const Umzug = require('umzug');
const Sequelize = require('sequelize');
const {Sequelize, Op} = require('sequelize');
const {omit, padEnd} = require('@lhci/utils/src/lodash.js');
const {hashAdminToken, generateAdminToken} = require('../auth.js');
const {E422} = require('../../express-utils.js');
Expand Down Expand Up @@ -82,7 +82,6 @@ function createSequelize(options) {
const sequelizeOptions = {
logging: /** @param {*} msg */ msg => logVerbose('[sequelize]', msg),
...options.sequelizeOptions,
operatorsAliases: false,
};

if (dialect === 'sqlite') {
Expand Down Expand Up @@ -163,13 +162,16 @@ function normalizeStatistic(statistic) {
/**
* @typedef SqlState
* @property {import('sequelize').Sequelize} sequelize
* @property {import('sequelize').Model<LHCI.ServerCommand.Project, ProjectAttrs>} projectModel
* @property {import('sequelize').Model<LHCI.ServerCommand.Build, BuildAttrs>} buildModel
* @property {import('sequelize').Model<LHCI.ServerCommand.Run, RunAttrs>} runModel
* @property {import('sequelize').Model<LHCI.ServerCommand.Statistic, StatisticAttrs>} statisticModel
* @property {import('sequelize').ModelDefined<LHCI.ServerCommand.Project, ProjectAttrs>} projectModel
* @property {import('sequelize').ModelDefined<LHCI.ServerCommand.Build, BuildAttrs>} buildModel
* @property {import('sequelize').ModelDefined<LHCI.ServerCommand.Run, RunAttrs>} runModel
* @property {import('sequelize').ModelDefined<LHCI.ServerCommand.Statistic, StatisticAttrs>} statisticModel
*/

/** Sort all records by most recently created */
/**
* Sort all records by most recently created
* @type {import("sequelize").Order}
*/
const order = [['createdAt', 'desc']];

class SqlStorageMethod {
Expand All @@ -188,20 +190,44 @@ class SqlStorageMethod {
}

/**
* @template T1
* @template T2
* @template {Object} T1
* @template {Object} T2
* @param {import('sequelize').Model<T1, T2>} model
* @return {T1}
*/
_value(model) {
return model.dataValues;
}

/**
* @template {Object} T1
* @template {Object} T2
* @param {import('sequelize').Model<T1, T2> | null} model
* @return {T1 | null}
*/
_valueOrNull(model) {
if (!model) return null;
return model.dataValues;
}

/**
* @template {Object} T1
* @template {Object} T2
* @param {import('sequelize').ModelDefined<T1, T2>} model
* @param {string} pk
* @return {Promise<T1 | null>}
*/
async _findByPk(model, pk) {
return model.findByPk(validateUuidOrEmpty(pk));
const result = await model.findByPk(validateUuidOrEmpty(pk));
return this._valueOrNull(result);
}

/**
* @template T1
* @template T2
* @param {import('sequelize').Model<T1, T2>} model
* @template {Object} T1
* @template {Object} T2
* @param {import('sequelize').ModelDefined<T1, T2>} model
* @param {import('sequelize').FindOptions<T1 & T2>} options
* @return {Promise<T1[]>}
*/
async _findAll(model, options) {
if (options.where) {
Expand All @@ -214,7 +240,8 @@ class SqlStorageMethod {
}
}

return model.findAll(options);
const result = await model.findAll(options);
return result.map(this._value);
}

/**
Expand Down Expand Up @@ -357,7 +384,7 @@ class SqlStorageMethod {

// Replace the adminToken with the original non-hashed version.
// This will be the only time it's readable other than reset.
return {...clone(project), adminToken};
return {...clone(this._value(project)), adminToken};
}

/**
Expand Down Expand Up @@ -427,7 +454,7 @@ class SqlStorageMethod {
if (existingForHash) throw new E422(`Build already exists for hash "${unsavedBuild.hash}"`);

const build = await buildModel.create({...unsavedBuild, id: uuid.v4()});
return clone(build);
return clone(this._value(build));
}

/**
Expand Down Expand Up @@ -459,10 +486,7 @@ class SqlStorageMethod {
const runIds = representativeRuns.map(run => run.id);

log('[sealBuild] updating run representative flag');
await runModel.update(
{representative: true},
{where: {id: {[Sequelize.Op.in]: runIds}}, transaction}
);
await runModel.update({representative: true}, {where: {id: {[Op.in]: runIds}}, transaction});

log('[sealBuild] committing transaction');
await transaction.commit();
Expand All @@ -480,10 +504,10 @@ class SqlStorageMethod {
async findBuildsBeforeTimestamp(runAt) {
const {buildModel} = this._sql();
const oldBuilds = await buildModel.findAll({
where: {runAt: {[Sequelize.Op.lte]: runAt}},
where: {runAt: {[Op.lte]: runAt}},
order: [['runAt', 'ASC']],
});
return oldBuilds;
return oldBuilds.map(this._value);
}

/**
Expand Down Expand Up @@ -541,12 +565,12 @@ class SqlStorageMethod {
const lowerUuid = formatAsUuid(prefix, '0');
const upperUuid = formatAsUuid(prefix, 'f');
const builds = await buildModel.findAll({
where: {id: {[Sequelize.Op.gte]: lowerUuid, [Sequelize.Op.lte]: upperUuid}, projectId},
where: {id: {[Op.gte]: lowerUuid, [Op.lte]: upperUuid}, projectId},
limit: 2,
});

if (builds.length !== 1) return undefined;
return clone(builds[0]);
return clone(this._value(builds[0]));
}

/**
Expand All @@ -572,11 +596,11 @@ class SqlStorageMethod {
const where = {
projectId: build.projectId,
branch: project.baseBranch,
id: {[Sequelize.Op.ne]: build.id},
id: {[Op.ne]: build.id},
};

const nearestBuildBefore = await buildModel.findAll({
where: {...where, runAt: {[Sequelize.Op.lte]: build.runAt}},
const nearestBuildBefore = await this._findAll(buildModel, {
where: {...where, runAt: {[Op.lte]: build.runAt}},
order: [['runAt', 'DESC']],
limit: 1,
});
Expand All @@ -585,8 +609,8 @@ class SqlStorageMethod {
return nearestBuildBefore[0];
}

const nearestBuildAfter = await buildModel.findAll({
where: {...where, runAt: {[Sequelize.Op.gte]: build.runAt}},
const nearestBuildAfter = await this._findAll(buildModel, {
where: {...where, runAt: {[Op.gte]: build.runAt}},
order: [['runAt', 'ASC']],
limit: 1,
});
Expand Down Expand Up @@ -642,7 +666,7 @@ class SqlStorageMethod {
if (unsavedRun.url.length > 256) throw new E422('URL too long');

const run = await runModel.create({...unsavedRun, representative: false, id: uuid.v4()});
return clone(run);
return clone(this._value(run));
}

/**
Expand All @@ -663,15 +687,17 @@ class SqlStorageMethod {
const transaction = context && context.transaction;
const {statisticModel} = this._sql();
logVerbose('[_createOrUpdateStatistic] looking up existing statistic');
const existing = await statisticModel.findOne({
where: {
projectId: unsavedStatistic.projectId,
buildId: unsavedStatistic.buildId,
url: unsavedStatistic.url,
name: unsavedStatistic.name,
},
transaction,
});
const existing = this._valueOrNull(
await statisticModel.findOne({
where: {
projectId: unsavedStatistic.projectId,
buildId: unsavedStatistic.buildId,
url: unsavedStatistic.url,
name: unsavedStatistic.name,
},
transaction,
})
);

/** @type {LHCI.ServerCommand.Statistic} */
let statistic;
Expand All @@ -683,7 +709,9 @@ class SqlStorageMethod {
statistic = updated;
} else {
logVerbose('[_createOrUpdateStatistic] no existing statistic found, creating one');
statistic = await statisticModel.create({...unsavedStatistic, id: uuid.v4()}, {transaction});
statistic = this._value(
await statisticModel.create({...unsavedStatistic, id: uuid.v4()}, {transaction})
);
}

return normalizeStatistic(clone(statistic));
Expand Down
20 changes: 10 additions & 10 deletions packages/server/src/api/storage/sql/statistic-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

/* eslint-disable new-cap */

const Sequelize = require('sequelize');
const {DataTypes} = require('sequelize');

/** @type {import('sequelize').Model<any, any>} */
const ModelRef = /** @type {any} */ (undefined);

/** @type {LHCI.ServerCommand.TableDefinition<LHCI.ServerCommand.Statistic>} */
const attributes = {
id: {type: Sequelize.UUID(), primaryKey: true},
projectId: {type: Sequelize.UUID(), references: {model: ModelRef, key: 'id'}},
buildId: {type: Sequelize.UUID(), references: {model: ModelRef, key: 'id'}},
version: {type: Sequelize.NUMERIC(8, 2)},
url: {type: Sequelize.STRING({length: 256})},
name: {type: Sequelize.STRING({length: 100})},
value: {type: Sequelize.NUMERIC(12, 4)},
createdAt: {type: Sequelize.DATE(6)},
updatedAt: {type: Sequelize.DATE(6)},
id: {type: DataTypes.UUID(), primaryKey: true},
projectId: {type: DataTypes.UUID(), references: {model: ModelRef, key: 'id'}},
buildId: {type: DataTypes.UUID(), references: {model: ModelRef, key: 'id'}},
version: {type: DataTypes.DECIMAL(8, 2)},
url: {type: DataTypes.STRING({length: 256})},
name: {type: DataTypes.STRING({length: 100})},
value: {type: DataTypes.DECIMAL(12, 4)},
createdAt: {type: DataTypes.DATE(6)},
updatedAt: {type: DataTypes.DATE(6)},
};

module.exports = {
Expand Down
Loading
Loading