2024-02-12 14:04:03 +01:00
|
|
|
-- Add migration script here
|
|
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS users(
|
|
|
|
|
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
username TEXT,
|
|
|
|
|
pass TEXT,
|
|
|
|
|
UNIQUE(username)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS accounts(
|
|
|
|
|
account_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
user INTEGER,
|
|
|
|
|
account_name TEXT,
|
|
|
|
|
FOREIGN KEY (user) REFERENCES users(user_id)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS categories (
|
|
|
|
|
category_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
name TEXT,
|
|
|
|
|
description TEXT
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS rules(
|
|
|
|
|
rule_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
user INTEGER,
|
|
|
|
|
regex TEXT,
|
|
|
|
|
category INTEGER,
|
|
|
|
|
FOREIGN KEY (user) REFERENCES users(user_id)
|
|
|
|
|
FOREIGN KEY (category) REFERENCES categories(category_id)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS transactions (
|
|
|
|
|
transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
account INTEGER,
|
|
|
|
|
description TEXT,
|
2024-03-21 23:34:43 +01:00
|
|
|
tx_date DATETIME,
|
2024-02-12 14:04:03 +01:00
|
|
|
category INTEGER,
|
|
|
|
|
amount INTEGER,
|
2024-03-21 23:34:43 +01:00
|
|
|
accumulated INTEGER DEFAULT 0,
|
|
|
|
|
tx_order INTEGER DEFAULT 0,
|
2024-02-12 14:04:03 +01:00
|
|
|
FOREIGN KEY (account) REFERENCES accounts(account_id),
|
|
|
|
|
FOREIGN KEY (category) REFERENCES categories(category_id)
|
|
|
|
|
);
|
|
|
|
|
|
2024-03-21 23:34:43 +01:00
|
|
|
CREATE TRIGGER tx_insert AFTER INSERT ON transactions
|
|
|
|
|
BEGIN
|
|
|
|
|
UPDATE transactions
|
|
|
|
|
SET accumulated=old.acc+NEW.amount
|
|
|
|
|
FROM (
|
2024-04-24 23:19:21 +02:00
|
|
|
SELECT COALESCE(accumulated, 0) AS acc
|
2024-03-21 23:34:43 +01:00
|
|
|
FROM transactions
|
2024-04-24 23:19:21 +02:00
|
|
|
WHERE tx_date <= NEW.tx_date
|
|
|
|
|
AND transaction_id <> NEW.transaction_id
|
|
|
|
|
AND account=NEW.account
|
|
|
|
|
ORDER BY tx_date DESC, tx_order DESC
|
2024-03-21 23:34:43 +01:00
|
|
|
LIMIT 1
|
|
|
|
|
) AS old
|
2024-04-24 23:19:21 +02:00
|
|
|
WHERE transaction_id=NEW.transaction_id;
|
2024-03-21 23:34:43 +01:00
|
|
|
|
|
|
|
|
UPDATE transactions
|
|
|
|
|
SET tx_order=old.tx_order+1 FROM (
|
|
|
|
|
SELECT COALESCE(max(tx_order), 0) as tx_order
|
2024-04-24 23:19:21 +02:00
|
|
|
FROM transactions WHERE tx_date=NEW.tx_date
|
2024-03-21 23:34:43 +01:00
|
|
|
) AS old
|
2024-04-24 23:19:21 +02:00
|
|
|
WHERE transaction_id=NEW.transaction_id;
|
2024-03-21 23:34:43 +01:00
|
|
|
|
2024-04-24 23:19:21 +02:00
|
|
|
UPDATE transactions SET accumulated=calc.acc+cte_tx.accumulated FROM (
|
|
|
|
|
SELECT tx.transaction_id, (
|
2024-03-21 23:34:43 +01:00
|
|
|
SUM(amount) OVER (
|
2024-04-24 23:19:21 +02:00
|
|
|
ORDER BY tx_date, tx_order
|
2024-03-21 23:34:43 +01:00
|
|
|
ROWS BETWEEN
|
|
|
|
|
UNBOUNDED PRECEDING
|
|
|
|
|
AND CURRENT ROW
|
|
|
|
|
)
|
|
|
|
|
) acc
|
|
|
|
|
FROM transactions tx
|
2024-04-24 23:19:21 +02:00
|
|
|
WHERE tx_date > NEW.tx_date AND account=NEW.account
|
|
|
|
|
) AS calc, (
|
|
|
|
|
SELECT accumulated
|
|
|
|
|
FROM transactions tx
|
|
|
|
|
WHERE tx.transaction_id=NEW.transaction_id
|
|
|
|
|
) AS cte_tx
|
|
|
|
|
WHERE transactions.transaction_id=calc.transaction_id;
|
2024-03-21 23:34:43 +01:00
|
|
|
END;
|
|
|
|
|
CREATE INDEX idx_transactions_ts ON transactions(account, tx_date);
|