Skip to content

Commit

Permalink
Migration of v_current_rev
Browse files Browse the repository at this point in the history
  • Loading branch information
csmig committed Nov 5, 2020
1 parent 82360d1 commit 7d09078
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
29 changes: 29 additions & 0 deletions api/source/service/mysql/migrations/0005.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7d09078

Please sign in to comment.