diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-08 16:39:29 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-08 16:39:29 +0200 |
commit | 479a15e722d45172dbd7c47ef4a3c4fae9051fbe (patch) | |
tree | b6ca35290a543441593c83eb08c7b63d84f4c65a /spec | |
parent | 0a46bf70b658e0a63fe980b77d215c8a9f4f4dc5 (diff) | |
download | gitlab-ce-479a15e722d45172dbd7c47ef4a3c4fae9051fbe.tar.gz |
Add database foreign key between pipelines and builds
Diffstat (limited to 'spec')
-rw-r--r-- | spec/migrations/add_pipeline_build_foreign_key_spec.rb | 30 | ||||
-rw-r--r-- | spec/support/helpers/migrations_helpers.rb | 11 |
2 files changed, 41 insertions, 0 deletions
diff --git a/spec/migrations/add_pipeline_build_foreign_key_spec.rb b/spec/migrations/add_pipeline_build_foreign_key_spec.rb new file mode 100644 index 00000000000..7358b1d265d --- /dev/null +++ b/spec/migrations/add_pipeline_build_foreign_key_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' +require Rails.root.join('db', 'migrate', '20180420010016_add_pipeline_build_foreign_key.rb') + +describe AddPipelineBuildForeignKey, :migration do + let(:namespaces) { table(:namespaces) } + let(:projects) { table(:projects) } + let(:pipelines) { table(:ci_pipelines) } + let(:builds) { table(:ci_builds) } + + before do + namespaces.create(id: 10, name: 'gitlab-org', path: 'gitlab-org') + projects.create!(id: 11, namespace_id: 10, name: 'gitlab', path: 'gitlab') + pipelines.create!(id: 12, project_id: 11, ref: 'master', sha: 'adf43c3a') + + builds.create!(id: 101, commit_id: 12, project_id: 11) + builds.create!(id: 102, commit_id: 222, project_id: 11) + builds.create!(id: 103, commit_id: 333, project_id: 11) + builds.create!(id: 104, commit_id: 12, project_id: 11) + end + + it 'adds foreign key after removing orphans' do + expect(builds.all.count).to eq 4 + expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_falsey + + migrate! + + expect(builds.all.pluck(:id)).to eq [101, 104] + expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_truthy + end +end diff --git a/spec/support/helpers/migrations_helpers.rb b/spec/support/helpers/migrations_helpers.rb index 5d6f662e8fe..bff2c31b0ca 100644 --- a/spec/support/helpers/migrations_helpers.rb +++ b/spec/support/helpers/migrations_helpers.rb @@ -24,6 +24,17 @@ module MigrationsHelpers end end + def foreign_key_exists?(source, target = nil, column: nil) + ActiveRecord::Base.connection.foreign_keys(source).any? do |key| + if column + key.options[:column].to_s == column.to_s + else + key.to_table.to_s == target.to_s + end + end + end + + def reset_column_in_all_models clear_schema_cache! |