From fb7ed16df97bde2578be4ee56a73a7712b3214b9 Mon Sep 17 00:00:00 2001 From: PJ Date: Thu, 14 Nov 2024 10:38:27 +0100 Subject: [PATCH] stores: update migration --- internal/sql/migrations.go | 17 ++++++++++++++++- stores/sql/main.go | 2 +- stores/sql/mysql/main.go | 5 +++++ .../main/migration_00027_autopilot_1.sql | 2 ++ stores/sql/mysql/migrations/main/schema.sql | 11 ----------- stores/sql/sqlite/main.go | 5 +++++ 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/internal/sql/migrations.go b/internal/sql/migrations.go index 5c86e5b3a..f8fa9831d 100644 --- a/internal/sql/migrations.go +++ b/internal/sql/migrations.go @@ -34,6 +34,7 @@ type ( MainMigrator interface { Migrator + InitAutopilot(ctx context.Context, tx Tx) error InsertDirectories(ctx context.Context, tx Tx, bucket, path string) (int64, error) MakeDirsForPath(ctx context.Context, tx Tx, path string) (int64, error) UpdateSetting(ctx context.Context, tx Tx, key, value string) error @@ -363,6 +364,11 @@ var ( return fmt.Errorf("failed to migrate: %v", err) } + // make sure the autopilot config is initialized + if err := m.InitAutopilot(ctx, tx); err != nil { + return fmt.Errorf("failed to initialize autopilot: %w", err) + } + // fetch existing autopilot and override the blank config var cfgraw []byte var period uint64 @@ -373,7 +379,16 @@ var ( } else if err := json.Unmarshal(cfgraw, &cfg); err != nil { log.Warnf("existing autopilot config not valid JSON, err %v", err) } else { - res, err := tx.Exec(ctx, `UPDATE autopilot SET current_period = ?, contracts_set = ?, contracts_amount = ?, contracts_period = ?, contracts_renew_window = ?, contracts_download = ?, contracts_upload = ?, contracts_storage = ?, contracts_prune = ?, hosts_allow_redundant_ips = ?, hosts_max_downtime_hours = ?, hosts_min_protocol_version = ?, hosts_max_consecutive_scan_failures = ? WHERE id = ?`, + var enabled bool + if err := cfg.Contracts.Validate(); err != nil { + log.Warnf("existing contracts config is invalid, autopilot will be disabled, err: %v", err) + } else if err := cfg.Hosts.Validate(); err != nil { + log.Warnf("existing hosts config is invalid, autopilot will be disabled, err: %v", err) + } else { + enabled = true + } + res, err := tx.Exec(ctx, `UPDATE autopilot_config SET enabled = ?, current_period = ?, contracts_set = ?, contracts_amount = ?, contracts_period = ?, contracts_renew_window = ?, contracts_download = ?, contracts_upload = ?, contracts_storage = ?, contracts_prune = ?, hosts_allow_redundant_ips = ?, hosts_max_downtime_hours = ?, hosts_min_protocol_version = ?, hosts_max_consecutive_scan_failures = ? WHERE id = ?`, + enabled, period, cfg.Contracts.Set, cfg.Contracts.Amount, diff --git a/stores/sql/main.go b/stores/sql/main.go index 262b19774..2c90621b4 100644 --- a/stores/sql/main.go +++ b/stores/sql/main.go @@ -2180,7 +2180,7 @@ WHERE fcid = ?`, func UpdateAutopilot(ctx context.Context, tx sql.Tx, ap api.Autopilot) error { _, err := tx.Exec(ctx, ` -UPDATE autopilot +UPDATE autopilot_config SET enabled = ?, current_period = ?, contracts_set = ?, diff --git a/stores/sql/mysql/main.go b/stores/sql/mysql/main.go index 642a79f69..23ea3f5a4 100644 --- a/stores/sql/mysql/main.go +++ b/stores/sql/mysql/main.go @@ -71,6 +71,11 @@ func (b *MainDatabase) LoadSlabBuffers(ctx context.Context) ([]ssql.LoadedSlabBu return ssql.LoadSlabBuffers(ctx, b.db) } +func (b *MainDatabase) InitAutopilot(ctx context.Context, tx sql.Tx) error { + mtx := b.wrapTxn(tx) + return mtx.InitAutopilot(ctx) +} + func (b *MainDatabase) InsertDirectories(ctx context.Context, tx sql.Tx, bucket, path string) (int64, error) { mtx := b.wrapTxn(tx) return mtx.InsertDirectories(ctx, bucket, path) diff --git a/stores/sql/mysql/migrations/main/migration_00027_autopilot_1.sql b/stores/sql/mysql/migrations/main/migration_00027_autopilot_1.sql index 574e1dfb6..4950d8e54 100644 --- a/stores/sql/mysql/migrations/main/migration_00027_autopilot_1.sql +++ b/stores/sql/mysql/migrations/main/migration_00027_autopilot_1.sql @@ -1,8 +1,10 @@ -- remove references to autopilots table ALTER TABLE host_checks DROP FOREIGN KEY fk_host_checks_autopilot; +ALTER TABLE host_checks DROP FOREIGN KEY fk_host_checks_host; ALTER TABLE host_checks DROP COLUMN db_autopilot_id; ALTER TABLE host_checks DROP INDEX idx_host_checks_id; ALTER TABLE host_checks ADD UNIQUE INDEX idx_host_checks_id (db_host_id); +ALTER TABLE host_checks ADD CONSTRAINT fk_host_checks_host FOREIGN KEY (db_host_id) REFERENCES hosts(id) ON DELETE CASCADE; -- create autopilot table & insert blank state object CREATE TABLE `autopilot_config` ( diff --git a/stores/sql/mysql/migrations/main/schema.sql b/stores/sql/mysql/migrations/main/schema.sql index a070be987..36a96fa18 100644 --- a/stores/sql/mysql/migrations/main/schema.sql +++ b/stores/sql/mysql/migrations/main/schema.sql @@ -1,14 +1,3 @@ --- dbAutopilot -CREATE TABLE `autopilots` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `created_at` datetime(3) DEFAULT NULL, - `identifier` varchar(191) NOT NULL, - `config` longtext, - `current_period` bigint unsigned DEFAULT '0', - PRIMARY KEY (`id`), - UNIQUE KEY `identifier` (`identifier`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - -- dbBucket CREATE TABLE `buckets` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, diff --git a/stores/sql/sqlite/main.go b/stores/sql/sqlite/main.go index aa22ed60e..dbe5931ac 100644 --- a/stores/sql/sqlite/main.go +++ b/stores/sql/sqlite/main.go @@ -70,6 +70,11 @@ func (b *MainDatabase) LoadSlabBuffers(ctx context.Context) ([]ssql.LoadedSlabBu return ssql.LoadSlabBuffers(ctx, b.db) } +func (b *MainDatabase) InitAutopilot(ctx context.Context, tx sql.Tx) error { + mtx := b.wrapTxn(tx) + return mtx.InitAutopilot(ctx) +} + func (b *MainDatabase) InsertDirectories(ctx context.Context, tx sql.Tx, bucket, path string) (int64, error) { mtx := b.wrapTxn(tx) return mtx.InsertDirectories(ctx, bucket, path)