Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails > 6 and Ruby > 3 support #577

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
.bundle
.rvmrc
.ruby-version
Gemfile.lock
gemfiles/*.lock
pkg/*
*.rbc
tmp/*
.*.sw[a-z]
database.log
.byebug_history
.env
12 changes: 3 additions & 9 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
inherit_from: .rubocop_todo.yml

ActionFilter:
EnforcedStyle: filter

CaseIndentation:
IndentWhenRelativeTo: end
EnforcedStyle: end

CollectionMethods:
PreferredMethods:
Expand All @@ -17,14 +14,11 @@ Encoding:
Enabled: false

EndAlignment:
AlignWith: variable
EnforcedStyleAlignWith: variable

HashSyntax:
EnforcedStyle: hash_rockets

Style/IndentHash:
EnforcedStyle: consistent

Loop:
Enabled: false

Expand All @@ -37,7 +31,7 @@ RegexpLiteral:
Semicolon:
AllowAsExpressionSeparator: true

Style/TrailingComma:
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Style/TrivialAccessors:
Expand Down
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Lint/AmbiguousRegexpLiteral:
Enabled: false

# Offense count: 1
Lint/HandleExceptions:
Lint/SuppressedException:
Enabled: false

# Offense count: 1
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.0.6
102 changes: 102 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
PATH
remote: .
specs:
ar-octopus-ruby-3 (0.11.2)
activerecord (~> 6.0.6.1)
activesupport (~> 6.0.6.1)

GEM
remote: https://rubygems.org/
specs:
activemodel (6.0.6.1)
activesupport (= 6.0.6.1)
activerecord (6.0.6.1)
activemodel (= 6.0.6.1)
activesupport (= 6.0.6.1)
activesupport (6.0.6.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
appraisal (2.4.1)
bundler
rake
thor (>= 0.14.0)
ast (2.4.2)
byebug (11.1.3)
coderay (1.1.3)
concurrent-ruby (1.2.2)
diff-lcs (1.5.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
method_source (1.0.0)
minitest (5.18.0)
mysql2 (0.5.5)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
racc
pg (0.21.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
racc (1.7.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.1)
rexml (3.2.5)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.52.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.2.3)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
sqlite3 (1.6.3-x86_64-linux)
thor (1.2.2)
thread_safe (0.3.6)
tzinfo (1.2.11)
thread_safe (~> 0.1)
unicode-display_width (2.4.2)
zeitwerk (2.6.8)

PLATFORMS
x86_64-linux

DEPENDENCIES
appraisal (>= 0.3.8)
ar-octopus-ruby-3!
mysql2 (~> 0.5)
pg (~> 0.18)
pry-byebug
rake
rspec (>= 3)
rubocop
sqlite3 (~> 1.4)

BUNDLED WITH
2.4.13
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace :db do
class BlankModel < ActiveRecord::Base; end

BlankModel.using(shard_symbol).connection.initialize_schema_migrations_table
BlankModel.using(shard_symbol).connection.initialize_metadata_table if Octopus.atleast_rails50?
BlankModel.using(shard_symbol).connection.initialize_metadata_table if Octopus.atleast_rails50?

BlankModel.using(shard_symbol).connection.create_table(:users) do |u|
u.string :name
Expand Down
16 changes: 10 additions & 6 deletions ar-octopus.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'octopus/version'

Gem::Specification.new do |s|
s.name = 'ar-octopus'
s.name = 'ar-octopus-ruby-3'
s.version = Octopus::VERSION
s.authors = ['Thiago Pradi', 'Mike Perham', 'Gabriel Sobrinho']
s.email = ['tchandy@gmail.com', 'mperham@gmail.com', 'gabriel.sobrinho@gmail.com']
Expand All @@ -17,22 +17,26 @@ Gem::Specification.new do |s|
s.require_paths = ['lib']

s.post_install_message = "Important: If you are upgrading from < Octopus 0.5.0 you need to run:\n" \
"$ rake octopus:copy_schema_versions\n\n" \
"$ rake octopus:copy_scha_versions\n\n" \
'Octopus now stores schema version information in each shard and migrations will not ' \
'work properly unless this task is invoked.'

s.required_ruby_version = '>= 2.2.0'

s.add_dependency 'activerecord', '>= 4.2.0'
s.add_dependency 'activesupport', '>= 4.2.0'
s.add_dependency 'activerecord', "~> 6.0.6.1"
s.add_dependency 'activesupport', "~> 6.0.6.1"

s.add_development_dependency 'appraisal', '>= 0.3.8'
s.add_development_dependency 'mysql2', '>= 0.3.18', "< 0.5"
# To install the mysql2 gem its necessary execute the following command before run bundle
# $ sudo apt-get install libmysqlclient-dev
s.add_development_dependency 'mysql2', '~> 0.5'
s.add_development_dependency 'pg', '~> 0.18'
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec', '>= 3'
s.add_development_dependency 'rubocop'
s.add_development_dependency 'sqlite3', '~> 1.3.6'
# To install the sqlite3 gem its necessary execute the following command before run bundle
# $ sudo apt-get install -y sqlite3 libsqlite3-dev
s.add_development_dependency 'sqlite3', '~> 1.4'
s.add_development_dependency 'pry-byebug'

s.license = 'MIT'
Expand Down
20 changes: 12 additions & 8 deletions lib/octopus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def self.config
@config ||= begin
file_name = File.join(Octopus.directory, 'config/shards.yml').to_s

if File.exist?(file_name) || File.symlink?(file_name)
config ||= HashWithIndifferentAccess.new(YAML.load(ERB.new(File.read(file_name)).result))[Octopus.env]
config = if File.exist?(file_name) || File.symlink?(file_name)
HashWithIndifferentAccess.new(YAML.safe_load(ERB.new(File.read(file_name)).result))[Octopus.env]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to outsource it in another PR as this one isn't related to rails and would probably help to have it upstreamed faster.

Also, we should allow alisases here (or have a config for that)

else
config ||= HashWithIndifferentAccess.new
HashWithIndifferentAccess.new
end

config
Expand Down Expand Up @@ -99,9 +99,9 @@ def self.rails42?
end

def self.rails50?
ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR.zero?
end

def self.atleast_rails50?
ActiveRecord::VERSION::MAJOR >= 5
end
Expand All @@ -114,6 +114,10 @@ def self.rails52?
ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 2
end

def self.rails60?
ActiveRecord::VERSION::MAJOR > 6 || ActiveRecord::VERSION::MAJOR == 6
end

def self.atleast_rails51?
ActiveRecord::VERSION::MAJOR > 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1)
end
Expand All @@ -125,10 +129,10 @@ def self.atleast_rails52?
attr_writer :logger

def self.logger
if defined?(Rails.logger)
@logger ||= Rails.logger
@logger = if defined?(Rails.logger)
Rails.logger
else
@logger ||= Logger.new($stderr)
Logger.new($stderr)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/octopus/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def initialize(*args)
end
end

ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:prepend, Octopus::AbstractAdapter::OctopusShard)
ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(Octopus::AbstractAdapter::OctopusShard)
2 changes: 1 addition & 1 deletion lib/octopus/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def current_shard
end
end

ActiveRecord::Associations::Association.send(:include, Octopus::Association)
ActiveRecord::Associations::Association.include(Octopus::Association)
12 changes: 6 additions & 6 deletions lib/octopus/association_shard_tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def initialize(record, current_shard)

def message
[
"Association Error: Records are from different shards",
'Association Error: Records are from different shards',
"Record: #{record.inspect}",
"Current Shard: #{current_shard.inspect}",
"Current Record Shard: #{record.current_shard.inspect}",
].join(" ")
"Current Record Shard: #{record.current_shard.inspect}"
].join(' ')
end
end

Expand All @@ -25,7 +25,7 @@ def self.extended(base)
module InstanceMethods
def connection_on_association=(record)
return unless ::Octopus.enabled?
return if !self.class.connection.respond_to?(:current_shard) || !self.respond_to?(:current_shard)
return if !self.class.connection.respond_to?(:current_shard) || !respond_to?(:current_shard)

if !record.current_shard.nil? && !current_shard.nil? && record.current_shard.to_s != current_shard.to_s
raise MismatchedShards.new(record, current_shard)
Expand All @@ -50,8 +50,8 @@ def has_and_belongs_to_many(association_id, scope = nil, options = {}, &extensio
end

def default_octopus_opts(options)
options[:before_add] = [ :connection_on_association=, options[:before_add] ].compact.flatten
options[:before_remove] = [ :connection_on_association=, options[:before_remove] ].compact.flatten
options[:before_add] = [:connection_on_association=, options[:before_add]].compact.flatten
options[:before_remove] = [:connection_on_association=, options[:before_remove]].compact.flatten
end

def assign_octopus_opts(scope, options)
Expand Down
4 changes: 2 additions & 2 deletions lib/octopus/collection_association.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Octopus
module CollectionAssociation
def self.included(base)
if Octopus.rails51? || Octopus.rails52?
if Octopus.atleast_rails51?
base.sharded_methods :reader, :writer, :ids_reader, :ids_writer, :create, :create!,
:build, :include?,
:load_target, :reload, :size, :select
Expand All @@ -14,4 +14,4 @@ def self.included(base)
end
end

ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::CollectionAssociation)
ActiveRecord::Associations::CollectionAssociation.include(Octopus::CollectionAssociation)
2 changes: 1 addition & 1 deletion lib/octopus/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def current_shard
end
end

ActiveRecord::Associations::CollectionProxy.send(:include, Octopus::CollectionProxy)
ActiveRecord::Associations::CollectionProxy.include(Octopus::CollectionProxy)
1 change: 1 addition & 0 deletions lib/octopus/load_balancing/round_robin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module LoadBalancing
class RoundRobin
def initialize(slaves_list)
raise Octopus::Exception.new("No slaves available") if slaves_list.empty?

@slaves_list = slaves_list
@slave_index = 0
end
Expand Down
2 changes: 1 addition & 1 deletion lib/octopus/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def debug_with_octopus_shard(msg)
end
end

ActiveRecord::LogSubscriber.send(:include, Octopus::LogSubscriber)
ActiveRecord::LogSubscriber.include(Octopus::LogSubscriber)
Loading