optimize query, only select relevant db columns

This commit is contained in:
Alexis Reigel 2017-07-26 10:24:46 +02:00
parent 4e53131f7d
commit 9488b7780e
2 changed files with 54 additions and 11 deletions

View file

@ -7,6 +7,7 @@ module Gitlab
def run
GpgSignature
.select(:id, :commit_sha, :project_id)
.where('gpg_key_id IS NULL OR valid_signature = ?', false)
.where(gpg_key_primary_keyid: @gpg_key.primary_keyid)
.find_each do |gpg_signature|

View file

@ -20,7 +20,7 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
end
before do
allow_any_instance_of(GpgSignature).to receive(:commit).and_return(commit)
allow_any_instance_of(Project).to receive(:commit).and_return(commit)
end
context 'gpg signature did have an associated gpg key which was removed later' do
@ -41,7 +41,13 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
key: GpgHelpers::User1.public_key,
user: user
expect(valid_gpg_signature.reload.gpg_key).to eq gpg_key
expect(valid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: gpg_key,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: true
)
end
it 'does not assign the gpg key when an unrelated gpg key is added' do
@ -50,7 +56,13 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
key: GpgHelpers::User2.public_key,
user: user
expect(valid_gpg_signature.reload.gpg_key).to be_nil
expect(valid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: nil,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: true
)
end
end
@ -68,11 +80,17 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
it 'updates the signature to being valid when the missing gpg key is added' do
# InvalidGpgSignatureUpdater is called by the after_create hook
create :gpg_key,
gpg_key = create :gpg_key,
key: GpgHelpers::User1.public_key,
user: user
expect(invalid_gpg_signature.reload.valid_signature).to be_truthy
expect(invalid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: gpg_key,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: true
)
end
it 'keeps the signature at being invalid when an unrelated gpg key is added' do
@ -81,7 +99,13 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
key: GpgHelpers::User2.public_key,
user: user
expect(invalid_gpg_signature.reload.valid_signature).to be_falsey
expect(invalid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: nil,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: false
)
end
end
@ -102,7 +126,7 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
end
it 'updates the signature to being valid when the user updates the email address' do
create :gpg_key,
gpg_key = create :gpg_key,
key: GpgHelpers::User1.public_key,
user: user
@ -111,20 +135,38 @@ RSpec.describe Gitlab::Gpg::InvalidGpgSignatureUpdater do
# InvalidGpgSignatureUpdater is called by the after_update hook
user.update_attributes!(email: GpgHelpers::User1.emails.first)
expect(invalid_gpg_signature.reload.valid_signature).to be_truthy
expect(invalid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: gpg_key,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: true
)
end
it 'keeps the signature at being invalid when the changed email address is still unrelated' do
create :gpg_key,
gpg_key = create :gpg_key,
key: GpgHelpers::User1.public_key,
user: user
expect(invalid_gpg_signature.reload.valid_signature).to be_falsey
expect(invalid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: gpg_key,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: false
)
# InvalidGpgSignatureUpdater is called by the after_update hook
user.update_attributes!(email: 'still.unrelated@example.com')
expect(invalid_gpg_signature.reload.valid_signature).to be_falsey
expect(invalid_gpg_signature.reload).to have_attributes(
project: project,
commit_sha: commit_sha,
gpg_key: gpg_key,
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
valid_signature: false
)
end
end
end