accounters/migrations/20231110161954_base.sql

75 lines
2.2 KiB
MySQL
Raw Normal View History

2024-02-12 14:04:03 +01:00
-- Add migration script here
CREATE TABLE IF NOT EXISTS accounts(
account_id INTEGER PRIMARY KEY AUTOINCREMENT,
2024-06-03 21:28:34 +02:00
account_name TEXT
2024-02-12 14:04:03 +01:00
);
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,
regex TEXT,
category INTEGER,
FOREIGN KEY (category) REFERENCES categories(category_id)
);
CREATE TABLE IF NOT EXISTS transactions (
transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,
account INTEGER,
description TEXT,
tx_date DATETIME,
2024-02-12 14:04:03 +01:00
category INTEGER,
amount INTEGER,
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)
);
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
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
LIMIT 1
) AS old
2024-04-24 23:19:21 +02:00
WHERE transaction_id=NEW.transaction_id;
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
) AS old
2024-04-24 23:19:21 +02:00
WHERE transaction_id=NEW.transaction_id;
2024-04-24 23:19:21 +02:00
UPDATE transactions SET accumulated=calc.acc+cte_tx.accumulated FROM (
SELECT tx.transaction_id, (
SUM(amount) OVER (
2024-04-24 23:19:21 +02:00
ORDER BY tx_date, tx_order
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;
END;
CREATE INDEX idx_transactions_ts ON transactions(account, tx_date);