Update db.js

This commit is contained in:
2025-12-11 23:35:36 +01:00
committed by GitHub
parent 348f5604bf
commit 6194001462

View File

@@ -73,25 +73,34 @@ const initDb = async () => {
id INT PRIMARY KEY,
current_year INT,
smtp_config JSON ${DB_CLIENT === 'postgres' ? 'NULL' : 'NULL'},
features JSON ${DB_CLIENT === 'postgres' ? 'NULL' : 'NULL'}
features JSON ${DB_CLIENT === 'postgres' ? 'NULL' : 'NULL'},
storage_config JSON ${DB_CLIENT === 'postgres' ? 'NULL' : 'NULL'}
)
`);
// Migration: Add features column if not exists
try {
let hasFeatures = false;
let hasStorage = false;
if (DB_CLIENT === 'postgres') {
const [cols] = await connection.query("SELECT column_name FROM information_schema.columns WHERE table_name='settings'");
hasFeatures = cols.some(c => c.column_name === 'features');
hasStorage = cols.some(c => c.column_name === 'storage_config');
} else {
const [cols] = await connection.query("SHOW COLUMNS FROM settings");
hasFeatures = cols.some(c => c.Field === 'features');
hasStorage = cols.some(c => c.Field === 'storage_config');
}
if (!hasFeatures) {
console.log('Migrating: Adding features to settings...');
await connection.query("ALTER TABLE settings ADD COLUMN features JSON");
}
if (!hasStorage) {
console.log('Migrating: Adding storage_config to settings...');
await connection.query("ALTER TABLE settings ADD COLUMN storage_config JSON");
}
} catch(e) { console.warn("Settings migration warning:", e.message); }
// 1. Condos Table
@@ -351,6 +360,24 @@ const initDb = async () => {
)
`);
// 13. DOCUMENTS (Cloud/Local)
await connection.query(`
CREATE TABLE IF NOT EXISTS documents (
id VARCHAR(36) PRIMARY KEY,
condo_id VARCHAR(36) NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
file_name VARCHAR(255) NOT NULL,
file_type VARCHAR(100),
file_size INT,
tags JSON ${DB_CLIENT === 'postgres' ? 'NULL' : 'NULL'},
storage_provider VARCHAR(50) DEFAULT 'local_db',
file_data ${LONG_TEXT_TYPE},
upload_date ${TIMESTAMP_TYPE} DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (condo_id) REFERENCES condos(id) ON DELETE CASCADE
)
`);
// --- SEEDING ---
const [rows] = await connection.query('SELECT * FROM settings WHERE id = 1');
const defaultFeatures = {
@@ -360,20 +387,28 @@ const initDb = async () => {
notices: true,
reports: true,
extraordinaryExpenses: true,
condoFinancialsView: false
condoFinancialsView: false,
documents: true // Enabled by default for demo
};
const defaultStorage = {
provider: 'local_db'
};
if (rows.length === 0) {
const currentYear = new Date().getFullYear();
await connection.query(
'INSERT INTO settings (id, current_year, features) VALUES (1, ?, ?)',
[currentYear, JSON.stringify(defaultFeatures)]
'INSERT INTO settings (id, current_year, features, storage_config) VALUES (1, ?, ?, ?)',
[currentYear, JSON.stringify(defaultFeatures), JSON.stringify(defaultStorage)]
);
} else {
// Ensure features column has defaults if null
if (!rows[0].features) {
await connection.query('UPDATE settings SET features = ? WHERE id = 1', [JSON.stringify(defaultFeatures)]);
}
if (!rows[0].storage_config) {
await connection.query('UPDATE settings SET storage_config = ? WHERE id = 1', [JSON.stringify(defaultStorage)]);
}
}
// ENSURE ADMIN EXISTS