Skip to content

Commit

Permalink
fix: Reset TestPlanVersion nextval during import if IDs aren't used (
Browse files Browse the repository at this point in the history
…#1153)

* Reset TestPlanVersion nextval during import if ids go unused

* Fix migration

* Update id

* Update testPlanVersion test id
  • Loading branch information
howard-e authored Jul 17, 2024
1 parent 6379fd0 commit c913504
Show file tree
Hide file tree
Showing 3 changed files with 488 additions and 439 deletions.
32 changes: 32 additions & 0 deletions server/migrations/20240715150459-resetTestPlanVersionIdToLatest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const { sequelize } = require('../models');
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(async transaction => {
const [latestTestPlanVersion] = await queryInterface.sequelize.query(
`select id
from "TestPlanVersion"
order by id desc
limit 1`,
{
type: Sequelize.QueryTypes.SELECT,
transaction
}
);

if (latestTestPlanVersion) {
await sequelize.query(
`SELECT setval(pg_get_serial_sequence('"TestPlanVersion"', 'id'), :currentSequenceValue)`,
{
replacements: { currentSequenceValue: latestTestPlanVersion.id },
transaction
}
);
}
});
},

async down() {}
};
25 changes: 21 additions & 4 deletions server/scripts/import-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,21 @@ const buildTestsAndCreateTestPlanVersions = async (commit, { transaction }) => {
// Gets the next ID and increments the ID counter in Postgres
// Needed to create the testIds - see LocationOfDataId.js for more info
const [testPlanVersionIdResult] = await sequelize.query(
`SELECT nextval(
pg_get_serial_sequence('"TestPlanVersion"', 'id')
)`,
`SELECT nextval(pg_get_serial_sequence('"TestPlanVersion"', 'id'))`,
{ transaction }
);
const testPlanVersionIdResultRow = testPlanVersionIdResult[0];
const testPlanVersionId = testPlanVersionIdResultRow.nextval;

// Get the currently set value to rollback the 'correct' nextval for
// subsequent runs
const [currentTestPlanVersionIdResult] = await sequelize.query(
`SELECT currval(pg_get_serial_sequence('"TestPlanVersion"', 'id'))`,
{ transaction }
);
const currentTestPlanVersionId =
currentTestPlanVersionIdResult[0].currval - 1;

// Target the specific /tests/<pattern> directory to determine when a pattern's folder was
// actually last changed
const {
Expand Down Expand Up @@ -180,7 +187,17 @@ const buildTestsAndCreateTestPlanVersions = async (commit, { transaction }) => {
transaction
});

if (existing.length) continue;
if (existing.length) {
// Rollback the sequence to avoid unintentional id jumps (potentially 35+)
await sequelize.query(
`SELECT setval(pg_get_serial_sequence('"TestPlanVersion"', 'id'), :currentTestPlanVersionId)`,
{
replacements: { currentTestPlanVersionId },
transaction
}
);
continue;
}

const { title, exampleUrl, designPatternUrl, testPageUrl } = readCsv({
sourceDirectoryPath,
Expand Down
Loading

0 comments on commit c913504

Please sign in to comment.