This commit is contained in:
sebseb7
2025-12-25 00:08:05 +01:00
parent 1f3292bc17
commit 077e76735e
15 changed files with 9311 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
const Database = require('better-sqlite3');
const path = require('path');
const { config } = require('dotenv');
// Load env from root
config({ path: path.resolve(__dirname, '../../.env') });
const dbPath = process.env.DB_PATH || path.resolve(__dirname, '../../server/data/sensors.db');
const db = new Database(dbPath);
console.log(`[Migrate] Connected to ${dbPath}`);
console.log('[Migrate] Applying schema migrations...');
try {
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL CHECK(role IN ('admin', 'normal')),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS views (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
config TEXT NOT NULL,
created_by INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(created_by) REFERENCES users(id)
);
`);
console.log('[Migrate] Schema applied successfully.');
} catch (err) {
console.error('[Migrate] Error applying schema:', err.message);
} finally {
db.close();
}