Files
ubicloud/migrate/20250227_id_based_locations.rb
Furkan Sahin 056e11e0a4 Make location references a foreign key, create indexes - Mig 1
This commit adds the location foreign_keys to all of the models that
uses location. This way, we will not be matching the text values but
make use of the database relationship.
We are refilling the location_ids based on the location column of the
entities.

We are also adding a new index on project_id, location_id, name because we
already had an index on project_id, location, name and it will be
dropped once the location column is dropped.
2025-03-23 15:48:19 +01:00

28 lines
794 B
Ruby

# frozen_string_literal: true
Sequel.migration do
up do
%i[vm_host vm vm_pool private_subnet firewall postgres_resource minio_cluster inference_endpoint kubernetes_cluster].each do |table|
alter_table(table) do
add_foreign_key :location_id, :location, type: :uuid
end
from(table).update(location_id: from(:location)
.where(name: Sequel[table][:location])
.select(:id))
end
end
down do
%i[vm_host vm vm_pool private_subnet firewall postgres_resource minio_cluster inference_endpoint kubernetes_cluster].each do |table|
from(table).update(location: from(:location)
.where(id: Sequel[table][:location_id])
.select(:name))
alter_table(table) do
drop_column :location_id
end
end
end
end