Commit 17896b3b authored by Igor Drozdov's avatar Igor Drozdov

Add foreign keys to lfs_objects_projects

It's added without a validation until the data is cleaned up
parent 15448f95
......@@ -7,7 +7,7 @@ class LfsObject < ApplicationRecord
include ObjectStorage::BackgroundMove
include FileStoreMounter
has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :lfs_objects_projects
has_many :projects, -> { distinct }, through: :lfs_objects_projects
scope :with_files_stored_locally, -> { where(file_store: LfsObjectUploader::Store::LOCAL) }
......
......@@ -286,7 +286,7 @@ class Project < ApplicationRecord
has_many :users_star_projects
has_many :starrers, through: :users_star_projects, source: :user
has_many :releases
has_many :lfs_objects_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :lfs_objects_projects
has_many :lfs_objects, -> { distinct }, through: :lfs_objects_projects
has_many :lfs_file_locks
has_many :project_group_links
......
# frozen_string_literal: true
class AddForeignKeyToLfsObjectsProjects < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
def up
add_concurrent_foreign_key :lfs_objects_projects, :lfs_objects, column: :lfs_object_id, on_delete: :restrict, validate: false
add_concurrent_foreign_key :lfs_objects_projects, :projects, column: :project_id, on_delete: :cascade, validate: false
end
def down
with_lock_retries do
remove_foreign_key :lfs_objects_projects, column: :lfs_object_id
remove_foreign_key :lfs_objects_projects, column: :project_id
end
end
end
8f746d7eb604ae31a5941840d6a078eae2e4fa59b7185bf8cc0db9c55b463c33
\ No newline at end of file
......@@ -25401,6 +25401,9 @@ ALTER TABLE ONLY notes
ALTER TABLE ONLY members
ADD CONSTRAINT fk_2e88fb7ce9 FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY lfs_objects_projects
ADD CONSTRAINT fk_2eb33f7a78 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE NOT VALID;
ALTER TABLE ONLY lists
ADD CONSTRAINT fk_30f2a831f4 FOREIGN KEY (iteration_id) REFERENCES sprints(id) ON DELETE CASCADE;
......@@ -25713,6 +25716,9 @@ ALTER TABLE ONLY bulk_import_entities
ALTER TABLE ONLY users
ADD CONSTRAINT fk_a4b8fefe3e FOREIGN KEY (managing_group_id) REFERENCES namespaces(id) ON DELETE SET NULL;
ALTER TABLE ONLY lfs_objects_projects
ADD CONSTRAINT fk_a56e02279c FOREIGN KEY (lfs_object_id) REFERENCES lfs_objects(id) ON DELETE RESTRICT NOT VALID;
ALTER TABLE ONLY dast_profiles_pipelines
ADD CONSTRAINT fk_a60cad829d FOREIGN KEY (ci_pipeline_id) REFERENCES ci_pipelines(id) ON DELETE CASCADE;
......@@ -178,4 +178,17 @@ RSpec.describe LfsObject do
expect(described_class.calculate_oid(path)).to eq expected
end
end
context 'when an lfs object is associated with a project' do
let!(:lfs_object) { create(:lfs_object) }
let!(:lfs_object_project) { create(:lfs_objects_project, lfs_object: lfs_object) }
it 'cannot be deleted' do
expect { lfs_object.destroy! }.to raise_error(ActiveRecord::InvalidForeignKey)
lfs_object_project.destroy!
expect { lfs_object.destroy! }.not_to raise_error
end
end
end
......@@ -6452,6 +6452,10 @@ RSpec.describe Project, factory_default: :keep do
expect(subject).to eq([lfs_object.oid])
end
end
it 'lfs_objects_projects associations are deleted along with project' do
expect { project.delete }.to change(LfsObjectsProject, :count).by(-2)
end
end
context 'when project has no associated LFS objects' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment