Skip to content

Commit 8d29685

Browse files
peterjanChrisSchinnerl
authored andcommitted
stores: dont error out if we update the autopilot with the same config
1 parent 1365adf commit 8d29685

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

stores/autopilot_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ func TestAutopilotStore(t *testing.T) {
8585
if updated.Config.Contracts.Amount != 99 {
8686
t.Fatal("expected amount to be 99")
8787
}
88+
89+
// update the autopilot with the same config and assert it does not fail
90+
err = ss.UpdateAutopilot(context.Background(), updated)
91+
if err != nil {
92+
t.Fatal(err)
93+
}
8894
}

stores/sql/mysql/main.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -783,21 +783,14 @@ func (tx *MainDatabaseTx) UnspentSiacoinElements(ctx context.Context) (elements
783783
}
784784

785785
func (tx *MainDatabaseTx) UpdateAutopilot(ctx context.Context, ap api.Autopilot) error {
786-
res, err := tx.Exec(ctx, `
786+
_, err := tx.Exec(ctx, `
787787
INSERT INTO autopilots (created_at, identifier, config, current_period)
788788
VALUES (?, ?, ?, ?)
789789
ON DUPLICATE KEY UPDATE
790790
config = VALUES(config),
791791
current_period = VALUES(current_period)
792792
`, time.Now(), ap.ID, (*ssql.AutopilotConfig)(&ap.Config), ap.CurrentPeriod)
793-
if err != nil {
794-
return err
795-
} else if n, err := res.RowsAffected(); err != nil {
796-
return err
797-
} else if n != 1 && n != 2 { // 1 if inserted, 2 if updated
798-
return fmt.Errorf("expected 1 row affected, got %v", n)
799-
}
800-
return nil
793+
return err
801794
}
802795

803796
func (tx *MainDatabaseTx) UpdateBucketPolicy(ctx context.Context, bucket string, bp api.BucketPolicy) error {

stores/sql/sqlite/main.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -784,21 +784,14 @@ func (tx *MainDatabaseTx) UnspentSiacoinElements(ctx context.Context) (elements
784784
}
785785

786786
func (tx *MainDatabaseTx) UpdateAutopilot(ctx context.Context, ap api.Autopilot) error {
787-
res, err := tx.Exec(ctx, `
787+
_, err := tx.Exec(ctx, `
788788
INSERT INTO autopilots (created_at, identifier, config, current_period)
789789
VALUES (?, ?, ?, ?)
790790
ON CONFLICT(identifier) DO UPDATE SET
791791
config = EXCLUDED.config,
792792
current_period = EXCLUDED.current_period
793793
`, time.Now(), ap.ID, (*ssql.AutopilotConfig)(&ap.Config), ap.CurrentPeriod)
794-
if err != nil {
795-
return err
796-
} else if n, err := res.RowsAffected(); err != nil {
797-
return err
798-
} else if n != 1 {
799-
return fmt.Errorf("expected 1 row affected, got %v", n)
800-
}
801-
return nil
794+
return err
802795
}
803796

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

0 commit comments

Comments
 (0)