Skip to content

Commit

Permalink
Update fact sql merge
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Jan 17, 2024
2 parents ac5baae + c44d543 commit d368cd8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 4 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;
};
19 changes: 19 additions & 0 deletions services/api/database/migrations/20240108000000_fix_env_orphans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
return knex('environment')
.leftJoin('project', 'environment.project', 'project.id')
.whereNull('project.id')
.andWhere('environment.deleted','0000-00-00 00:00:00')
.update('deleted', knex.raw('NOW()'));
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async 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, service) => {
let query = 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
2 changes: 1 addition & 1 deletion services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ const typeDefs = gql`
"""
The size of the restored file in bytes
"""
restoreSize: Int
restoreSize: Float
created: String
}
Expand Down
6 changes: 3 additions & 3 deletions services/logs2notifications/internal/handler/email_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (h *Messaging) processEmailTemplates(notification *Notification) (string, s
if err != nil {
eventSplit := strings.Split(notification.Event, ":")
if eventSplit[0] != "problem" {
return "", "", "", "", "", nil
return "", "", "", "", "", fmt.Errorf("no matching event")
}
if eventSplit[1] == "insert" {
tpl = "problemNotification"
Expand Down Expand Up @@ -127,7 +127,7 @@ func (h *Messaging) processEmailTemplates(notification *Notification) (string, s
case "problemNotification":
eventSplit := strings.Split(notification.Event, ":")
if eventSplit[0] != "problem" && eventSplit[1] == "insert" {
return "", "", "", "", "", nil
return "", "", "", "", "", fmt.Errorf("no matching event")
}
mainHTMLTpl = `[{{.ProjectName}}] New problem found for <code>{{.EnvironmentName}}</code>
<ul><li>* Service: {{.ServiceName}}</li>{{ if ne .Severity "" }}
Expand All @@ -142,7 +142,7 @@ func (h *Messaging) processEmailTemplates(notification *Notification) (string, s
notification.Meta.EnvironmentName,
)
default:
return "", "", "", "", "", nil
return "", "", "", "", "", fmt.Errorf("no matching event")
}

var body bytes.Buffer
Expand Down

0 comments on commit d368cd8

Please sign in to comment.