Skip to content

Commit

Permalink
Rails 6.2.0.rc2 compatibility
Browse files Browse the repository at this point in the history
Rails 6.2.0.rc2 changed join order when combining Arel and Rails joins.
Worked around by using only Arel joins in the affected query.
See rails/rails#36761

Fixes #822
  • Loading branch information
glebm committed Jul 25, 2019
1 parent c691be6 commit 97ad5b2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -2,7 +2,7 @@

source 'https://rubygems.org'

gem 'rails', '~> 6.0.0.rc1'
gem 'rails', '~> 6.0.0.rc2'
gem 'rails-i18n', '~> 6.0.0.beta1'

# https://github.com/rails/rails/blob/v6.0.0.rc1/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L12
Expand Down
7 changes: 4 additions & 3 deletions app/models/concerns/thredded/topic_common.rb
Expand Up @@ -53,9 +53,10 @@ def unread(user)
topics = arel_table
reads_class = reflect_on_association(:user_read_states).klass
reads = reads_class.arel_table
joins(topics.join(reads, Arel::Nodes::OuterJoin)
.on(topics[:id].eq(reads[:postable_id]).and(reads[:user_id].eq(user.id))).join_sources)
.merge(reads_class.where(reads[:id].eq(nil).or(reads[:unread_posts_count].not_eq(0))))
merge(reads_class.where(reads[:id].eq(nil).or(reads[:unread_posts_count].not_eq(0)))).joins(
topics.join(reads, Arel::Nodes::OuterJoin)
.on(topics[:id].eq(reads[:postable_id]).and(reads[:user_id].eq(user.id))).join_sources
)
end

private
Expand Down
16 changes: 9 additions & 7 deletions app/models/thredded/messageboard.rb
Expand Up @@ -128,13 +128,15 @@ def unread_topics_counts(user:, topics_scope: Thredded::Topic.all)
messageboards = arel_table
read_states = Thredded::UserTopicReadState.arel_table
topics = topics_scope.arel_table
joins(:topics).merge(topics_scope).joins(
messageboards.outer_join(read_states).on(
messageboards[:id].eq(read_states[:messageboard_id])
.and(read_states[:postable_id].eq(topics[:id]))
.and(read_states[:user_id].eq(user.id))
.and(read_states[:unread_posts_count].eq(0))
).join_sources
merge(topics_scope).joins(
messageboards.join(topics)
.on(topics[:messageboard_id].eq(messageboards[:id]))
.outer_join(read_states).on(
messageboards[:id].eq(read_states[:messageboard_id])
.and(read_states[:postable_id].eq(topics[:id]))
.and(read_states[:user_id].eq(user.id))
.and(read_states[:unread_posts_count].eq(0))
).join_sources
).group(messageboards[:id]).pluck(
:id,
Arel::Nodes::Subtraction.new(topics[:id].count, read_states[:id].count)
Expand Down
11 changes: 6 additions & 5 deletions lib/thredded/db_tools.rb
Expand Up @@ -9,13 +9,14 @@ class << self
def migrate(paths:, quiet:, &filter)
verbose_was = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = !quiet
migrate = -> {
if Rails::VERSION::STRING >= '5.2'
ActiveRecord::MigrationContext.new(paths).migrate(nil, &filter)
migrate =
if Rails.gem_version >= Gem::Version.new('6.0.0.rc2')
-> { ActiveRecord::MigrationContext.new(paths, ActiveRecord::SchemaMigration).migrate(nil, &filter) }
elsif Rails::VERSION::STRING >= '5.2'
-> { ActiveRecord::MigrationContext.new(paths).migrate(nil, &filter) }
else
ActiveRecord::Migrator.migrate(paths, &filter)
-> { ActiveRecord::Migrator.migrate(paths, &filter) }
end
}
if quiet
silence_active_record(&migrate)
else
Expand Down
2 changes: 1 addition & 1 deletion spec/gemfiles/rails_6_0.gemfile
Expand Up @@ -4,7 +4,7 @@ source 'https://rubygems.org'
gemspec path: '../../'
eval_gemfile '../../shared.gemfile'

gem 'rails', '~> 6.0.0.rc1'
gem 'rails', '~> 6.0.0.rc2'
gem 'rails-i18n', '~> 6.0.0.beta1'

# https://github.com/rails/rails/blob/v6.0.0.rc1/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L12
Expand Down

0 comments on commit 97ad5b2

Please sign in to comment.