diff --git a/app/models/repository.rb b/app/models/repository.rb index 9056bf59b..5a6ee251e 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -7,8 +7,8 @@ class Repository < ApplicationRecord foreign_key: :created_by_id, class_name: 'User', optional: true - has_many :repository_columns - has_many :repository_rows + has_many :repository_columns, dependent: :destroy + has_many :repository_rows, dependent: :destroy has_many :repository_table_states, inverse_of: :repository, dependent: :destroy has_many :report_elements, inverse_of: :repository, dependent: :destroy @@ -60,7 +60,7 @@ class Repository < ApplicationRecord def self.name_like(query) where('repositories.name ILIKE ?', "%#{query}%") end - + def importable_repository_fields fields = {} # First and foremost add record name diff --git a/db/migrate/20180518113526_remove_repository_rows_and_columns_without_repository.rb b/db/migrate/20180518113526_remove_repository_rows_and_columns_without_repository.rb new file mode 100644 index 000000000..785f17c2b --- /dev/null +++ b/db/migrate/20180518113526_remove_repository_rows_and_columns_without_repository.rb @@ -0,0 +1,7 @@ +class RemoveRepositoryRowsAndColumnsWithoutRepository < ActiveRecord::Migration[5.1] + def up + repository_ids = Repository.select(:id) + RepositoryRow.where.not(repository_id: repository_ids).delete_all + RepositoryColumn.where.not(repository_id: repository_ids).delete_all + end +end diff --git a/db/schema.rb b/db/schema.rb index 9a5ae98b2..f22e76ba0 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.define(version: 20180417062042) do +ActiveRecord::Schema.define(version: 20180518113526) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"