Skip to content

Commit

Permalink
fix: Update incorrectly set deprecation dates for test plan versions (#…
Browse files Browse the repository at this point in the history
…1122)

Address #966

---------

Co-authored-by: Howard Edwards <[email protected]>
  • Loading branch information
Paul-Clue and howard-e authored Jun 5, 2024
1 parent 347bf07 commit d077bb5
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 3 deletions.
120 changes: 120 additions & 0 deletions server/migrations/20240522032230-fixIncorrectDeprecationDates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
const SECOND = 1000;
const MINUTE = 60 * SECOND;

const versionAndRangeCheck = async (
deprecatedVersion,
maybeDeprecatingVersion,
transaction
) => {
let relevantPhaseDateFound = false;
let maybeDeprecatingVersionPhaseDate =
maybeDeprecatingVersion.updatedAt;

if (deprecatedVersion.recommendedPhaseReachedAt) {
if (!maybeDeprecatingVersion.recommendedPhaseReachedAt) return;
maybeDeprecatingVersionPhaseDate =
maybeDeprecatingVersion.recommendedPhaseReachedAt;
relevantPhaseDateFound = true;
}

if (
deprecatedVersion.candidatePhaseReachedAt &&
!relevantPhaseDateFound
) {
if (!maybeDeprecatingVersion.candidatePhaseReachedAt) return;
maybeDeprecatingVersionPhaseDate =
maybeDeprecatingVersion.candidatePhaseReachedAt;
relevantPhaseDateFound = true;
}

if (
deprecatedVersion.draftPhaseReachedAt &&
!relevantPhaseDateFound
) {
if (!maybeDeprecatingVersion.draftPhaseReachedAt) return;
maybeDeprecatingVersionPhaseDate =
maybeDeprecatingVersion.draftPhaseReachedAt;
}

// Get a 5-minute range around deprecated version to compare to other
// versions which could have potentially deprecated it based on
// their phase change dates
const deprecatedAtDate = new Date(deprecatedVersion.deprecatedAt);
const startDate = new Date(deprecatedAtDate.getTime() - 5 * MINUTE);
const endDate = new Date(deprecatedAtDate.getTime() + 5 * MINUTE);

if (
maybeDeprecatingVersionPhaseDate.getTime() >=
startDate.getTime() &&
maybeDeprecatingVersionPhaseDate.getTime() <= endDate.getTime()
) {
// Set updated deprecatedAt date as being 2 seconds before
// whichever dated phase change caused it; exactly how
// updatePhaseResolver and import-tests is done
const updatedDeprecatedAt = new Date(
new Date(
maybeDeprecatingVersionPhaseDate.getTime() - 2 * SECOND
)
);

await queryInterface.sequelize.query(
`update "TestPlanVersion"
set "deprecatedAt" = ?
where id = ?
and "gitSha" = ?
and directory = ?`,
{
replacements: [
updatedDeprecatedAt,
deprecatedVersion.id,
deprecatedVersion.gitSha,
deprecatedVersion.directory
],
transaction
}
);
}
};

return queryInterface.sequelize.transaction(async transaction => {
const testPlanVersions = await queryInterface.sequelize.query(
`select "id",
"phase",
"gitSha",
"directory",
"updatedAt",
"draftPhaseReachedAt",
"candidatePhaseReachedAt",
"recommendedPhaseReachedAt",
"deprecatedAt"
from "TestPlanVersion"`,
{ type: Sequelize.QueryTypes.SELECT, transaction }
);

const deprecatedVersions = testPlanVersions.filter(
({ phase }) => phase === 'DEPRECATED'
);

for (let deprecatedVersion of deprecatedVersions) {
const maybeDeprecatingVersions = testPlanVersions.filter(
({ id, directory }) =>
id !== deprecatedVersion.id &&
directory === deprecatedVersion.directory
);

for (let maybeDeprecatingVersion of maybeDeprecatingVersions) {
await versionAndRangeCheck(
deprecatedVersion,
maybeDeprecatingVersion,
transaction
);
}
}
});
}
};
15 changes: 13 additions & 2 deletions server/resolvers/TestPlanVersionOperations/updatePhaseResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,24 @@ const updatePhaseResolver = async (
// If oldTestPlanVersion's results are being used to update this earlier
// version, deprecate it (if the same phase)
if (oldTestPlanVersion && phase === oldTestPlanVersion.phase) {
// Set deprecation date to happen 2 minutes before the new version being
// updated
let deprecatedAt;
if (updateParams.draftPhaseReachedAt)
deprecatedAt = new Date(updateParams.draftPhaseReachedAt);
else if (updateParams.candidatePhaseReachedAt)
deprecatedAt = new Date(updateParams.candidatePhaseReachedAt);
else deprecatedAt = new Date(updateParams.recommendedPhaseReachedAt);

if (deprecatedAt)
deprecatedAt.setSeconds(deprecatedAt.getSeconds() - 120);

await updateTestPlanVersionById({
id: oldTestPlanVersion.id, // same as testPlanVersionDataToIncludeId
values: { phase: 'DEPRECATED', deprecatedAt: new Date() },
values: { phase: 'DEPRECATED', deprecatedAt },
transaction
});
}

await updateTestPlanVersionById({
id: testPlanVersionId,
values: updateParams,
Expand Down
2 changes: 1 addition & 1 deletion server/scripts/import-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ const buildTestsAndCreateTestPlanVersions = async (commit, { transaction }) => {
// Deprecations happen slightly before update during normal app operations.
// This is to maintain correctness and any app sorts issues
const deprecatedAt = new Date(updatedAt);
deprecatedAt.setSeconds(deprecatedAt.getSeconds() - 60);
deprecatedAt.setSeconds(deprecatedAt.getSeconds() - 120);
await updateTestPlanVersionById({
id: testPlanVersionToDeprecate.id,
values: { phase: 'DEPRECATED', deprecatedAt },
Expand Down

0 comments on commit d077bb5

Please sign in to comment.