ubicloud/migrate/20250908_backfill_vring_workers_and_add_constraint.rb
Hadi Moshayedi 9310b61ee7 Backfill vring_workers and add constraint.
Populates `vring_workers` column of existing ubiblk backed storage
volumes with the default value, and adds a constraint to ensure this
column is a positive integer for ubiblk volumes.
2025-09-09 11:58:45 -07:00

28 lines
841 B
Ruby

# frozen_string_literal: true
Sequel.migration do
up do
run <<~SQL
UPDATE vm_storage_volume vsv
SET vring_workers = GREATEST(1, vm.vcpus / 2)
FROM vm
WHERE vsv.vm_id = vm.id
AND vsv.vring_workers IS NULL
AND vsv.vhost_block_backend_id IS NOT NULL;
SQL
alter_table(:vm_storage_volume) do
add_constraint(:vring_workers_positive_if_ubiblk,
Sequel.lit("vhost_block_backend_id IS NULL OR (vring_workers IS NOT NULL AND vring_workers > 0)"))
add_constraint(:vring_workers_null_if_not_ubiblk,
Sequel.lit("vhost_block_backend_id IS NOT NULL OR vring_workers IS NULL"))
end
end
down do
alter_table(:vm_storage_volume) do
drop_constraint(:vring_workers_positive_if_ubiblk)
drop_constraint(:vring_workers_null_if_not_ubiblk)
end
end
end