p4-hotfixes #14
@@ -13,27 +13,14 @@ db.pragma('foreign_keys = ON');
|
|||||||
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
|
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
|
||||||
db.exec(schema);
|
db.exec(schema);
|
||||||
|
|
||||||
// Migrate: add negated columns if upgrading from Phase 1-3
|
// ── Migrations for existing DBs ─────────────────────────────────────────────
|
||||||
const cols = db.prepare("PRAGMA table_info(violations)").all().map(c => c.name);
|
const cols = db.prepare('PRAGMA table_info(violations)').all().map(c => c.name);
|
||||||
if (!cols.includes('negated')) db.exec("ALTER TABLE violations ADD COLUMN negated INTEGER NOT NULL DEFAULT 0");
|
if (!cols.includes('negated')) db.exec("ALTER TABLE violations ADD COLUMN negated INTEGER NOT NULL DEFAULT 0");
|
||||||
if (!cols.includes('negated_at')) db.exec("ALTER TABLE violations ADD COLUMN negated_at DATETIME");
|
if (!cols.includes('negated_at')) db.exec("ALTER TABLE violations ADD COLUMN negated_at DATETIME");
|
||||||
|
if (!cols.includes('prior_active_points')) db.exec("ALTER TABLE violations ADD COLUMN prior_active_points INTEGER");
|
||||||
|
if (!cols.includes('prior_tier_label')) db.exec("ALTER TABLE violations ADD COLUMN prior_tier_label TEXT");
|
||||||
|
|
||||||
// After adding columns + resolutions table, ensure view is correct
|
// Ensure resolutions table exists
|
||||||
db.exec(`
|
|
||||||
DROP VIEW IF EXISTS active_cpas_scores;
|
|
||||||
CREATE VIEW active_cpas_scores AS
|
|
||||||
SELECT
|
|
||||||
employee_id,
|
|
||||||
SUM(points) AS active_points,
|
|
||||||
COUNT(*) AS violation_count
|
|
||||||
FROM violations
|
|
||||||
WHERE negated = 0
|
|
||||||
AND incident_date >= DATE('now', '-90 days')
|
|
||||||
GROUP BY employee_id;
|
|
||||||
`);
|
|
||||||
|
|
||||||
|
|
||||||
// Ensure resolutions table exists on upgrade
|
|
||||||
db.exec(`CREATE TABLE IF NOT EXISTS violation_resolutions (
|
db.exec(`CREATE TABLE IF NOT EXISTS violation_resolutions (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
violation_id INTEGER NOT NULL REFERENCES violations(id) ON DELETE CASCADE,
|
violation_id INTEGER NOT NULL REFERENCES violations(id) ON DELETE CASCADE,
|
||||||
@@ -43,5 +30,17 @@ db.exec(`CREATE TABLE IF NOT EXISTS violation_resolutions (
|
|||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
)`);
|
)`);
|
||||||
|
|
||||||
|
// Recreate view so it always filters negated rows
|
||||||
|
db.exec(`DROP VIEW IF EXISTS active_cpas_scores;
|
||||||
|
CREATE VIEW active_cpas_scores AS
|
||||||
|
SELECT
|
||||||
|
employee_id,
|
||||||
|
SUM(points) AS active_points,
|
||||||
|
COUNT(*) AS violation_count
|
||||||
|
FROM violations
|
||||||
|
WHERE negated = 0
|
||||||
|
AND incident_date >= DATE('now', '-90 days')
|
||||||
|
GROUP BY employee_id;`);
|
||||||
|
|
||||||
console.log('[DB] Connected:', dbPath);
|
console.log('[DB] Connected:', dbPath);
|
||||||
module.exports = db;
|
module.exports = db;
|
||||||
|
|||||||
@@ -7,21 +7,23 @@ CREATE TABLE IF NOT EXISTS employees (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS violations (
|
CREATE TABLE IF NOT EXISTS violations (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
employee_id INTEGER NOT NULL REFERENCES employees(id),
|
employee_id INTEGER NOT NULL REFERENCES employees(id),
|
||||||
violation_type TEXT NOT NULL,
|
violation_type TEXT NOT NULL,
|
||||||
violation_name TEXT NOT NULL,
|
violation_name TEXT NOT NULL,
|
||||||
category TEXT NOT NULL DEFAULT 'General',
|
category TEXT NOT NULL DEFAULT 'General',
|
||||||
points INTEGER NOT NULL,
|
points INTEGER NOT NULL,
|
||||||
incident_date TEXT NOT NULL,
|
incident_date TEXT NOT NULL,
|
||||||
incident_time TEXT,
|
incident_time TEXT,
|
||||||
location TEXT,
|
location TEXT,
|
||||||
details TEXT,
|
details TEXT,
|
||||||
submitted_by TEXT,
|
submitted_by TEXT,
|
||||||
witness_name TEXT,
|
witness_name TEXT,
|
||||||
negated INTEGER NOT NULL DEFAULT 0,
|
negated INTEGER NOT NULL DEFAULT 0,
|
||||||
negated_at DATETIME,
|
negated_at DATETIME,
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
prior_active_points INTEGER, -- snapshot at time of logging
|
||||||
|
prior_tier_label TEXT, -- optional human-readable tier
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS violation_resolutions (
|
CREATE TABLE IF NOT EXISTS violation_resolutions (
|
||||||
|
|||||||
Reference in New Issue
Block a user