Skip to content

Commit

Permalink
Merge pull request #3630 from uselagoon/feature/cleanup_insights
Browse files Browse the repository at this point in the history
Clean up insights data on environment deletion
  • Loading branch information
shreddedbacon authored Jan 8, 2024
2 parents e4ce695 + 915f381 commit 92af324
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return Promise.all([
knex.schema.raw(`
DELETE ef
FROM environment_fact ef
LEFT JOIN environment e ON ef.environment = e.id
WHERE e.id IS NULL OR e.deleted != '0000-00-00 00:00:00' OR e.project not in (select id from project)
`),
knex.schema.raw(`
DELETE ep
FROM environment_problem ep
LEFT JOIN environment e ON ep.environment = e.id
WHERE e.id IS NULL OR e.deleted != '0000-00-00 00:00:00' OR e.project not in (select id from project)
`),
]);
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema;
};
15 changes: 15 additions & 0 deletions services/api/src/resources/environment/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Pool } from 'mariadb';
import { asyncPipe } from '@lagoon/commons/dist/util/func';
import { query } from '../../util/db';
import { Sql } from './sql';
import { Sql as problemSql } from '../problem/sql';
import { Sql as factSql } from '../fact/sql';
import { Helpers as projectHelpers } from '../project/helpers';
// import { logger } from '../../loggers/logger';

Expand Down Expand Up @@ -48,6 +50,19 @@ export const Helpers = (sqlClientPool: Pool) => {
sqlClientPool,
Sql.deleteEnvironment(name, pid)
);

// Here we clean up insights attached to the environment

await query(
sqlClientPool,
factSql.deleteFactsForEnvironment(eid)
);

await query(
sqlClientPool,
problemSql.deleteProblemsForEnvironment(eid)
);

},
getEnvironmentsDeploytarget: async (eid) => {
const rows = await query(
Expand Down
7 changes: 7 additions & 0 deletions services/api/src/resources/fact/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export const Sql = {
})
.del()
.toString(),
deleteFactsForEnvironment: (environment) => // Used when removing environments.
knex('environment_fact')
.where({
environment
})
.del()
.toString(),
deleteFactsFromSource: (environment, source) =>
knex('environment_fact')
.where({ environment, source })
Expand Down
7 changes: 7 additions & 0 deletions services/api/src/resources/problem/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ export const Sql = {
}
return q.del().toString();
},
deleteProblemsForEnvironment: (environment) => { // This should be used primarily to remove problems when deleting an environment
let q = knex('environment_problem')
.where({
environment: environment
});
return q.del().toString();
},
deleteProblemsFromSource: (environment, source, service) =>
knex('environment_problem')
.where({
Expand Down

0 comments on commit 92af324

Please sign in to comment.