Skip to content

Commit 2772a71

Browse files
committed
fixes
1 parent 64a0c6e commit 2772a71

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

app.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,35 @@ func (a *App) SaveBackupConfig(cfg BackupConfig) error {
153153
return a.bm.SaveConfig(cfg)
154154
}
155155

156+
// AutoRestoreIfNeeded checks on startup whether the local database is empty
157+
// and a backup is configured. If so, it fetches and imports the remote backup
158+
// automatically. Returns true if a restore was performed.
159+
//
160+
// Errors from fetching (e.g. remote not yet initialised) are treated as
161+
// non-fatal — the app just starts with an empty DB as usual.
162+
func (a *App) AutoRestoreIfNeeded() (bool, error) {
163+
if a.bm == nil {
164+
return false, nil
165+
}
166+
cfg, err := a.bm.LoadConfig()
167+
if err != nil || cfg.Repo == "" {
168+
return false, nil
169+
}
170+
empty, err := a.db.IsEmpty()
171+
if err != nil || !empty {
172+
return false, err
173+
}
174+
jsonData, err := a.bm.FetchBackup()
175+
if err != nil {
176+
// Remote may not have a backup yet — not an error the user needs to see.
177+
return false, nil
178+
}
179+
if err := a.db.ImportJSON(jsonData); err != nil {
180+
return false, err
181+
}
182+
return true, nil
183+
}
184+
156185
func (a *App) BackupNow() error {
157186
if a.bm == nil {
158187
return fmt.Errorf("backup manager unavailable")

db.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ func NewDB(path string) (*DB, error) {
8888

8989
func (db *DB) Close() { db.conn.Close() }
9090

91+
// IsEmpty returns true when the database has no accounts and no transactions,
92+
// i.e. the app has never been used on this machine.
93+
func (db *DB) IsEmpty() (bool, error) {
94+
var n int
95+
if err := db.conn.QueryRow("SELECT COUNT(*) FROM accounts").Scan(&n); err != nil {
96+
return false, err
97+
}
98+
if n > 0 {
99+
return false, nil
100+
}
101+
if err := db.conn.QueryRow("SELECT COUNT(*) FROM transactions").Scan(&n); err != nil {
102+
return false, err
103+
}
104+
return n == 0, nil
105+
}
106+
91107
func (db *DB) init() error {
92108
_, err := db.conn.Exec(`
93109
CREATE TABLE IF NOT EXISTS transactions (

frontend/src/routes/+page.svelte

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
SaveBackupConfig,
2424
BackupNow,
2525
RestoreFromBackup,
26+
AutoRestoreIfNeeded,
2627
} from "../../wailsjs/go/main/App";
2728
2829
interface Transaction {
@@ -858,16 +859,28 @@
858859
});
859860
860861
// ── Init ──────────────────────────────────────────────────
861-
onMount(() => {
862+
onMount(async () => {
862863
const today = new Date().toISOString().split("T")[0];
863864
eDate = today;
864865
rDate = today;
865866
tfDate = today;
867+
868+
// Load backup config first so AutoRestoreIfNeeded can use it.
869+
await loadBackupConfig();
870+
try {
871+
const restored = await AutoRestoreIfNeeded();
872+
if (restored) {
873+
backupStatus = "auto-restored from backup";
874+
}
875+
} catch (e) {
876+
console.error("auto-restore on startup failed:", e);
877+
}
878+
879+
// Load data — if a restore just happened this will see the restored DB.
866880
setPeriod("3m");
867881
loadAccounts();
868882
loadTransfers();
869883
loadInstallments();
870-
loadBackupConfig();
871884
});
872885
</script>
873886

frontend/wailsjs/go/main/App.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// This file is automatically generated. DO NOT EDIT
33
import {main} from '../models';
44

5+
export function AutoRestoreIfNeeded():Promise<boolean>;
6+
57
export function AddAccount(arg1:main.Account):Promise<number>;
68

79
export function AddInstallment(arg1:main.Installment):Promise<number>;

frontend/wailsjs/go/main/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
33
// This file is automatically generated. DO NOT EDIT
44

5+
export function AutoRestoreIfNeeded() {
6+
return window['go']['main']['App']['AutoRestoreIfNeeded']();
7+
}
8+
59
export function AddAccount(arg1) {
610
return window['go']['main']['App']['AddAccount'](arg1);
711
}

0 commit comments

Comments
 (0)