CartoDB/cartodb20

View on GitHub
app/models/user/db_service.rb

Summary

Maintainability
F
1 wk
Test Coverage

File db_service.rb has 1410 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require_relative 'db_queries'
require_dependency 'carto/db/database'
require_dependency 'carto/db/user_schema_mover'
require 'cartodb/sequel_connection_helper'
require 'carto/configuration'
Severity: Major
Found in app/models/user/db_service.rb - About 3 days to fix

    Class DBService has 111 methods (exceeds 20 allowed). Consider refactoring.
    Open

        class DBService
          include CartoDB::MiniSequel
          include Carto::Configuration
          include ::LoggerHelper
          extend CartoDB::SequelConnectionHelper
    Severity: Major
    Found in app/models/user/db_service.rb - About 2 days to fix

      Method db_configuration_for has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
      Open

            def db_configuration_for(user_role = nil)
              logger = (Rails.env.development? || Rails.env.test? ? ::Rails.logger : nil)
              if user_role == :superuser
                ::SequelRails.configuration.environment_for(Rails.env).merge(
                  'database' => @user.database_name,
      Severity: Minor
      Found in app/models/user/db_service.rb - About 4 hrs 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

      Method drop_all_functions_from_schema has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring.
      Open

            def drop_all_functions_from_schema(schema_name)
              recursivity_max_depth = 3
      
              return if schema_name == SCHEMA_PUBLIC
      
      
      Severity: Minor
      Found in app/models/user/db_service.rb - About 3 hrs 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

      Method create_function_invalidate_varnish_invalidation_service has 76 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

            def create_function_invalidate_varnish_invalidation_service
              add_python
      
              invalidation_host = Cartodb.get_config(:invalidation_service, 'host') || '127.0.0.1'
              invalidation_port = Cartodb.get_config(:invalidation_service, 'port') || 3142
      Severity: Major
      Found in app/models/user/db_service.rb - About 3 hrs to fix

        Method drop_organization_user has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
        Open

              def drop_organization_user(org_id, is_owner: false, force_destroy: false)
                raise CartoDB::BaseCartoDBError.new('Tried to delete an organization user without org id') if org_id.nil?
                Thread.new do
                  @user.in_database(as: :superuser) do |database|
                    if is_owner
        Severity: Minor
        Found in app/models/user/db_service.rb - About 2 hrs 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

        Method create_function_invalidate_varnish_http has 56 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

              def create_function_invalidate_varnish_http
                add_python
        
                varnish_host = Cartodb.get_config(:varnish_management, 'host') || '127.0.0.1'
                varnish_port = Cartodb.get_config(:varnish_management, 'http_port') || 6081
        Severity: Major
        Found in app/models/user/db_service.rb - About 2 hrs to fix

          Method drop_user has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
          Open

                def drop_user(conn = nil, username = nil)
                  conn ||= @user.in_database(as: :cluster_admin)
                  username ||= @user.database_username
                  database_with_conflicts = nil
                  retried = false
          Severity: Minor
          Found in app/models/user/db_service.rb - About 2 hrs 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

          Method drop_all_functions_from_schema has 52 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

                def drop_all_functions_from_schema(schema_name)
                  recursivity_max_depth = 3
          
                  return if schema_name == SCHEMA_PUBLIC
          
          
          Severity: Major
          Found in app/models/user/db_service.rb - About 2 hrs to fix

            Method create_function_invalidate_varnish_telnet has 49 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                  def create_function_invalidate_varnish_telnet
                    add_python
            
                    varnish_host = Cartodb.get_config(:varnish_management, 'host') || '127.0.0.1'
                    varnish_port = Cartodb.get_config(:varnish_management, 'port') || 6082
            Severity: Minor
            Found in app/models/user/db_service.rb - About 1 hr to fix

              Method all_schemas_granted has 47 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                    def all_schemas_granted(role)
                      roles_str = role ? role : all_user_roles.join(',')
                      permissions = 'create,usage'
                      query = %{
                        WITH
              Severity: Minor
              Found in app/models/user/db_service.rb - About 1 hr to fix

                Method upgrade_cartodb_postgres_extension has 42 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                      def upgrade_cartodb_postgres_extension(statement_timeout = nil, cdb_extension_target_version = nil)
                        if cdb_extension_target_version.nil?
                          # WARNING: Do not forget about updating dependencies in cartodb-platform and carto-postgres-artifacts
                          cdb_extension_target_version = '0.37.1'
                        end
                Severity: Minor
                Found in app/models/user/db_service.rb - About 1 hr to fix

                  Method run_pg_query has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
                  Open

                        def run_pg_query(query)
                          time = nil
                          res  = nil
                          translation_proc = nil
                          @user.in_database do |user_database|
                  Severity: Minor
                  Found in app/models/user/db_service.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

                  Method upgrade_cartodb_postgres_extension has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
                  Open

                        def upgrade_cartodb_postgres_extension(statement_timeout = nil, cdb_extension_target_version = nil)
                          if cdb_extension_target_version.nil?
                            # WARNING: Do not forget about updating dependencies in cartodb-platform and carto-postgres-artifacts
                            cdb_extension_target_version = '0.37.1'
                          end
                  Severity: Minor
                  Found in app/models/user/db_service.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

                  Method db_configuration_for has 36 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                        def db_configuration_for(user_role = nil)
                          logger = (Rails.env.development? || Rails.env.test? ? ::Rails.logger : nil)
                          if user_role == :superuser
                            ::SequelRails.configuration.environment_for(Rails.env).merge(
                              'database' => @user.database_name,
                  Severity: Minor
                  Found in app/models/user/db_service.rb - About 1 hr to fix

                    Method run_pg_query has 33 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                          def run_pg_query(query)
                            time = nil
                            res  = nil
                            translation_proc = nil
                            @user.in_database do |user_database|
                    Severity: Minor
                    Found in app/models/user/db_service.rb - About 1 hr to fix

                      Method drop_user has 33 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                            def drop_user(conn = nil, username = nil)
                              conn ||= @user.in_database(as: :cluster_admin)
                              username ||= @user.database_username
                              database_with_conflicts = nil
                              retried = false
                      Severity: Minor
                      Found in app/models/user/db_service.rb - About 1 hr to fix

                        Method drop_organization_user has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                              def drop_organization_user(org_id, is_owner: false, force_destroy: false)
                                raise CartoDB::BaseCartoDBError.new('Tried to delete an organization user without org id') if org_id.nil?
                                Thread.new do
                                  @user.in_database(as: :superuser) do |database|
                                    if is_owner
                        Severity: Minor
                        Found in app/models/user/db_service.rb - About 1 hr to fix

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

                                def revoke_cdb_conf_access
                                  errors = []
                          
                                  roles = [@user.database_username]
                                  if @user.organization_owner?
                          Severity: Minor
                          Found in app/models/user/db_service.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

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

                                def get_drop_functions_sql(database, schema_name, aggregated: false)
                                  set_pgversion
                          
                                  if @pgversion >= 110000
                                    agg_join_clause = "pg_proc.prokind #{aggregated ? ' = ' : ' <> '} 'a'"
                          Severity: Minor
                          Found in app/models/user/db_service.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

                          Method revoke_cdb_conf_access has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                          Open

                                def revoke_cdb_conf_access
                                  errors = []
                          
                                  roles = [@user.database_username]
                                  if @user.organization_owner?
                          Severity: Minor
                          Found in app/models/user/db_service.rb - About 1 hr to fix

                            Method terminate_database_connections has 26 lines of code (exceeds 25 allowed). Consider refactoring.
                            Open

                                  def self.terminate_database_connections(database_name, database_host)
                                    connection_params = ::SequelRails.configuration.environment_for(Rails.env).merge(
                                      'host' => database_host,
                                      'database' => 'postgres'
                                    ) { |_, o, n| n.nil? ? o : n }
                            Severity: Minor
                            Found in app/models/user/db_service.rb - About 1 hr to fix

                              Method rebuild_quota_trigger_with_database has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                    def rebuild_quota_trigger_with_database(db)
                                      if !cartodb_extension_version_pre_mu? && @user.has_organization?
                                        db.run("DROP FUNCTION IF EXISTS public._CDB_UserQuotaInBytes();")
                                      end
                              
                              
                              Severity: Minor
                              Found in app/models/user/db_service.rb - About 45 mins 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

                              Method load_cartodb_functions has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                    def load_cartodb_functions(statement_timeout = nil, cdb_extension_target_version = nil)
                                      add_python
                              
                                      # Install dependencies of cartodb extension
                                      @user.in_database(as: :superuser, no_cartodb_in_schema: true) do |db|
                              Severity: Minor
                              Found in app/models/user/db_service.rb - About 45 mins 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

                              Avoid deeply nested control flow statements.
                              Open

                                        ) { |_, o, n| n.nil? ? o : n }
                              Severity: Major
                              Found in app/models/user/db_service.rb - About 45 mins to fix

                                Method move_to_own_schema has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                                Open

                                      def move_to_own_schema
                                        new_schema_name = @user.username
                                        old_database_schema_name = @user.database_schema
                                        if @user.database_schema != new_schema_name
                                          Carto::Db::UserSchemaMover.new(@user).move_objects(new_schema_name)
                                Severity: Minor
                                Found in app/models/user/db_service.rb - About 45 mins 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

                                Avoid deeply nested control flow statements.
                                Open

                                          ) { |_, o, n| n.nil? ? o : n }
                                Severity: Major
                                Found in app/models/user/db_service.rb - About 45 mins to fix

                                  Identical blocks of code found in 2 locations. Consider refactoring.
                                  Open

                                        def self.build_search_path(user_schema, quote_user_schema = true)
                                          quote_char = quote_user_schema ? "\"" : ""
                                          "#{quote_char}#{user_schema}#{quote_char}, #{SCHEMA_CARTODB}, #{SCHEMA_CDB_DATASERVICES_API}, #{SCHEMA_PUBLIC}"
                                  Severity: Minor
                                  Found in app/models/user/db_service.rb and 1 other location - About 15 mins to fix
                                  app/models/carto/user_db_service.rb on lines 19..22

                                  Duplicated Code

                                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                  Tuning

                                  This issue has a mass of 26.

                                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                  Refactorings

                                  Further Reading

                                  There are no issues that match your filters.

                                  Category
                                  Status