Update db.js
This commit is contained in:
43
server/db.js
43
server/db.js
@@ -73,25 +73,34 @@ const initDb = async () => {
|
|||||||
id INT PRIMARY KEY,
|
id INT PRIMARY KEY,
|
||||||
current_year INT,
|
current_year INT,
|
||||||
smtp_config JSON ${DB_CLIENT === 'postgres' ? 'NULL' : 'NULL'},
|
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
|
// Migration: Add features column if not exists
|
||||||
try {
|
try {
|
||||||
let hasFeatures = false;
|
let hasFeatures = false;
|
||||||
|
let hasStorage = false;
|
||||||
|
|
||||||
if (DB_CLIENT === 'postgres') {
|
if (DB_CLIENT === 'postgres') {
|
||||||
const [cols] = await connection.query("SELECT column_name FROM information_schema.columns WHERE table_name='settings'");
|
const [cols] = await connection.query("SELECT column_name FROM information_schema.columns WHERE table_name='settings'");
|
||||||
hasFeatures = cols.some(c => c.column_name === 'features');
|
hasFeatures = cols.some(c => c.column_name === 'features');
|
||||||
|
hasStorage = cols.some(c => c.column_name === 'storage_config');
|
||||||
} else {
|
} else {
|
||||||
const [cols] = await connection.query("SHOW COLUMNS FROM settings");
|
const [cols] = await connection.query("SHOW COLUMNS FROM settings");
|
||||||
hasFeatures = cols.some(c => c.Field === 'features');
|
hasFeatures = cols.some(c => c.Field === 'features');
|
||||||
|
hasStorage = cols.some(c => c.Field === 'storage_config');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasFeatures) {
|
if (!hasFeatures) {
|
||||||
console.log('Migrating: Adding features to settings...');
|
console.log('Migrating: Adding features to settings...');
|
||||||
await connection.query("ALTER TABLE settings ADD COLUMN features JSON");
|
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); }
|
} catch(e) { console.warn("Settings migration warning:", e.message); }
|
||||||
|
|
||||||
// 1. Condos Table
|
// 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 ---
|
// --- SEEDING ---
|
||||||
const [rows] = await connection.query('SELECT * FROM settings WHERE id = 1');
|
const [rows] = await connection.query('SELECT * FROM settings WHERE id = 1');
|
||||||
const defaultFeatures = {
|
const defaultFeatures = {
|
||||||
@@ -360,20 +387,28 @@ const initDb = async () => {
|
|||||||
notices: true,
|
notices: true,
|
||||||
reports: true,
|
reports: true,
|
||||||
extraordinaryExpenses: true,
|
extraordinaryExpenses: true,
|
||||||
condoFinancialsView: false
|
condoFinancialsView: false,
|
||||||
|
documents: true // Enabled by default for demo
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultStorage = {
|
||||||
|
provider: 'local_db'
|
||||||
};
|
};
|
||||||
|
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
await connection.query(
|
await connection.query(
|
||||||
'INSERT INTO settings (id, current_year, features) VALUES (1, ?, ?)',
|
'INSERT INTO settings (id, current_year, features, storage_config) VALUES (1, ?, ?, ?)',
|
||||||
[currentYear, JSON.stringify(defaultFeatures)]
|
[currentYear, JSON.stringify(defaultFeatures), JSON.stringify(defaultStorage)]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Ensure features column has defaults if null
|
// Ensure features column has defaults if null
|
||||||
if (!rows[0].features) {
|
if (!rows[0].features) {
|
||||||
await connection.query('UPDATE settings SET features = ? WHERE id = 1', [JSON.stringify(defaultFeatures)]);
|
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
|
// ENSURE ADMIN EXISTS
|
||||||
|
|||||||
Reference in New Issue
Block a user