From 8d29685ca56f35bda362efa687b53cf41ae2f715 Mon Sep 17 00:00:00 2001 From: PJ Date: Thu, 18 Jul 2024 11:43:45 +0200 Subject: [PATCH] stores: dont error out if we update the autopilot with the same config --- stores/autopilot_test.go | 6 ++++++ stores/sql/mysql/main.go | 11 ++--------- stores/sql/sqlite/main.go | 11 ++--------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/stores/autopilot_test.go b/stores/autopilot_test.go index ef94d7d8f..1ed78370c 100644 --- a/stores/autopilot_test.go +++ b/stores/autopilot_test.go @@ -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) + } } diff --git a/stores/sql/mysql/main.go b/stores/sql/mysql/main.go index a5e2de626..64b05eef4 100644 --- a/stores/sql/mysql/main.go +++ b/stores/sql/mysql/main.go @@ -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 { diff --git a/stores/sql/sqlite/main.go b/stores/sql/sqlite/main.go index 907cbd819..f9441d9cc 100644 --- a/stores/sql/sqlite/main.go +++ b/stores/sql/sqlite/main.go @@ -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 {