Change exposed_ports to array

This commit is contained in:
Sebastian Serth
2021-10-17 18:59:03 +02:00
parent 064c55b711
commit 06ef4430f5
13 changed files with 75 additions and 40 deletions

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
class ChangeTypeOfExposedPortsInExecutionEnvironment < ActiveRecord::Migration[6.1]
# rubocop:disable Rails/SkipsModelValidations:
def up
rename_column :execution_environments, :exposed_ports, :exposed_ports_migration
add_column :execution_environments, :exposed_ports, :integer, array: true, default: [], nil: true
ExecutionEnvironment.all.each do |execution_environment|
next if execution_environment.exposed_ports_migration.nil?
cleaned = execution_environment.exposed_ports_migration.scan(/\d+/)
list = cleaned.map(&:to_i).uniq.sort
execution_environment.update_columns(exposed_ports: list)
end
remove_column :execution_environments, :exposed_ports_migration
end
def down
rename_column :execution_environments, :exposed_ports, :exposed_ports_migration
add_column :execution_environments, :exposed_ports, :string
ExecutionEnvironment.all.each do |execution_environment|
next if execution_environment.exposed_ports_migration.empty?
list = execution_environment.exposed_ports_migration
if list.empty?
execution_environment.update_columns(exposed_ports: nil)
else
execution_environment.update_columns(exposed_ports: list.join(','))
end
end
remove_column :execution_environments, :exposed_ports_migration
end
# rubocop:enable Rails/SkipsModelValidations:
end

View File

@ -1,17 +0,0 @@
# frozen_string_literal: true
class CleanExposedPortsInExecutionEnvironment < ActiveRecord::Migration[6.1]
def change
ExecutionEnvironment.all.each do |execution_environment|
next if execution_environment.exposed_ports.nil?
cleaned = execution_environment.exposed_ports.gsub(/[[:space:]]/, '')
list = cleaned.split(',').map(&:to_i).uniq
if list.empty?
execution_environment.update(exposed_ports: nil)
else
execution_environment.update(exposed_ports: list.join(','))
end
end
end
end

View File

@ -104,7 +104,6 @@ ActiveRecord::Schema.define(version: 2021_06_02_071834) do
t.string "test_command", limit: 255
t.string "testing_framework", limit: 255
t.text "help"
t.string "exposed_ports", limit: 255
t.integer "permitted_execution_time"
t.integer "user_id"
t.string "user_type", limit: 255
@ -113,6 +112,7 @@ ActiveRecord::Schema.define(version: 2021_06_02_071834) do
t.integer "memory_limit"
t.boolean "network_enabled"
t.integer "cpu_limit", default: 20, null: false
t.integer "exposed_ports", default: [], array: true
end
create_table "exercise_collection_items", id: :serial, force: :cascade do |t|