Skip to content

Commit dc9d321

Browse files
committed
Add constraints to Runner model and table
The runners table lacked a few important uniqueness constraints and indices, too. Specifically, the runner_id is expected to be unique (at least per Runner::Strategy). Since CodeOcean currently supports only one Runner::Strategy concurrently, we set the constraint (and disallow NULL values). Furthermore, we link Runner properly to ExecutionEnvironments. Since the table primarily serves as a "cache" for previous runner assignments, we can easily drop those runners for non-present ExecutionEnvironments.
1 parent dfaf90e commit dc9d321

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

app/models/execution_environment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ExecutionEnvironment < ApplicationRecord
1515
belongs_to :file_type
1616
has_many :error_templates
1717
has_many :testrun_execution_environments, dependent: :destroy
18+
has_many :runners, dependent: :destroy
1819

1920
scope :with_exercises, -> { where('id IN (SELECT execution_environment_id FROM exercises)') }
2021

app/models/runner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Runner < ApplicationRecord
1111
RESERVATION_BUFFER = 1.second
1212

1313
before_validation :request_id
14-
validates :runner_id, presence: true
15-
14+
validates :runner_id, presence: true, uniqueness: true
15+
validates :execution_environment_id, uniqueness: {scope: %i[contributor_id contributor_type]}
1616
attr_accessor :strategy
1717

1818
def self.strategy_class
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
class AddConstraintsToRunner < ActiveRecord::Migration[8.0]
4+
class ExecutionEnvironment < ApplicationRecord
5+
has_many :runners
6+
end
7+
8+
class Runner < ApplicationRecord
9+
belongs_to :execution_environment
10+
end
11+
12+
def change
13+
change_column_null :runners, :runner_id, false
14+
change_column_null :runners, :execution_environment_id, false
15+
change_column_null :runners, :contributor_id, false
16+
change_column_null :runners, :contributor_type, false
17+
18+
add_index :runners, :runner_id, unique: true
19+
20+
up_only do
21+
# We cannot add a foreign key to a table that has rows that violate the constraint.
22+
Runner.where.not(execution_environment_id: ExecutionEnvironment.select(:id)).delete_all
23+
end
24+
add_foreign_key :runners, :execution_environments
25+
26+
add_index :runners, %i[runner_id contributor_id contributor_type], unique: true
27+
end
28+
end

db/schema.rb

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)