From e3e6fc3af10e4ccd53504da29dfca5c7c153da28 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Mon, 20 Mar 2023 23:50:12 +0100 Subject: [PATCH] Add foreign keys and an index to StructuredError(Attributes) These changes will ensure a better data consistency and faster access. We further ensure that records should be deleted properly. --- app/models/structured_error.rb | 2 +- app/models/submission.rb | 2 +- ...230320220011_add_foreign_keys_to_structured_errors.rb | 8 ++++++++ ...eign_keys_and_index_to_structured_error_attributes.rb | 9 +++++++++ db/schema.rb | 8 +++++++- 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20230320220011_add_foreign_keys_to_structured_errors.rb create mode 100644 db/migrate/20230320220012_add_foreign_keys_and_index_to_structured_error_attributes.rb diff --git a/app/models/structured_error.rb b/app/models/structured_error.rb index 3291423d..f3d8f24c 100644 --- a/app/models/structured_error.rb +++ b/app/models/structured_error.rb @@ -4,7 +4,7 @@ class StructuredError < ApplicationRecord belongs_to :error_template belongs_to :submission - has_many :structured_error_attributes + has_many :structured_error_attributes, dependent: :destroy def self.create_from_template(template, message_buffer, submission) create( diff --git a/app/models/submission.rb b/app/models/submission.rb index abb62dcd..7af947cc 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -15,7 +15,7 @@ class Submission < ApplicationRecord belongs_to :study_group, optional: true has_many :testruns - has_many :structured_errors + has_many :structured_errors, dependent: :destroy has_many :comments, through: :files belongs_to :external_users, lambda { diff --git a/db/migrate/20230320220011_add_foreign_keys_to_structured_errors.rb b/db/migrate/20230320220011_add_foreign_keys_to_structured_errors.rb new file mode 100644 index 00000000..291e410a --- /dev/null +++ b/db/migrate/20230320220011_add_foreign_keys_to_structured_errors.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddForeignKeysToStructuredErrors < ActiveRecord::Migration[7.0] + def change + add_foreign_key :structured_errors, :submissions + add_foreign_key :structured_errors, :error_templates + end +end diff --git a/db/migrate/20230320220012_add_foreign_keys_and_index_to_structured_error_attributes.rb b/db/migrate/20230320220012_add_foreign_keys_and_index_to_structured_error_attributes.rb new file mode 100644 index 00000000..fdac55e3 --- /dev/null +++ b/db/migrate/20230320220012_add_foreign_keys_and_index_to_structured_error_attributes.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddForeignKeysAndIndexToStructuredErrorAttributes < ActiveRecord::Migration[7.0] + def change + add_foreign_key :structured_error_attributes, :structured_errors + add_foreign_key :structured_error_attributes, :error_template_attributes + add_index :structured_error_attributes, :structured_error_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 080612d7..2f935968 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_03_14_084733) do +ActiveRecord::Schema[7.0].define(version: 2023_03_20_220012) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" enable_extension "pgcrypto" @@ -420,6 +420,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_14_084733) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "match" + t.index ["structured_error_id"], name: "index_structured_error_attributes_on_structured_error_id" end create_table "structured_errors", id: :serial, force: :cascade do |t| @@ -600,6 +601,11 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_14_084733) do add_foreign_key "exercise_tips", "exercises" add_foreign_key "exercise_tips", "tips" add_foreign_key "remote_evaluation_mappings", "study_groups" + add_foreign_key "structured_error_attributes", "error_template_attributes" + add_foreign_key "structured_error_attributes", "structured_errors" + add_foreign_key "structured_errors", "error_templates" + add_foreign_key "structured_errors", "error_templates", name: "structured_errors_error_templates_id_fk" + add_foreign_key "structured_errors", "submissions" add_foreign_key "submissions", "study_groups" add_foreign_key "subscriptions", "study_groups" add_foreign_key "testrun_execution_environments", "execution_environments"