From c2dc676ecf2a9c205d78260f9ce69713d4f9dafb Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Sun, 10 Nov 2019 17:15:32 -0600 Subject: [PATCH] feat(server): add statistic version --- .../server/src/api/statistic-definitions.js | 2 +- .../2019111001-statistic-version.js | 25 +++++++++++++++++++ packages/server/src/api/storage/sql/sql.js | 2 ++ .../src/api/storage/sql/statistic-model.js | 20 +++++++++------ .../server/src/api/storage/storage-method.js | 6 ++++- types/server.d.ts | 1 + 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 packages/server/src/api/storage/sql/migrations/2019111001-statistic-version.js diff --git a/packages/server/src/api/statistic-definitions.js b/packages/server/src/api/statistic-definitions.js index 187a7c593..4a032b1f7 100644 --- a/packages/server/src/api/statistic-definitions.js +++ b/packages/server/src/api/statistic-definitions.js @@ -63,4 +63,4 @@ const definitions = { }; // Keep the export separate from declaration to enable tsc to typecheck the `@type` annotation. -module.exports = definitions; +module.exports = {definitions, VERSION: 1}; diff --git a/packages/server/src/api/storage/sql/migrations/2019111001-statistic-version.js b/packages/server/src/api/storage/sql/migrations/2019111001-statistic-version.js new file mode 100644 index 000000000..7642a9578 --- /dev/null +++ b/packages/server/src/api/storage/sql/migrations/2019111001-statistic-version.js @@ -0,0 +1,25 @@ +/** + * @license Copyright 2019 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +/* eslint-disable new-cap */ + +module.exports = { + /** + * @param {import('sequelize').QueryInterface} queryInterface + * @param {typeof import('sequelize')} Sequelize + */ + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn('statistics', 'version', {type: Sequelize.NUMERIC(8, 2)}); + await queryInterface.bulkUpdate('statistics', {version: 1}, {version: null}); + }, + /** + * @param {import('sequelize').QueryInterface} queryInterface + */ + down: async queryInterface => { + await queryInterface.removeColumn('statistics', 'version'); + }, +}; diff --git a/packages/server/src/api/storage/sql/sql.js b/packages/server/src/api/storage/sql/sql.js index 899a68125..867d0349d 100644 --- a/packages/server/src/api/storage/sql/sql.js +++ b/packages/server/src/api/storage/sql/sql.js @@ -184,6 +184,8 @@ class SqlStorageMethod { if (!buildModelDefn.attributes.projectId.references) throw new Error('Invalid buildModel'); if (!runModelDefn.attributes.projectId.references) throw new Error('Invalid runModel'); if (!runModelDefn.attributes.buildId.references) throw new Error('Invalid runModel'); + if (!statisticModelDefn.attributes.projectId.references) throw new Error('Invalid runModel'); + if (!statisticModelDefn.attributes.buildId.references) throw new Error('Invalid runModel'); const sequelize = createSequelize(options); diff --git a/packages/server/src/api/storage/sql/statistic-model.js b/packages/server/src/api/storage/sql/statistic-model.js index 8558f5416..5202ad87f 100644 --- a/packages/server/src/api/storage/sql/statistic-model.js +++ b/packages/server/src/api/storage/sql/statistic-model.js @@ -12,15 +12,19 @@ const Sequelize = require('sequelize'); /** @type {import('sequelize').Model} */ const ModelRef = /** @type {any} */ (undefined); +/** @type {LHCI.ServerCommand.TableDefinition} */ +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)}, +}; + module.exports = { tableName: 'statistics', - 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'}}, - url: {type: Sequelize.STRING({length: 256})}, - name: {type: Sequelize.STRING({length: 100})}, - value: {type: Sequelize.NUMERIC(12, 4)}, - }, + attributes, indexes: [], }; diff --git a/packages/server/src/api/storage/storage-method.js b/packages/server/src/api/storage/storage-method.js index c5e410495..a728bea8d 100644 --- a/packages/server/src/api/storage/storage-method.js +++ b/packages/server/src/api/storage/storage-method.js @@ -8,7 +8,10 @@ const _ = require('@lhci/utils/src/lodash.js'); const PRandom = require('@lhci/utils/src/seed-data/prandom.js'); const {computeRepresentativeRuns} = require('@lhci/utils/src/representative-runs.js'); -const statisticDefinitions = require('../statistic-definitions.js'); +const { + definitions: statisticDefinitions, + VERSION: STATISTIC_VERSION, +} = require('../statistic-definitions.js'); class StorageMethod { /** @@ -254,6 +257,7 @@ class StorageMethod { projectId, buildId, url, + version: STATISTIC_VERSION, name, value, }, diff --git a/types/server.d.ts b/types/server.d.ts index a8ad2a16e..cffd838b3 100644 --- a/types/server.d.ts +++ b/types/server.d.ts @@ -68,6 +68,7 @@ declare global { id: string; projectId: string; buildId: string; + version: number; url: string; name: StatisticName; value: number;