File tree Expand file tree Collapse file tree 5 files changed +66
-2
lines changed
Expand file tree Collapse file tree 5 files changed +66
-2
lines changed Original file line number Diff line number Diff 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+
156185func (a * App ) BackupNow () error {
157186 if a .bm == nil {
158187 return fmt .Errorf ("backup manager unavailable" )
Original file line number Diff line number Diff line change @@ -88,6 +88,22 @@ func NewDB(path string) (*DB, error) {
8888
8989func (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+
91107func (db * DB ) init () error {
92108 _ , err := db .conn .Exec (`
93109 CREATE TABLE IF NOT EXISTS transactions (
Original file line number Diff line number Diff line change 2323 SaveBackupConfig ,
2424 BackupNow ,
2525 RestoreFromBackup ,
26+ AutoRestoreIfNeeded ,
2627 } from " ../../wailsjs/go/main/App" ;
2728
2829 interface Transaction {
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
Original file line number Diff line number Diff line change 22// This file is automatically generated. DO NOT EDIT
33import { main } from '../models' ;
44
5+ export function AutoRestoreIfNeeded ( ) :Promise < boolean > ;
6+
57export function AddAccount ( arg1 :main . Account ) :Promise < number > ;
68
79export function AddInstallment ( arg1 :main . Installment ) :Promise < number > ;
Original file line number Diff line number Diff line change 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+
59export function AddAccount ( arg1 ) {
610 return window [ 'go' ] [ 'main' ] [ 'App' ] [ 'AddAccount' ] ( arg1 ) ;
711}
You can’t perform that action at this time.
0 commit comments