From 7d09078392c951e78b015661418c4cccabe3d767 Mon Sep 17 00:00:00 2001 From: csmig Date: Wed, 4 Nov 2020 18:11:30 -0500 Subject: [PATCH] Migration of v_current_rev --- api/source/service/mysql/migrations/0005.js | 29 ++++++ .../sql/0005/down/10-v-current-rev.sql | 49 ++++++++++ .../sql/0005/up/10-v-current-rev.sql | 89 +++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 api/source/service/mysql/migrations/0005.js create mode 100644 api/source/service/mysql/migrations/sql/0005/down/10-v-current-rev.sql create mode 100644 api/source/service/mysql/migrations/sql/0005/up/10-v-current-rev.sql diff --git a/api/source/service/mysql/migrations/0005.js b/api/source/service/mysql/migrations/0005.js new file mode 100644 index 000000000..d427c0922 --- /dev/null +++ b/api/source/service/mysql/migrations/0005.js @@ -0,0 +1,29 @@ +const Importer = require('./lib/mysql-import.js') +const config = require('../../../utils/config') +const path = require('path') +const fs = require('fs') + +module.exports = { + up: async (pool) => { + let migrationName = path.basename(__filename, '.js') + console.log(`[DB] Running migration ${migrationName} UP`) + const importer = new Importer(pool) + let dir = path.join(__dirname, 'sql', migrationName, 'up') + let files = await fs.promises.readdir(dir) + for (file of files) { + console.log(`[DB] Running MySQL script ${file}...`) + await importer.import(path.join(dir, file)) + } + }, + down: async(pool)=> { + let migrationName = path.basename(__filename, '.js') + console.log(`[DB] Running migration ${migrationName} DOWN`) + const importer = new Importer(pool) + let dir = path.join(__dirname, 'sql', migrationName, 'down') + let files = await fs.promises.readdir(dir) + for (file of files) { + console.log(`[DB] Running MySQL script ${file}...`) + await importer.import(path.join(dir, file)) + } + } +} \ No newline at end of file diff --git a/api/source/service/mysql/migrations/sql/0005/down/10-v-current-rev.sql b/api/source/service/mysql/migrations/sql/0005/down/10-v-current-rev.sql new file mode 100644 index 000000000..e4343c637 --- /dev/null +++ b/api/source/service/mysql/migrations/sql/0005/down/10-v-current-rev.sql @@ -0,0 +1,49 @@ +ALTER VIEW `v_current_rev` AS +select +`rr`.`revId` AS `revId`,`rr`.`benchmarkId` AS `benchmarkId`,`rr`.`version` AS `version`,`rr`.`release` AS `release`,`rr`.`benchmarkDate` AS `benchmarkDate`,`rr`.`benchmarkDateSql` AS `benchmarkDateSql`,`rr`.`status` AS `status`,`rr`.`statusDate` AS `statusDate`,`rr`.`description` AS `description`,`rr`.`active` AS `active`,`rr`.`groupCount` AS `groupCount`,`rr`.`ruleCount` AS `ruleCount`,`rr`.`checkCount` AS `checkCount`,`rr`.`fixCount` AS `fixCount`,`rr`.`ovalCount` AS `ovalCount` from (select `r`.`revId` AS `revId`,`r`.`benchmarkId` AS `benchmarkId`,`r`.`version` AS `version`,`r`.`release` AS `release`,`r`.`benchmarkDate` AS `benchmarkDate`,`r`.`benchmarkDateSql` AS `benchmarkDateSql`,`r`.`status` AS `status`,`r`.`statusDate` AS `statusDate`,`r`.`description` AS `description`,`r`.`active` AS `active`,`r`.`groupCount` AS `groupCount`,`r`.`ruleCount` AS `ruleCount`,`r`.`checkCount` AS `checkCount`,`r`.`fixCount` AS `fixCount`,(select count(`rule_oval_map`.`roId`) from `rule_oval_map` where (`rule_oval_map`.`benchmarkId` = `r`.`benchmarkId`)) AS `ovalCount`,row_number() OVER (PARTITION BY `r`.`benchmarkId` ORDER BY (`r`.`version` + 0) desc,(`r`.`release` + 0) desc ) AS `rn` from `revision` `r` where (`r`.`status` = 'accepted')) `rr` where (`rr`.`rn` = 1); +DELETE from current_rev; +INSERT INTO current_rev ( + revId, + benchmarkId, + `version`, + `release`, + benchmarkDate, + benchmarkDateSql, + status, + statusDate, + description, + active, + groupCount, + ruleCount, + checkCount, + fixCount, + ovalCount) + SELECT + revId, + benchmarkId, + `version`, + `release`, + benchmarkDate, + benchmarkDateSql, + status, + statusDate, + description, + active, + groupCount, + ruleCount, + checkCount, + fixCount, + ovalCount + FROM + v_current_rev; +DELETE FROM current_group_rule; +INSERT INTO current_group_rule (groupId, ruleId, benchmarkId) + SELECT rg.groupId, + rgr.ruleId, + cr.benchmarkId + from + current_rev cr + left join rev_group_map rg on rg.revId=cr.revId + left join rev_group_rule_map rgr on rgr.rgId=rg.rgId + order by + rg.groupId,rgr.ruleId,cr.benchmarkId; diff --git a/api/source/service/mysql/migrations/sql/0005/up/10-v-current-rev.sql b/api/source/service/mysql/migrations/sql/0005/up/10-v-current-rev.sql new file mode 100644 index 000000000..5738eca01 --- /dev/null +++ b/api/source/service/mysql/migrations/sql/0005/up/10-v-current-rev.sql @@ -0,0 +1,89 @@ +ALTER VIEW `v_current_rev` AS +select + `rr`.`revId` AS `revId`, + `rr`.`benchmarkId` AS `benchmarkId`, + `rr`.`version` AS `version`, + `rr`.`release` AS `release`, + `rr`.`benchmarkDate` AS `benchmarkDate`, + `rr`.`benchmarkDateSql` AS `benchmarkDateSql`, + `rr`.`status` AS `status`, + `rr`.`statusDate` AS `statusDate`, + `rr`.`description` AS `description`, + `rr`.`active` AS `active`, + `rr`.`groupCount` AS `groupCount`, + `rr`.`ruleCount` AS `ruleCount`, + `rr`.`checkCount` AS `checkCount`, + `rr`.`fixCount` AS `fixCount`, + `rr`.`ovalCount` AS `ovalCount` + from ( + select + `r`.`revId` AS `revId`, + `r`.`benchmarkId` AS `benchmarkId`, + `r`.`version` AS `version`, + `r`.`release` AS `release`, + `r`.`benchmarkDate` AS `benchmarkDate`, + `r`.`benchmarkDateSql` AS `benchmarkDateSql`, + `r`.`status` AS `status`, + `r`.`statusDate` AS `statusDate`, + `r`.`description` AS `description`, + `r`.`active` AS `active`, + `r`.`groupCount` AS `groupCount`, + `r`.`ruleCount` AS `ruleCount`, + `r`.`checkCount` AS `checkCount`, + `r`.`fixCount` AS `fixCount`, + (select count(`rule_oval_map`.`roId`) from `rule_oval_map` where (`rule_oval_map`.`benchmarkId` = `r`.`benchmarkId`)) AS `ovalCount`, + row_number() OVER ( + PARTITION BY `r`.`benchmarkId` + ORDER BY + FIELD(status, 'draft', 'accepted') desc, + (`r`.`version` + 0) desc, + (`r`.`release` + 0) desc ) AS `rn` + from + `revision` `r`) `rr` where (`rr`.`rn` = 1); + +DELETE from current_rev; +INSERT INTO current_rev ( + revId, + benchmarkId, + `version`, + `release`, + benchmarkDate, + benchmarkDateSql, + status, + statusDate, + description, + active, + groupCount, + ruleCount, + checkCount, + fixCount, + ovalCount) + SELECT + revId, + benchmarkId, + `version`, + `release`, + benchmarkDate, + benchmarkDateSql, + status, + statusDate, + description, + active, + groupCount, + ruleCount, + checkCount, + fixCount, + ovalCount + FROM + v_current_rev; +DELETE FROM current_group_rule; +INSERT INTO current_group_rule (groupId, ruleId, benchmarkId) + SELECT rg.groupId, + rgr.ruleId, + cr.benchmarkId + from + current_rev cr + left join rev_group_map rg on rg.revId=cr.revId + left join rev_group_rule_map rgr on rgr.rgId=rg.rgId + order by + rg.groupId,rgr.ruleId,cr.benchmarkId;