Skip to content

Commit

Permalink
tx for process reorder database update
Browse files Browse the repository at this point in the history
  • Loading branch information
doorbash committed Nov 29, 2022
1 parent f20b204 commit 15b5d1b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
9 changes: 1 addition & 8 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,5 @@ func (b *App) GetLogs(id int64) []*Log {
}

func (b *App) ProcessesReorder(ids []int64) bool {
// log.Println(ids)
for i, v := range ids {
err := b.dbHandler.UpdateProcessOrderId(b.ctx, v, i+1)
if err != nil {
return false
}
}
return true
return b.dbHandler.UpdateProcessesOrderId(b.ctx, ids) == nil
}
21 changes: 13 additions & 8 deletions db_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,26 @@ func (d *DBHandler) UpdateProcess(ctx context.Context, p *Process) error {
return err
}

func (d *DBHandler) UpdateProcessOrderId(ctx context.Context, id int64, orderId int) error {
func (d *DBHandler) UpdateProcessesOrderId(ctx context.Context, ids []int64) error {
db, err := sql.Open("sqlite3", d.dbPath)
if err != nil {
return err
}
defer db.Close()
ctx, cancel := context.WithTimeout(ctx, d.contextTimeout)
defer cancel()
_, err = db.ExecContext(
ctx,
"update processes set order_id = ? where id = ?",
orderId,
id,
)
return err
tx, err := db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
defer tx.Rollback()
for i, v := range ids {
_, err = tx.ExecContext(ctx, "update processes set order_id = ? where id = ?", i+1, v)
if err != nil {
return err
}
}
return tx.Commit()
}

func (d *DBHandler) DeleteProcess(ctx context.Context, id int64) error {
Expand Down
2 changes: 1 addition & 1 deletion wails.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"Info": {
"companyName": "mpm",
"productName": "mpm",
"productVersion": "1.1.2",
"productVersion": "1.1.3",
"copyright": "Copyright 2022",
"comments": "Built using Wails (https://wails.app)"
},
Expand Down

0 comments on commit 15b5d1b

Please sign in to comment.