Database import

This commit is contained in:
Artyom Ashirov
2024-11-16 20:55:47 +03:00
parent e3f2e87fcc
commit b45aa35527
4 changed files with 383 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ import config from '../config/config.js';
import fs from "fs";
import db from "../config/database.js";
import archiver from "archiver";
import decompress from "decompress";
export default class AdminDumpHandler {
constructor(bot) {
@@ -29,7 +30,7 @@ export default class AdminDumpHandler {
await this.bot.sendMessage(chatId, 'Choose an option', {reply_markup: keyboard});
}
async exportDatabase(callbackQuery) {
async handleExportDatabase(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const tables = [
@@ -54,7 +55,7 @@ export default class AdminDumpHandler {
fs.mkdirSync(dumpPath);
for (const table of tables) {
const result = await db.runAsync(`SELECT * FROM ${table}`);
const result = await db.allAsync(`SELECT * FROM ${table}`);
const tableData = JSON.stringify(result);
fs.writeFileSync(`${dumpPath}/${table}.json`, tableData);
}
@@ -72,8 +73,67 @@ export default class AdminDumpHandler {
});
}
async importDatabase(callbackQuery) {
async handleImportDatabase(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
this.userStates.set(chatId, { action: 'upload_database_dump' });
await this.bot.editMessageText(
'Please upload database dump',
{
chat_id: chatId,
message_id: callbackQuery.message.message_id,
}
);
}
async getDumpStatistic() {
const tables = [
"categories",
"crypto_wallets",
"locations",
"products",
"purchases",
"transactions",
"users"
]
const stat = {}
for (const table of tables) {
const jsonContent = await fs.readFileSync(`./dump/${table}.json`, 'utf8');
const data = JSON.parse(jsonContent);
stat[table] = data.length
}
return stat;
}
async handleDumpImport(msg) {
const chatId = msg.chat.id;
const state = this.userStates.get(chatId);
if (!state || state.action !== 'upload_database_dump') {
return false;
}
if (msg.document) {
if (!msg.document.file_name.endsWith('.zip')) {
await this.bot.sendMessage(chatId, 'Please upload a .zip file.');
return true;
}
const file = await this.bot.getFile(msg.document.file_id);
const fileContent = await this.bot.downloadFile(file.file_id, '.');
await decompress(fileContent, './dump');
const statistics = await this.getDumpStatistic()
await this.bot.sendMessage(chatId, JSON.stringify(statistics, null, 2));
} else {
await this.bot.sendMessage(chatId, 'Please upload a valid .zip file.');
return true;
}
}
async confirmImport(callbackQuery) {

View File

@@ -107,6 +107,11 @@ bot.on('message', async (msg) => {
return;
}
// Check for database dump import
if (await adminDumpHandler.handleDumpImport(msg)) {
return;
}
logDebug(msg.text, 'handleMessage');
switch (msg.text) {
@@ -339,8 +344,10 @@ bot.on('callback_query', async (callbackQuery) => {
}
// Dump manage
else if (action === "export_database") {
await adminDumpHandler.exportDatabase(callbackQuery);
await adminDumpHandler.handleExportDatabase(callbackQuery);
return;
} else if (action === "import_database") {
await adminDumpHandler.handleImportDatabase(callbackQuery);
}
await bot.answerCallbackQuery(callbackQuery.id);