Add data migration export/import endpoints
All checks were successful
Backend Build and Test / build (push) Successful in 40s

Add GET /api/export to the Go backend that dumps all users, allowances,
history, and tasks (including completed) as a single JSON snapshot.

Add POST /api/import to the Spring backend that accepts the same JSON,
wipes existing data, inserts all records with original IDs preserved via
native SQL, and resets PostgreSQL sequences to avoid future collisions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-03-01 16:17:37 +01:00
parent 29284f6eac
commit a08a462e22
6 changed files with 248 additions and 1 deletions

View File

@@ -779,3 +779,59 @@ func (db *Db) TransferAllowance(fromId int, toId int, amount float64) error {
return tx.Commit()
}
func (db *Db) ExportAllData() (*ExportData, error) {
var err error
data := &ExportData{
Users: make([]ExportUser, 0),
Allowances: make([]ExportAllowance, 0),
History: make([]ExportHistory, 0),
Tasks: make([]ExportTask, 0),
}
for row := range db.db.Query("select id, name, balance, weight from users").Range(&err) {
u := ExportUser{}
if err = row.Scan(&u.ID, &u.Name, &u.Balance, &u.Weight); err != nil {
return nil, err
}
data.Users = append(data.Users, u)
}
if err != nil {
return nil, err
}
for row := range db.db.Query("select id, user_id, name, target, balance, weight, colour from allowances").Range(&err) {
a := ExportAllowance{}
if err = row.Scan(&a.ID, &a.UserID, &a.Name, &a.Target, &a.Balance, &a.Weight, &a.Colour); err != nil {
return nil, err
}
data.Allowances = append(data.Allowances, a)
}
if err != nil {
return nil, err
}
for row := range db.db.Query("select id, user_id, timestamp, amount, description from history").Range(&err) {
h := ExportHistory{}
if err = row.Scan(&h.ID, &h.UserID, &h.Timestamp, &h.Amount, &h.Description); err != nil {
return nil, err
}
data.History = append(data.History, h)
}
if err != nil {
return nil, err
}
for row := range db.db.Query("select id, name, reward, assigned, schedule, completed, next_run from tasks").Range(&err) {
t := ExportTask{}
if err = row.Scan(&t.ID, &t.Name, &t.Reward, &t.Assigned, &t.Schedule, &t.Completed, &t.NextRun); err != nil {
return nil, err
}
data.Tasks = append(data.Tasks, t)
}
if err != nil {
return nil, err
}
return data, nil
}