209 lines
9.3 KiB
JavaScript
209 lines
9.3 KiB
JavaScript
const sqlite3 = require('sqlite3').verbose();
|
|
|
|
const db = new sqlite3.Database('database.sqlite');
|
|
const db_old = new sqlite3.Database('db_old.sqlite');
|
|
|
|
|
|
// Створення підїздів
|
|
// const sql_1 = `SELECT * FROM areas`;
|
|
// db_old.all(sql_1, [], (err, areas) => {
|
|
// if (err) {
|
|
// throw err;
|
|
// }
|
|
|
|
// for (let i = 0; i < areas.length; i++) {
|
|
// const area = areas[i];
|
|
|
|
// if(area.type == "house"){
|
|
// // console.log(JSON.parse(area.entrance));
|
|
|
|
// db.get('SELECT * FROM house WHERE title = ? AND number = ?', [area.address_title, area.address_number], (err, house) => {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// let entrances = JSON.parse(area.entrance);
|
|
// let entrance_numbers = JSON.parse(area.entrance_number);
|
|
|
|
// for (let q = 0; q < entrances.length; q++) {
|
|
// const entrance = entrances[q];
|
|
// const number = entrance_numbers[q];
|
|
// console.log(entrance, number);
|
|
|
|
// db.run(`INSERT INTO entrance(house_id, entrance_number, title, points, points_number) VALUES(?, ?, ?, ?, ?)`,
|
|
// [
|
|
// house.id,
|
|
// q,
|
|
// `Під'їзд ${q+1}`,
|
|
// JSON.stringify(entrance),
|
|
// JSON.stringify(number)
|
|
// ],
|
|
// function (err) {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// console.log("New user entrance added with id " + this.lastID);
|
|
// }
|
|
// }
|
|
// );
|
|
// }
|
|
|
|
// // console.log(JSON.parse(area.entrance));
|
|
// }
|
|
// })
|
|
// }
|
|
// }
|
|
// });
|
|
|
|
|
|
|
|
// Міграція історії з старої БД в нову
|
|
// const sql_1 = `SELECT * FROM history WHERE date_start >= 1737667735000 ORDER BY date_start`;
|
|
// db_old.all(sql_1, [], (err, historys) => {
|
|
// if (err) {
|
|
// throw err;
|
|
// }
|
|
|
|
// for (let i = 0; i < historys.length; i++) {
|
|
// const history = historys[i];
|
|
|
|
// db_old.get('SELECT * FROM territory WHERE number = ?', [history.territory_number], (err, territory) => {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// // console.log(territory);
|
|
// let areas_id = JSON.parse(territory.areas_id)
|
|
|
|
// for (let index = 0; index < areas_id.length; index++) {
|
|
// const element = areas_id[index];
|
|
|
|
// db_old.get('SELECT * FROM areas WHERE id = ?', [element[0]], (err, area) => {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// if (area.type == "house") {
|
|
// console.log(area.address_title, area.address_number);
|
|
|
|
// db.get('SELECT * FROM house WHERE title = ? AND number = ?', [area.address_title, area.address_number], (err, house) => {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// console.log(area.address_title, area.address_number);
|
|
// console.log(house.id, element[1]);
|
|
|
|
// db.get('SELECT * FROM entrance WHERE house_id = ? AND entrance_number = ?', [house.id, element[1]], (err, entrance) => {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// console.log(house.id, element[1]);
|
|
|
|
// console.log(entrance.id, house.title, house.number, entrance.title);
|
|
|
|
// db.run(`INSERT INTO entrance_history(entrance_id, name, date_start, date_end, group_id, working) VALUES(?, ?, ?, ?, ?, ?)`,
|
|
// [
|
|
// entrance.id,
|
|
// history.name,
|
|
// history.date_start,
|
|
// history.date_end,
|
|
// history.group_number,
|
|
// history.working,
|
|
// ],
|
|
// function (err) {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// console.log("New user history added with id " + this.lastID);
|
|
// }
|
|
// }
|
|
// );
|
|
// }
|
|
// })
|
|
// }
|
|
// })
|
|
|
|
// } else {
|
|
// db.get('SELECT * FROM homestead WHERE title = ? AND number = ?', [area.address_title, area.address_number], (err, homestead) => {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// // console.log(house.id, house.title, house.number);
|
|
|
|
// db.run(`INSERT INTO homestead_history(homestead_id, name, date_start, date_end, group_id, working) VALUES(?, ?, ?, ?, ?, ?)`,
|
|
// [
|
|
// homestead.id,
|
|
// history.name,
|
|
// history.date_start,
|
|
// history.date_end,
|
|
// history.group_number,
|
|
// history.working,
|
|
// ],
|
|
// function (err) {
|
|
// if (err) {
|
|
// console.error(err.message);
|
|
// } else {
|
|
// console.log("New user history added with id " + this.lastID);
|
|
// }
|
|
// }
|
|
// );
|
|
// }
|
|
// })
|
|
// }
|
|
// }
|
|
// })
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
// })
|
|
// }
|
|
// });
|
|
|
|
|
|
// Додавання ID вісника в entrance_history
|
|
// Оновлення кожного запису в entrance_history
|
|
db.serialize(() => {
|
|
db.all(`SELECT id, name FROM entrance_history`, (err, rows) => {
|
|
if (err) return console.error('Read error:', err.message);
|
|
|
|
const updateStmt = db.prepare(`UPDATE entrance_history SET sheep_id = ? WHERE id = ?`);
|
|
let pending = rows.length;
|
|
|
|
if (pending === 0) {
|
|
updateStmt.finalize();
|
|
db.close();
|
|
console.log('Нема записів для оновлення.');
|
|
return;
|
|
}
|
|
|
|
rows.forEach((row) => {
|
|
db.get(`SELECT id FROM sheeps WHERE name = ?`, [row.name], (err, sheep) => {
|
|
if (err) {
|
|
console.error('Search error:', err.message);
|
|
if (--pending === 0) {
|
|
updateStmt.finalize();
|
|
db.close();
|
|
console.log('Оновлення завершено (з помилками).');
|
|
}
|
|
return;
|
|
}
|
|
|
|
const sheepId = sheep ? sheep.id : 0;
|
|
|
|
updateStmt.run(sheepId, row.id, (err) => {
|
|
if (err) {
|
|
console.error(`Помилка оновлення запису ID ${row.id}:`, err.message);
|
|
}
|
|
|
|
if (--pending === 0) {
|
|
updateStmt.finalize();
|
|
db.close();
|
|
console.log('Оновлення завершено');
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|