Files
finplan/snapshot.sh
Damien De Paoli e05b2c7b5b Major change: I have added finance_history
* pulled old values of data via restic backups
    * inserted them into a new finance_history table
    * added a finplan user, use of sudo & better ENV (container/production) & wrapper.sh into Dockerfile (like I do for other projects)
    * recalculate the bills / Living Expenses now we have real bills for a year
    * remove tax back for now (need to handle quit date vs. end of financial year better)
    * hard-coded / hacked in 2026 for pay cycle dates / should be dynamic, but not sure I'll work in 2027+
    * refactor front-end to handle 2026 as the current year / its more or less dynamic (via hard-coded FIRST_YEAR) variable - could not use datetime, as it was in late Dec. when I noticed the issue ('next pay' was in 2026, current year was 2025)
    * also added history to graphs, changed formatting to make the history /
    * savings projections to be orannge with circles, but historical is a solid line, future is a dash, also made all lines have lineWidth: 1 for aesthetics
2026-01-17 19:28:24 +11:00

42 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
DB_FILE="finance.db"
HISTORY_TABLE="finance_history"
# Current date in the format you've been using
DATE_STR=$(date +%Y-%m-%d)
# 1. Identify which table exists (finance or finance_data)
SRC_TABLE=finance
if [ -z "$SRC_TABLE" ]; then
echo "Error: Source table not found."
exit 1
fi
# 2. Get columns dynamically (excluding 'id')
COLS=$(sqlite3 "$DB_FILE" "PRAGMA table_info($SRC_TABLE);" | cut -d'|' -f2 | grep -v '^id$' | xargs | tr ' ' ',')
# 3. Change Detection Logic
# We grab the latest row from history and compare it to the current source data
CURRENT_VALS=$(sqlite3 "$DB_FILE" "SELECT $COLS FROM $SRC_TABLE ORDER BY id DESC LIMIT 1;")
LAST_VALS=$(sqlite3 "$DB_FILE" "SELECT $COLS FROM $HISTORY_TABLE ORDER BY snapshot_date DESC LIMIT 1;")
if [ "$CURRENT_VALS" == "$LAST_VALS" ]; then
echo "Data identical to last snapshot ($DATE_STR). Skipping."
exit 0
fi
# 4. Auto-Migration
# Ensure any new columns in 'finance' also exist in 'finance_history'
for col in ${COLS//,/ }; do
sqlite3 "$DB_FILE" "ALTER TABLE $HISTORY_TABLE ADD COLUMN $col NUMERIC;" 2>/dev/null
done
# 5. The Snapshot Insert
# INSERT OR IGNORE ensures that if Docker restarts today, we don't duplicate the row.
echo "Changes detected. Recording snapshot for $DATE_STR..."
sqlite3 "$DB_FILE" <<EOF
INSERT OR IGNORE INTO $HISTORY_TABLE (snapshot_date, $COLS)
SELECT '$DATE_STR', $COLS FROM $SRC_TABLE ORDER BY id DESC LIMIT 1;
EOF