49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
const Database = require('better-sqlite3');
|
|
const path = require('path');
|
|
|
|
const dbPath = path.resolve(__dirname, 'server/data/sensors.db');
|
|
console.log(`Connecting to database at ${dbPath}`);
|
|
const db = new Database(dbPath);
|
|
|
|
// 1. Verify Table Creation
|
|
console.log('Creating changelog table...');
|
|
try {
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS changelog (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
date TEXT NOT NULL,
|
|
user TEXT,
|
|
text TEXT NOT NULL
|
|
)
|
|
`);
|
|
console.log('PASS: Table creation successful (or already exists)');
|
|
} catch (err) {
|
|
console.error('FAIL: Table creation failed:', err.message);
|
|
process.exit(1);
|
|
}
|
|
|
|
// 2. Verify Insert
|
|
console.log('Inserting test entry...');
|
|
try {
|
|
const stmt = db.prepare('INSERT INTO changelog (date, user, text) VALUES (?, ?, ?)');
|
|
const info = stmt.run(new Date().toISOString(), 'test_user', 'Test changelog entry');
|
|
console.log(`PASS: Insert successful, ID: ${info.lastInsertRowid}`);
|
|
} catch (err) {
|
|
console.error('FAIL: Insert failed:', err.message);
|
|
process.exit(1);
|
|
}
|
|
|
|
// 3. Verify Read
|
|
console.log('Reading entries...');
|
|
try {
|
|
const rows = db.prepare('SELECT * FROM changelog ORDER BY id DESC LIMIT 5').all();
|
|
console.table(rows);
|
|
if (rows.length > 0 && rows[0].user === 'test_user') {
|
|
console.log('PASS: Read verification successful');
|
|
} else {
|
|
console.error('FAIL: Read verification failed or data mismatch');
|
|
}
|
|
} catch (err) {
|
|
console.error('FAIL: Read failed:', err.message);
|
|
}
|