Skip to content

Commit

Permalink
stores: update migration
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Nov 14, 2024
1 parent eaea057 commit fb7ed16
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
17 changes: 16 additions & 1 deletion internal/sql/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion stores/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?,
Expand Down
5 changes: 5 additions & 0 deletions stores/sql/mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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` (
Expand Down
11 changes: 0 additions & 11 deletions stores/sql/mysql/migrations/main/schema.sql
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 5 additions & 0 deletions stores/sql/sqlite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fb7ed16

Please sign in to comment.