-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #100
- Loading branch information
Showing
19 changed files
with
443 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { RequestHandler } from "express"; | ||
export declare const handler: RequestHandler; | ||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const handler = async (_request, response) => { | ||
response.render("admin-cleanup", { | ||
headTitle: "Database Cleanup" | ||
}); | ||
}; | ||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { RequestHandler } from "express"; | ||
|
||
export const handler: RequestHandler = async (_request, response) => { | ||
|
||
response.render("admin-cleanup", { | ||
headTitle: "Database Cleanup" | ||
}); | ||
}; | ||
|
||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { RequestHandler } from "express"; | ||
export declare const handler: RequestHandler; | ||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { cleanupDatabase } from "../../helpers/licencesDB/cleanupDatabase.js"; | ||
export const handler = async (_request, response) => { | ||
const rowCount = cleanupDatabase(); | ||
response.json({ | ||
success: true, | ||
rowCount | ||
}); | ||
}; | ||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { RequestHandler } from "express"; | ||
|
||
import { cleanupDatabase } from "../../helpers/licencesDB/cleanupDatabase.js"; | ||
|
||
|
||
export const handler: RequestHandler = async (_request, response) => { | ||
|
||
const rowCount = cleanupDatabase(); | ||
|
||
response.json({ | ||
success: true, | ||
rowCount | ||
}); | ||
}; | ||
|
||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export declare const cleanupDatabase: () => number; | ||
export default cleanupDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sqlite from "better-sqlite3"; | ||
import { licencesDB as databasePath } from "../../data/databasePaths.js"; | ||
const recordDelete_timeMillis_is_not_null = "recordDelete_timeMillis is not null"; | ||
const cleanupQueries = [ | ||
("delete from LicenceTransactions" + | ||
" where " + recordDelete_timeMillis_is_not_null), | ||
("delete from RelatedLicences" + | ||
" where licenceIdA in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")" + | ||
" or licenceIdB in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
("delete from LicenceFields" + | ||
" where licenceId in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
("delete from LicenceApprovals" + | ||
" where licenceId in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
("delete from LicenceAdditionalFees" + | ||
" where licenceId in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
("delete from Licences" + | ||
" where " + recordDelete_timeMillis_is_not_null + | ||
" and licenceId not in (select licenceId from LicenceAdditionalFees)" + | ||
" and licenceId not in (select licenceId from LicenceFields)" + | ||
" and licenceId not in (select licenceId from LicenceApprovals)" + | ||
" and licenceId not in (select licenceId from LicenceTransactions)" + | ||
" and licenceId not in (select licenceIdA from RelatedLicences)" + | ||
" and licenceId not in (select licenceIdB from RelatedLicences)"), | ||
("delete from LicenceCategoryFields" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceFieldKey not in (select licenceFieldKey from LicenceFields)"), | ||
("delete from LicenceCategoryApprovals" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceApprovalKey not in (select licenceApprovalKey from LicenceApprovals)"), | ||
("delete from LicenceCategoryAdditionalFees" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceAdditionalFeeKey not in (select licenceAdditionalFeeKey from LicenceAdditionalFees)"), | ||
("delete from LicenceCategoryFees" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from Licences)"), | ||
("delete from LicenceCategories" + | ||
" where " + recordDelete_timeMillis_is_not_null + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from Licences)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceCategoryFields)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceCategoryApprovals)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceAdditionalFees)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceCategoryFees)") | ||
]; | ||
export const cleanupDatabase = () => { | ||
const database = sqlite(databasePath); | ||
let rowCount = 0; | ||
for (const cleanupQuery of cleanupQueries) { | ||
rowCount += database.prepare(cleanupQuery).run().changes; | ||
} | ||
database.close(); | ||
return rowCount; | ||
}; | ||
export default cleanupDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import sqlite from "better-sqlite3"; | ||
import { licencesDB as databasePath } from "../../data/databasePaths.js"; | ||
|
||
|
||
// Avoid mistyping!!! | ||
const recordDelete_timeMillis_is_not_null = "recordDelete_timeMillis is not null"; | ||
|
||
|
||
const cleanupQueries = [ | ||
|
||
/* | ||
* Licences | ||
*/ | ||
|
||
// Purge deleted transactions | ||
("delete from LicenceTransactions" + | ||
" where " + recordDelete_timeMillis_is_not_null), | ||
|
||
// Purge related licences | ||
("delete from RelatedLicences" + | ||
" where licenceIdA in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")" + | ||
" or licenceIdB in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
|
||
// Purge fields | ||
("delete from LicenceFields" + | ||
" where licenceId in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
|
||
// Purge approvals | ||
("delete from LicenceApprovals" + | ||
" where licenceId in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
|
||
// Purge additional fees | ||
("delete from LicenceAdditionalFees" + | ||
" where licenceId in (select licenceId from Licences where " + recordDelete_timeMillis_is_not_null + ")"), | ||
|
||
// Purge deleted licences with no foreign keys | ||
("delete from Licences" + | ||
" where " + recordDelete_timeMillis_is_not_null + | ||
" and licenceId not in (select licenceId from LicenceAdditionalFees)" + | ||
" and licenceId not in (select licenceId from LicenceFields)" + | ||
" and licenceId not in (select licenceId from LicenceApprovals)" + | ||
" and licenceId not in (select licenceId from LicenceTransactions)" + | ||
" and licenceId not in (select licenceIdA from RelatedLicences)" + | ||
" and licenceId not in (select licenceIdB from RelatedLicences)"), | ||
|
||
/* | ||
* Licence Categories | ||
*/ | ||
|
||
// Purge fields | ||
("delete from LicenceCategoryFields" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceFieldKey not in (select licenceFieldKey from LicenceFields)"), | ||
|
||
// Purge approvals | ||
("delete from LicenceCategoryApprovals" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceApprovalKey not in (select licenceApprovalKey from LicenceApprovals)"), | ||
|
||
// Purge additional fees | ||
("delete from LicenceCategoryAdditionalFees" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceAdditionalFeeKey not in (select licenceAdditionalFeeKey from LicenceAdditionalFees)"), | ||
|
||
// Purge fees | ||
("delete from LicenceCategoryFees" + | ||
" where (" + recordDelete_timeMillis_is_not_null + " or licenceCategoryKey in (select licenceCategoryKey from LicenceCategories where " + recordDelete_timeMillis_is_not_null + "))" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from Licences)"), | ||
|
||
// Purge categories | ||
("delete from LicenceCategories" + | ||
" where " + recordDelete_timeMillis_is_not_null + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from Licences)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceCategoryFields)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceCategoryApprovals)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceAdditionalFees)" + | ||
" and licenceCategoryKey not in (select licenceCategoryKey from LicenceCategoryFees)") | ||
]; | ||
|
||
|
||
export const cleanupDatabase = (): number => { | ||
|
||
const database = sqlite(databasePath); | ||
|
||
let rowCount = 0; | ||
|
||
for (const cleanupQuery of cleanupQueries) { | ||
rowCount += database.prepare(cleanupQuery).run().changes; | ||
} | ||
|
||
database.close(); | ||
|
||
return rowCount; | ||
}; | ||
|
||
|
||
export default cleanupDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
(() => { | ||
const urlPrefix = document.querySelector("main").dataset.urlPrefix; | ||
document.querySelector("#cleanup--backupDatabase").addEventListener("click", (clickEvent) => { | ||
clickEvent.preventDefault(); | ||
const doBackup = () => { | ||
cityssm.postJSON(urlPrefix + "/admin/doBackupDatabase", {}, (responseJSON) => { | ||
if (responseJSON.success) { | ||
bulmaJS.alert({ | ||
title: "Database Backed Up Successfully", | ||
message: "Database backed up as <strong>" + cityssm.escapeHTML(responseJSON.fileName) + "</strong>.", | ||
messageIsHtml: true, | ||
contextualColorName: "success" | ||
}); | ||
} | ||
}); | ||
}; | ||
bulmaJS.confirm({ | ||
title: "Backup Database", | ||
message: "To ensure all data is backed up properly," + | ||
" please make sure all users with update privileges avoid making changes while the backup is running.", | ||
contextualColorName: "info", | ||
okButton: { | ||
text: "Backup Database Now", | ||
callbackFunction: doBackup | ||
} | ||
}); | ||
}); | ||
document.querySelector("#cleanup--cleanupDatabase").addEventListener("click", (clickEvent) => { | ||
clickEvent.preventDefault(); | ||
const doCleanup = () => { | ||
cityssm.postJSON(urlPrefix + "/admin/doCleanupDatabase", {}, (responseJSON) => { | ||
if (responseJSON.success) { | ||
bulmaJS.alert({ | ||
title: "Database Cleaned Up Successfully", | ||
message: responseJSON.rowCount + " row" + (responseJSON.rowCount === 1 ? "" : "s") + " deleted.", | ||
contextualColorName: "success" | ||
}); | ||
} | ||
}); | ||
}; | ||
bulmaJS.confirm({ | ||
title: "Cleanup Database", | ||
message: "Are you sure you want to cleanup the database?", | ||
contextualColorName: "info", | ||
okButton: { | ||
text: "Cleanup Database Now", | ||
callbackFunction: doCleanup | ||
} | ||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-disable unicorn/filename-case, unicorn/prefer-module */ | ||
|
||
import type { cityssmGlobal } from "@cityssm/bulma-webapp-js/src/types"; | ||
import type { BulmaJS } from "@cityssm/bulma-js/types"; | ||
|
||
declare const cityssm: cityssmGlobal; | ||
declare const bulmaJS: BulmaJS; | ||
|
||
(() => { | ||
const urlPrefix = document.querySelector("main").dataset.urlPrefix; | ||
|
||
document.querySelector("#cleanup--backupDatabase").addEventListener("click", (clickEvent) => { | ||
clickEvent.preventDefault(); | ||
|
||
const doBackup = () => { | ||
|
||
cityssm.postJSON(urlPrefix + "/admin/doBackupDatabase", {}, | ||
(responseJSON: { success: boolean; fileName?: string; }) => { | ||
|
||
if (responseJSON.success) { | ||
bulmaJS.alert({ | ||
title: "Database Backed Up Successfully", | ||
message: "Database backed up as <strong>" + cityssm.escapeHTML(responseJSON.fileName) + "</strong>.", | ||
messageIsHtml: true, | ||
contextualColorName: "success" | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
bulmaJS.confirm({ | ||
title: "Backup Database", | ||
message: "To ensure all data is backed up properly," + | ||
" please make sure all users with update privileges avoid making changes while the backup is running.", | ||
contextualColorName: "info", | ||
okButton: { | ||
text: "Backup Database Now", | ||
callbackFunction: doBackup | ||
} | ||
}); | ||
}); | ||
|
||
document.querySelector("#cleanup--cleanupDatabase").addEventListener("click", (clickEvent) => { | ||
clickEvent.preventDefault(); | ||
|
||
const doCleanup = () => { | ||
|
||
cityssm.postJSON(urlPrefix + "/admin/doCleanupDatabase", {}, | ||
(responseJSON: { success: boolean; rowCount?: number; }) => { | ||
|
||
if (responseJSON.success) { | ||
bulmaJS.alert({ | ||
title: "Database Cleaned Up Successfully", | ||
message: responseJSON.rowCount + " row" + (responseJSON.rowCount === 1 ? "" : "s") + " deleted.", | ||
contextualColorName: "success" | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
bulmaJS.confirm({ | ||
title: "Cleanup Database", | ||
message: "Are you sure you want to cleanup the database?", | ||
contextualColorName: "info", | ||
okButton: { | ||
text: "Cleanup Database Now", | ||
callbackFunction: doCleanup | ||
} | ||
}); | ||
}); | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.