Skip to content

Commit

Permalink
refactor: delete backups when they are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jun 6, 2024
1 parent 38fd482 commit 85aacb5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
16 changes: 16 additions & 0 deletions docs/releases/2.20.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#### Release Links
* lagoon [v2.20.0](https://github.com/uselagoon/lagoon/releases/tag/v2.20.0)
* lagoon-ui [core-v2.20.0](https://github.com/uselagoon/lagoon-ui/releases/tag/core-v2.20.0)
* lagoon-build-deploy [core-v2.20.0](https://github.com/uselagoon/build-deploy-tool/releases/tag/core-v2.20.0)
* lagoon-core chart [1.45.0](https://github.com/uselagoon/lagoon-charts/releases/tag/lagoon-core-1.45.0)
* lagoon-remote chart [0.89.0](https://github.com/uselagoon/lagoon-charts/releases/tag/lagoon-remote-0.89.0)
* lagoon-test chart: [0.57.0](https://github.com/uselagoon/lagoon-charts/releases/tag/lagoon-test-0.57.0)


## Upgrades
There are no required actions or considerations with this release. As always, we suggest upgrading all minor versions.

## Deprecations

### Deleted Backups
* When a backup is deleted via the webhook, it will now actually removed from the API rather than being flagged as deleted. The `Backup` type field `deleted` is deprecated, and will be removed in a future release. Additionally, `includeDeleted` if requested when querying backups will not change the result as there will be no deleted backups to include.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
// remove any deleted environment backups that are flagged as deleted
// or where the environment/project are deleted or no longer exist
return knex.schema
.raw(`DELETE eb
FROM environment_backup eb
LEFT JOIN environment e ON eb.environment = e.id
WHERE eb.deleted != '0000-00-00 00:00:00' OR e.id IS NULL OR e.deleted != '0000-00-00 00:00:00' OR e.project NOT IN (SELECT id FROM project)
`)
// drop the deleted column
.alterTable('environment_backup', (table) => {
table.dropColumn('deleted');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema
.alterTable('environment_backup', (table) => {
table.timestamp('deleted').notNullable().defaultTo('0000-00-00 00:00:00');
});
};
6 changes: 1 addition & 5 deletions services/api/src/resources/backup/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const getRestoreLocation = async (backupId, restoreLocation, sqlClientPool) => {

export const getBackupsByEnvironmentId: ResolverFn = async (
{ id: environmentId },
{ includeDeleted, limit },
{ limit },
{ sqlClientPool, hasPermission, adminScopes }
) => {
const environment = await environmentHelpers(
Expand All @@ -142,10 +142,6 @@ export const getBackupsByEnvironmentId: ResolverFn = async (
.orderBy('created', 'desc')
.orderBy('id', 'desc');

if (!includeDeleted) {
queryBuilder = queryBuilder.where('deleted', '0000-00-00 00:00:00');
}

if (limit) {
queryBuilder = queryBuilder.limit(limit);
}
Expand Down
20 changes: 1 addition & 19 deletions services/api/src/resources/backup/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@ export const Sql = {
knex('env_vars')
.where('project', projectId)
.toString(),
selectBackupsByEnvironmentId: ({
environmentId,
includeDeleted
}: {
environmentId: number;
includeDeleted: boolean;
}) => {
const query = knex('environment_backup')
.where('environment', environmentId)
.orderBy('created', 'desc')
.orderBy('id', 'desc');

if (includeDeleted) {
return query.toString();
}

return query.where('deleted', '=', '0000-00-00 00:00:00').toString();
},
insertBackup: ({
id,
environment,
Expand All @@ -56,7 +38,7 @@ export const Sql = {
deleteBackup: (backupId: string) =>
knex('environment_backup')
.where('backup_id', backupId)
.update({ deleted: knex.fn.now() })
.delete()
.toString(),
truncateBackup: () =>
knex('environment_backup')
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 @@ -965,7 +965,7 @@ const typeDefs = gql`
source: String
backupId: String
created: String
deleted: String
deleted: String @deprecated(reason: "This will be removed in a future release.")
restore: Restore
}
Expand Down

0 comments on commit 85aacb5

Please sign in to comment.