u
This commit is contained in:
38
uiserver/scripts/migrate.js
Normal file
38
uiserver/scripts/migrate.js
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user