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 22162cc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 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
18 changes: 10 additions & 8 deletions app/models/thredded/messageboard.rb
Expand Up @@ -128,14 +128,16 @@ 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
).group(messageboards[:id]).pluck(
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
).merge(topics_scope).group(messageboards[:id]).pluck(
:id,
Arel::Nodes::Subtraction.new(topics[:id].count, read_states[:id].count)
).to_h
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 22162cc

Please sign in to comment.