hsgubert/rails-sharding

View on GitHub
lib/rails/sharding/core.rb

Summary

Maintainability
A
1 hr
Test Coverage

Assignment Branch Condition size for configurations is too high. [17.38/15]
Open

    def self.configurations(environment=Rails.env)
      @@db_configs ||= YAML.load(ERB.new(File.read(Config.shards_config_file)).result)
      environment_config = @@db_configs[environment]
      return environment_config if environment_config

Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Cyclomatic complexity for for_each_shard is too high. [7/6]
Open

    def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
      shard_group_filter.to_s if shard_group_filter
      shard_name_filter.to_s if shard_name_filter

      configurations(environment).each do |shard_group, shards_configurations|
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

Method for_each_shard has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
Open

    def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
      shard_group_filter.to_s if shard_group_filter
      shard_name_filter.to_s if shard_name_filter

      configurations(environment).each do |shard_group, shards_configurations|
Severity: Minor
Found in lib/rails/sharding/core.rb - About 1 hr to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Space missing after colon.
Open

    def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Checks for colon (:) not followed by some kind of space. N.B. this cop does not handle spaces after a ternary operator, which are instead handled by Layout/SpaceAroundOperators.

Example:

# bad
def f(a:, b:2); {a:3}; end

# good
def f(a:, b: 2); {a: 3}; end

Line is too long. [95/80]
Open

      if Config.no_connection_retrieved_warning && !connection_used && !was_connected_to_master
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Line is too long. [112/80]
Open

      ConnectionHandler.connection_pool(shard_group, shard_name).release_connection if shard_group && shard_name
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Line is too long. [95/80]
Open

      raise Errors::ConfigNotFoundError, Config.shards_config_file.to_s + ' file was not found'
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Extra empty line detected at class body beginning.
Open


    # Opens a block where all queries will be directed to the selected shard
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cops checks if empty lines around the bodies of classes match the configuration.

Example: EnforcedStyle: empty_lines

# good

class Foo

  def bar
    # ...
  end

end

Example: EnforcedStyle: emptylinesexcept_namespace

# good

class Foo
  class Bar

    # ...

  end
end

Example: EnforcedStyle: emptylinesspecial

# good
class Foo

  def bar; end

end

Example: EnforcedStyle: noemptylines (default)

# good

class Foo
  def bar
    # ...
  end
end

Space missing after colon.
Open

    def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Checks for colon (:) not followed by some kind of space. N.B. this cop does not handle spaces after a ternary operator, which are instead handled by Layout/SpaceAroundOperators.

Example:

# bad
def f(a:, b:2); {a:3}; end

# good
def f(a:, b: 2); {a: 3}; end

Line is too long. [86/80]
Open

      shard_group, shard_name, connection_used = ShardThreadRegistry.pop_current_shard
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Surrounding space missing in default value assignment.
Open

    def self.configurations(environment=Rails.env)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Checks that the equals signs in parameter default assignments have or don't have surrounding space depending on configuration.

Example:

# bad
def some_method(arg1=:default, arg2=nil, arg3=[])
  # do something...
end

# good
def some_method(arg1 = :default, arg2 = nil, arg3 = [])
  # do something...
end

Line is too long. [171/80]
Open

      raise Errors::ConfigNotFoundError, 'Found no shard configurations for environment "' + environment + '" in ' + Config.shards_config_file.to_s + ' file was not found'
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Missing magic comment # frozen_string_literal: true.
Open

require 'rails/sharding/active_record_extensions'
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop is designed to help upgrade to Ruby 3.0. It will add the comment # frozen_string_literal: true to the top of files to enable frozen string literals. Frozen string literals may be default in Ruby 3.0. The comment will be added below a shebang and encoding comment. The frozen string literal comment is only valid in Ruby 2.3+.

Example: EnforcedStyle: when_needed (default)

# The `when_needed` style will add the frozen string literal comment
# to files only when the `TargetRubyVersion` is set to 2.3+.
# bad
module Foo
  # ...
end

# good
# frozen_string_literal: true

module Foo
  # ...
end

Example: EnforcedStyle: always

# The `always` style will always add the frozen string literal comment
# to a file, regardless of the Ruby version or if `freeze` or `<<` are
# called on a string literal.
# bad
module Bar
  # ...
end

# good
# frozen_string_literal: true

module Bar
  # ...
end

Example: EnforcedStyle: never

# The `never` will enforce that the frozen string literal comment does
# not exist in a file.
# bad
# frozen_string_literal: true

module Baz
  # ...
end

# good
module Baz
  # ...
end

Missing top-level class documentation comment.
Open

  class Core
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, or constant definitions.

The documentation requirement is annulled if the class or module has a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the same for all its children.

Example:

