2022-12-30 16:20:53 +00:00
|
|
|
import log from "../log";
|
|
|
|
import {Command} from "commander";
|
|
|
|
import ClientManager from "../clientManager";
|
|
|
|
import Utils from "./utils";
|
|
|
|
import SqliteMessageStorage from "../plugins/messageStorage/sqlite";
|
2023-11-04 16:30:43 +00:00
|
|
|
import {StorageCleaner} from "../storageCleaner";
|
2022-12-30 16:20:53 +00:00
|
|
|
|
|
|
|
const program = new Command("storage").description(
|
|
|
|
"various utilities related to the message storage"
|
|
|
|
);
|
|
|
|
|
|
|
|
program
|
|
|
|
.command("migrate")
|
2023-11-04 16:30:43 +00:00
|
|
|
.argument("[username]", "migrate a specific user only, all if not provided")
|
2022-12-30 16:20:53 +00:00
|
|
|
.description("Migrate message storage where needed")
|
|
|
|
.on("--help", Utils.extraHelp)
|
|
|
|
.action(function (user) {
|
|
|
|
runMigrations(user).catch((err) => {
|
|
|
|
log.error(err.toString());
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-11-04 16:30:43 +00:00
|
|
|
program
|
|
|
|
.command("clean")
|
|
|
|
.argument("[user]", "clean messages for a specific user only, all if not provided")
|
|
|
|
.description("Delete messages from the DB based on the storage policy")
|
|
|
|
.on("--help", Utils.extraHelp)
|
|
|
|
.action(function (user) {
|
|
|
|
runCleaning(user).catch((err) => {
|
|
|
|
log.error(err.toString());
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
async function runMigrations(user?: string) {
|
2022-12-30 16:20:53 +00:00
|
|
|
const manager = new ClientManager();
|
|
|
|
const users = manager.getUsers();
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
if (!users.includes(user)) {
|
|
|
|
throw new Error(`invalid user ${user}`);
|
|
|
|
}
|
|
|
|
|
2022-12-31 12:53:29 +00:00
|
|
|
return migrateUser(manager, user);
|
2022-12-30 16:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const name of users) {
|
2022-12-31 12:53:29 +00:00
|
|
|
await migrateUser(manager, name);
|
2022-12-30 16:20:53 +00:00
|
|
|
// if any migration fails we blow up,
|
|
|
|
// chances are the rest won't complete either
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// runs sqlite migrations for a user, which must exist
|
2022-12-31 12:53:29 +00:00
|
|
|
async function migrateUser(manager: ClientManager, user: string) {
|
2022-12-30 16:20:53 +00:00
|
|
|
log.info("handling user", user);
|
2022-12-31 12:53:29 +00:00
|
|
|
|
|
|
|
if (!isUserLogEnabled(manager, user)) {
|
|
|
|
log.info("logging disabled for user", user, ". Skipping");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:20:53 +00:00
|
|
|
const sqlite = new SqliteMessageStorage(user);
|
|
|
|
await sqlite.enable(); // enable runs migrations
|
|
|
|
await sqlite.close();
|
|
|
|
log.info("user", user, "migrated successfully");
|
|
|
|
}
|
|
|
|
|
2022-12-31 12:53:29 +00:00
|
|
|
function isUserLogEnabled(manager: ClientManager, user: string): boolean {
|
|
|
|
const conf = manager.readUserConfig(user);
|
|
|
|
|
|
|
|
if (!conf) {
|
|
|
|
log.error("Could not open user configuration of", user);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf.log;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:30:43 +00:00
|
|
|
async function runCleaning(user: string) {
|
|
|
|
const manager = new ClientManager();
|
|
|
|
const users = manager.getUsers();
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
if (!users.includes(user)) {
|
|
|
|
throw new Error(`invalid user ${user}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanUser(manager, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const name of users) {
|
|
|
|
await cleanUser(manager, name);
|
|
|
|
// if any migration fails we blow up,
|
|
|
|
// chances are the rest won't complete either
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanUser(manager: ClientManager, user: string) {
|
|
|
|
log.info("handling user", user);
|
|
|
|
|
|
|
|
if (!isUserLogEnabled(manager, user)) {
|
|
|
|
log.info("logging disabled for user", user, ". Skipping");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sqlite = new SqliteMessageStorage(user);
|
|
|
|
await sqlite.enable();
|
|
|
|
const cleaner = new StorageCleaner(sqlite);
|
|
|
|
const num_deleted = await cleaner.runDeletesNoLimit();
|
|
|
|
log.info(`deleted ${num_deleted} messages`);
|
|
|
|
log.info("running a vacuum now, this might take a while");
|
|
|
|
|
|
|
|
if (num_deleted > 0) {
|
|
|
|
await sqlite.vacuum();
|
|
|
|
}
|
|
|
|
|
|
|
|
await sqlite.close();
|
|
|
|
log.info(`cleaning messages for ${user} has been successful`);
|
|
|
|
}
|
|
|
|
|
2022-12-30 16:20:53 +00:00
|
|
|
export default program;
|