Schema: add running_balance column to vault table

This commit is contained in:
Keith Solomon
2026-08-03 21:47:46 -05:00
parent 908e2c50ab
commit 2e1bd050e1
+13 -5
View File
@@ -30,11 +30,12 @@ function getDatabaseConnection() {
// SQL to create the vault table if it doesn't exist
$createTableSQL = "CREATE TABLE IF NOT EXISTS vault (
id TEXT PRIMARY KEY,
user TEXT NOT NULL,
timestamp INTEGER NOT NULL,
description TEXT NOT NULL,
amount REAL NOT NULL
id TEXT PRIMARY KEY,
user TEXT NOT NULL,
timestamp INTEGER NOT NULL,
description TEXT NOT NULL,
amount REAL NOT NULL,
running_balance INTEGER
);";
if ($pdo === null) {
@@ -43,6 +44,13 @@ function getDatabaseConnection() {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec($createTableSQL);
// Add the running_balance column for databases created before the
// column was added. Idempotent: skip if it already exists.
$columns = $pdo->query("PRAGMA table_info(vault)")->fetchAll(PDO::FETCH_COLUMN, 1);
if (!in_array('running_balance', $columns, true)) {
$pdo->exec('ALTER TABLE vault ADD COLUMN running_balance INTEGER');
}
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}