diff --git a/Gemfile b/Gemfile index 91e6a042a..78d67fb94 100644 --- a/Gemfile +++ b/Gemfile @@ -77,6 +77,8 @@ gem 'devise-async', git: 'https://github.com/mhfs/devise-async.git', branch: 'devise-4.x' +gem 'discard', '~> 1.0' + gem 'ruby-graphviz', '~> 1.2' # Graphviz for rails gem 'tinymce-rails', '~> 4.6.4' # Rich text editor diff --git a/Gemfile.lock b/Gemfile.lock index afcb41b51..99dc558be 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -204,6 +204,8 @@ GEM actionmailer (>= 4.1.0) devise (>= 4.0.0) diff-lcs (1.3) + discard (1.0.0) + activerecord (>= 4.2, < 6) docile (1.1.5) erubi (1.7.0) execjs (2.7.0) @@ -547,6 +549,7 @@ DEPENDENCIES devise-async! devise_invitable devise_security_extension! + discard (~> 1.0) factory_bot_rails faker figaro diff --git a/app/models/repository.rb b/app/models/repository.rb index 5a6ee251e..174b88ac4 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1,7 +1,8 @@ class Repository < ApplicationRecord include SearchableModel include RepositoryImportParser - + include Discard::Model + belongs_to :team, optional: true belongs_to :created_by, foreign_key: :created_by_id, @@ -22,6 +23,8 @@ class Repository < ApplicationRecord validates :team, presence: true validates :created_by, presence: true + default_scope -> { kept } + def self.search( user, query = nil, 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 index 785f17c2b..69adf5698 100644 --- a/db/migrate/20180518113526_remove_repository_rows_and_columns_without_repository.rb +++ b/db/migrate/20180518113526_remove_repository_rows_and_columns_without_repository.rb @@ -1,7 +1,9 @@ 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 + if column_exists?(:repositories, :discarded_at) + 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 end diff --git a/db/migrate/20180524091143_add_discard_to_repositories.rb b/db/migrate/20180524091143_add_discard_to_repositories.rb new file mode 100644 index 000000000..e1706ccf5 --- /dev/null +++ b/db/migrate/20180524091143_add_discard_to_repositories.rb @@ -0,0 +1,6 @@ +class AddDiscardToRepositories < ActiveRecord::Migration[5.1] + def change + add_column :repositories, :discarded_at, :datetime + add_index :repositories, :discarded_at + end +end diff --git a/db/schema.rb b/db/schema.rb index f22e76ba0..ce0aa9260 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: 20180518113526) do +ActiveRecord::Schema.define(version: 20180524091143) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -363,6 +363,8 @@ ActiveRecord::Schema.define(version: 20180518113526) do t.string "name" t.datetime "created_at" t.datetime "updated_at" + t.datetime "discarded_at" + t.index ["discarded_at"], name: "index_repositories_on_discarded_at" t.index ["team_id"], name: "index_repositories_on_team_id" end diff --git a/lib/tasks/clear_discarded_repositories.rake b/lib/tasks/clear_discarded_repositories.rake new file mode 100644 index 000000000..a791ed68b --- /dev/null +++ b/lib/tasks/clear_discarded_repositories.rake @@ -0,0 +1,6 @@ +namespace :clear_discarded_repositories do + desc 'Removes all discarded repositories' + task run: :environment do + Repository.with_discarded.discarded.destroy_all + end +end