# bad
class Person
  # ...
end

# good
# Description/Explanation of Person class
class Person
  # ...
end

Redundant self detected.
Open

      self.configurations.keys
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for redundant uses of self.

The usage of self is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of self with operators because it would be awkward otherwise.

Example:

# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

Redundant self detected.
Open

      self.configurations('test')
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for redundant uses of self.

The usage of self is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of self with operators because it would be awkward otherwise.

Example:

# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
Open

      if block_given?
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Checks for if and unless statements that would fit on one line if written as a modifier if/unless. The maximum line length is configured in the Metrics/LineLength cop.

Example:

# bad
if condition
  do_stuff(bar)
end

unless qux.empty?
  Foo.do_something
end

# good
do_stuff(bar) if condition
Foo.do_something unless qux.empty?

Line is too long. [84/80]
Open

      # Releases connections in case user left some connection in the reserved state
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Use a guard clause instead of wrapping the code inside a conditional expression.
Open

      if Config.extend_active_record_scope
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Use a guard clause instead of wrapping the code inside a conditional expression

Example:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something
  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

Prefer using YAML.safe_load over YAML.load.
Open

      @@db_configs ||= YAML.load(ERB.new(File.read(Config.shards_config_file)).result)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for the use of YAML class methods which have potential security issues leading to remote code execution when loading from an untrusted source.

Example:

# bad
YAML.load("--- foo")

# good
YAML.safe_load("--- foo")
YAML.dump("foo")

Replace class var @@db_configs with a class instance var.
Open

      @@db_configs = nil
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for uses of class variables. Offenses are signaled only on assignment to class variables to reduce the number of offenses that would be reported.

Extra empty line detected at class body end.
Open


  end
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cops checks if empty lines around the bodies of classes match the configuration.

Example: EnforcedStyle: empty_lines

# good

class Foo

  def bar
    # ...
  end

end

Example: EnforcedStyle: emptylinesexcept_namespace

# good

class Foo
  class Bar

    # ...

  end
end

Example: EnforcedStyle: emptylinesspecial

# good
class Foo

  def bar; end

end

Example: EnforcedStyle: noemptylines (default)

# good

class Foo
  def bar
    # ...
  end
end

Line is too long. [86/80]
Open

      @@db_configs ||= YAML.load(ERB.new(File.read(Config.shards_config_file)).result)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Replace class var @@db_configs with a class instance var.
Open

      @@db_configs ||= YAML.load(ERB.new(File.read(Config.shards_config_file)).result)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for uses of class variables. Offenses are signaled only on assignment to class variables to reduce the number of offenses that would be reported.

Space missing after colon.
Open

    def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Checks for colon (:) not followed by some kind of space. N.B. this cop does not handle spaces after a ternary operator, which are instead handled by Layout/SpaceAroundOperators.

Example:

# bad
def f(a:, b:2); {a:3}; end

# good
def f(a:, b: 2); {a: 3}; end

Line is too long. [320/80]
Open

        puts "Warning: no connection to shard '#{shard_group}:#{shard_name}' was retrieved inside the using_shard block. Make sure you don't forget to include Rails::Sharding::ShardableModel to the models you want to be sharded. Disable this warning with Rails::Sharding::Config.no_connection_retrieved_warning = false."
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Line is too long. [97/80]
Open

    def self.for_each_shard(environment:Rails.env, shard_group_filter:nil, shard_name_filter:nil)
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

Use nested module/class definitions instead of compact style.
Open

module Rails::Sharding
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks the style of children definitions at classes and modules. Basically there are two different styles:

Example: EnforcedStyle: nested (default)

# good
# have each child on its own line
class Foo
  class Bar
  end
end

Example: EnforcedStyle: compact

# good
# combine definitions as much as possible
class Foo::Bar
end

The compact style is only forced for classes/modules with one child.

Redundant self detected.
Open

      self.configurations[shard_group.to_s].keys
Severity: Minor
Found in lib/rails/sharding/core.rb by rubocop

This cop checks for redundant uses of self.

The usage of self is only needed when:

  • Sending a message to same object with zero arguments in presence of a method name clash with an argument or a local variable.

  • Calling an attribute writer to prevent an local variable assignment.

Note, with using explicit self you can only send messages with public or protected scope, you cannot send private messages this way.

Note we allow uses of self with operators because it would be awkward otherwise.

Example:

# bad
def foo(bar)
  self.baz
end

# good
def foo(bar)
  self.bar  # Resolves name clash with the argument.
end

def foo
  bar = 1
  self.bar  # Resolves name clash with the local variable.
end

def foo
  %w[x y z].select do |bar|
    self.bar == bar  # Resolves name clash with argument of the block.
  end
end

There are no issues that match your filters.

Category
Status