-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1779 from tactilenews/1778_reduce_db_queries
Optimize db queries for requests index page
- Loading branch information
Showing
17 changed files
with
194 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
db/migrate/20240201093310_change_replies_count_default_on_requests.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class ChangeRepliesCountDefaultOnRequests < ActiveRecord::Migration[6.1] | ||
def up | ||
change_column :requests, :replies_count, :integer, default: 0 | ||
end | ||
|
||
def down | ||
change_column :requests, :replies_count, :integer, default: nil | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20240201093729_change_photos_count_default_on_messages.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class ChangePhotosCountDefaultOnMessages < ActiveRecord::Migration[6.1] | ||
def up | ||
change_column :messages, :photos_count, :integer, default: 0 | ||
end | ||
|
||
def down | ||
change_column :messages, :photos_count, :integer, default: nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :messages do | ||
desc 'Add photos count to messages to remove nil default' | ||
task add_photos_count_to_messages: :environment do | ||
puts "Add photos_count to #{Message.count} messages" | ||
ActiveRecord::Base.transaction do | ||
Message.replies.find_each do |message| | ||
message.photos_count = message.photos.count | ||
message.photos_count += message.files.joins(:attachment_blob).where(active_storage_blobs: { | ||
content_type: %w[image/jpg image/jpeg | ||
image/png image/gif] | ||
}).size | ||
message.save | ||
print '.' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :requests do | ||
desc 'Add replies count to requests to remove nil default' | ||
task add_replies_count_to_requests: :environment do | ||
puts "Add replies_count to #{Request.count} requests" | ||
ActiveRecord::Base.transaction do | ||
Request.find_each do |request| | ||
request.replies_count = request.replies.count | ||
request.save | ||
print '.' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Message::File, type: :model do | ||
describe '::counter_culture_fix_counts' do | ||
subject do | ||
described_class.counter_culture_fix_counts | ||
message.reload | ||
end | ||
|
||
context 'given the photos count has become invalid for whatever reason' do | ||
let(:request) { create(:request) } | ||
|
||
describe 'counts images of replies' do | ||
let(:message) { create(:message, :with_file, request: request, attachment: fixture_file_upload('example-image.png')) } | ||
before { message.update(photos_count: 4711) } | ||
it { expect { subject }.to (change { message.photos_count }).from(4711).to(1) } | ||
end | ||
|
||
describe 'does not count other content types' do | ||
let(:message) { create(:message, :with_file, request: request, attachment: fixture_file_upload('example-audio.oga')) } | ||
before { message.update(photos_count: 4711) } | ||
it { expect { subject }.to (change { message.photos_count }).from(4711).to(0) } | ||
end | ||
|
||
describe 'does not count non-replies' do | ||
let(:message) { create(:message, :with_file, :outbound, request: request, attachment: fixture_file_upload('example-image.png')) } | ||
before { message.update(photos_count: 4711) } | ||
it { expect { subject }.to (change { message.photos_count }).from(4711).to(0) } | ||
end | ||
|
||
context 'if there is a photo model' do | ||
let(:message) { create(:message, :with_file, request: request, attachment: fixture_file_upload('example-image.png')) } | ||
subject do | ||
Photo.counter_culture_fix_counts | ||
described_class.counter_culture_fix_counts | ||
message.reload | ||
end | ||
|
||
before do | ||
create(:photo, message: message) | ||
message.update(photos_count: 4711) | ||
end | ||
|
||
it 'both caches get combined' do | ||
pending 'counter_culture does not like two models updating a single count' | ||
expect { subject }.to (change { message.photos_count }).from(4711).to(2) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters