Skip to content

Commit

Permalink
stores: dont error out if we update the autopilot with the same config
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan authored and ChrisSchinnerl committed Jul 30, 2024
1 parent 1365adf commit 8d29685
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
6 changes: 6 additions & 0 deletions stores/autopilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ func TestAutopilotStore(t *testing.T) {
if updated.Config.Contracts.Amount != 99 {
t.Fatal("expected amount to be 99")
}

// update the autopilot with the same config and assert it does not fail
err = ss.UpdateAutopilot(context.Background(), updated)
if err != nil {
t.Fatal(err)
}
}
11 changes: 2 additions & 9 deletions stores/sql/mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,21 +783,14 @@ func (tx *MainDatabaseTx) UnspentSiacoinElements(ctx context.Context) (elements
}

func (tx *MainDatabaseTx) UpdateAutopilot(ctx context.Context, ap api.Autopilot) error {
res, err := tx.Exec(ctx, `
_, err := tx.Exec(ctx, `
INSERT INTO autopilots (created_at, identifier, config, current_period)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
config = VALUES(config),
current_period = VALUES(current_period)
`, time.Now(), ap.ID, (*ssql.AutopilotConfig)(&ap.Config), ap.CurrentPeriod)
if err != nil {
return err
} else if n, err := res.RowsAffected(); err != nil {
return err
} else if n != 1 && n != 2 { // 1 if inserted, 2 if updated
return fmt.Errorf("expected 1 row affected, got %v", n)
}
return nil
return err
}

func (tx *MainDatabaseTx) UpdateBucketPolicy(ctx context.Context, bucket string, bp api.BucketPolicy) error {
Expand Down
11 changes: 2 additions & 9 deletions stores/sql/sqlite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,21 +784,14 @@ func (tx *MainDatabaseTx) UnspentSiacoinElements(ctx context.Context) (elements
}

func (tx *MainDatabaseTx) UpdateAutopilot(ctx context.Context, ap api.Autopilot) error {
res, err := tx.Exec(ctx, `
_, err := tx.Exec(ctx, `
INSERT INTO autopilots (created_at, identifier, config, current_period)
VALUES (?, ?, ?, ?)
ON CONFLICT(identifier) DO UPDATE SET
config = EXCLUDED.config,
current_period = EXCLUDED.current_period
`, time.Now(), ap.ID, (*ssql.AutopilotConfig)(&ap.Config), ap.CurrentPeriod)
if err != nil {
return err
} else if n, err := res.RowsAffected(); err != nil {
return err
} else if n != 1 {
return fmt.Errorf("expected 1 row affected, got %v", n)
}
return nil
return err
}

func (tx *MainDatabaseTx) UpdateBucketPolicy(ctx context.Context, bucket string, policy api.BucketPolicy) error {
Expand Down

0 comments on commit 8d29685

Please sign in to comment.