sequelize/sequelize

View on GitHub
src/dialects/postgres/query-generator.js

Summary

Maintainability
F
1 wk
Test Coverage

File query-generator.js has 738 lines of code (exceeds 250 allowed). Consider refactoring.
Open

const Utils = require('../../utils');
const util = require('util');
const DataTypes = require('../../data-types');
const AbstractQueryGenerator = require('../abstract/query-generator');
const semver = require('semver');
Severity: Major
Found in src/dialects/postgres/query-generator.js - About 1 day to fix

    PostgresQueryGenerator has 55 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class PostgresQueryGenerator extends AbstractQueryGenerator {
      setSearchPath(searchPath) {
        return `SET search_path to ${searchPath};`;
      }
    
    
    Severity: Major
    Found in src/dialects/postgres/query-generator.js - About 7 hrs to fix

      Function attributeToSQL has a Cognitive Complexity of 44 (exceeds 5 allowed). Consider refactoring.
      Open

        attributeToSQL(attribute, options) {
          if (!_.isPlainObject(attribute)) {
            attribute = {
              type: attribute
            };
      Severity: Minor
      Found in src/dialects/postgres/query-generator.js - About 6 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

      Function attributeToSQL has 94 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        attributeToSQL(attribute, options) {
          if (!_.isPlainObject(attribute)) {
            attribute = {
              type: attribute
            };
      Severity: Major
      Found in src/dialects/postgres/query-generator.js - About 3 hrs to fix

        Function _checkValidJsonStatement has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
        Open

          _checkValidJsonStatement(stmt) {
            if (typeof stmt !== 'string') {
              return false;
            }
        
        
        Severity: Minor
        Found in src/dialects/postgres/query-generator.js - 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

        Function _checkValidJsonStatement has 46 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          _checkValidJsonStatement(stmt) {
            if (typeof stmt !== 'string') {
              return false;
            }
        
        
        Severity: Minor
        Found in src/dialects/postgres/query-generator.js - About 1 hr to fix

          Function createTableQuery has 44 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            createTableQuery(tableName, attributes, options) {
              options = { ...options };
          
              //Postgres 9.0 does not support CREATE TABLE IF NOT EXISTS, 9.1 and above do
              const databaseVersion = _.get(this, 'sequelize.options.databaseVersion', 0);
          Severity: Minor
          Found in src/dialects/postgres/query-generator.js - About 1 hr to fix

            Function changeColumnQuery has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
            Open

              changeColumnQuery(tableName, attributes) {
                const query = subQuery => `ALTER TABLE ${this.quoteTable(tableName)} ALTER COLUMN ${subQuery};`;
                const sql = [];
                for (const attributeName in attributes) {
                  let definition = this.dataTypeMapping(tableName, attributeName, attributes[attributeName]);
            Severity: Minor
            Found in src/dialects/postgres/query-generator.js - 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

            Function changeColumnQuery has 40 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              changeColumnQuery(tableName, attributes) {
                const query = subQuery => `ALTER TABLE ${this.quoteTable(tableName)} ALTER COLUMN ${subQuery};`;
                const sql = [];
                for (const attributeName in attributes) {
                  let definition = this.dataTypeMapping(tableName, attributeName, attributes[attributeName]);
            Severity: Minor
            Found in src/dialects/postgres/query-generator.js - About 1 hr to fix

              Function handleSequelizeMethod has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
              Open

                handleSequelizeMethod(smth, tableName, factory, options, prepend) {
                  if (smth instanceof Utils.Json) {
                    // Parse nested object
                    if (smth.conditions) {
                      const conditions = this.parseConditionObject(smth.conditions).map(
              Severity: Minor
              Found in src/dialects/postgres/query-generator.js - 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

              Function createTableQuery has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
              Open

                createTableQuery(tableName, attributes, options) {
                  options = { ...options };
              
                  //Postgres 9.0 does not support CREATE TABLE IF NOT EXISTS, 9.1 and above do
                  const databaseVersion = _.get(this, 'sequelize.options.databaseVersion', 0);
              Severity: Minor
              Found in src/dialects/postgres/query-generator.js - 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

              Function dataTypeMapping has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
              Open

                dataTypeMapping(tableName, attr, dataType) {
                  if (dataType.includes('PRIMARY KEY')) {
                    dataType = dataType.replace('PRIMARY KEY', '');
                  }
              
              
              Severity: Minor
              Found in src/dialects/postgres/query-generator.js - About 35 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

              Function deleteQuery has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
              Open

                deleteQuery(tableName, where, options = {}, model) {
                  const table = this.quoteTable(tableName);
                  let whereClause = this.getWhereConditions(where, null, model, options);
                  const limit = options.limit ? ` LIMIT ${this.escape(options.limit)}` : '';
                  let primaryKeys = '';
              Severity: Minor
              Found in src/dialects/postgres/query-generator.js - About 35 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

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

                  if (smth instanceof Utils.Json) {
                    // Parse nested object
                    if (smth.conditions) {
                      const conditions = this.parseConditionObject(smth.conditions).map(
                        condition =>
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 1 day to fix
              src/dialects/mysql/query-generator.js on lines 232..272

              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 252.

              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

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

                  while (currentIndex < stmt.length) {
                    const string = stmt.substr(currentIndex);
                    const functionMatches = jsonFunctionRegex.exec(string);
                    if (functionMatches) {
                      currentIndex += functionMatches[0].indexOf('(');
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 1 day to fix
              src/dialects/mysql/query-generator.js on lines 452..484

              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 229.

              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

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

                  if (options.uniqueKeys) {
                    _.each(options.uniqueKeys, columns => {
                      if (columns.customIndex) {
                        attributesClause += `, UNIQUE (${columns.fields.map(field => this.quoteIdentifier(field)).join(', ')})`;
                      }
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 2 hrs to fix
              src/dialects/sqlite/query-generator.js on lines 61..67

              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 92.

              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

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

                removeIndexQuery(tableName, indexNameOrAttributes) {
                  let indexName = indexNameOrAttributes;
              
                  if (typeof indexName !== 'string') {
                    indexName = Utils.underscore(`${tableName}_${indexNameOrAttributes.join('_')}`);
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 2 hrs to fix
              src/dialects/sqlite/query-generator.js on lines 353..361

              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 86.

              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

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

                    'SELECT ' +
                    'DISTINCT tc.constraint_name as constraint_name, ' +
                    'tc.constraint_schema as constraint_schema, ' +
                    'tc.constraint_catalog as constraint_catalog, ' +
                    'tc.table_name as table_name,' +
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 1 hr to fix
              src/dialects/postgres/query-generator.js on lines 129..145

              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 65.

              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

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

                    'SELECT ' +
                    'pk.constraint_type as "Constraint",' +
                    'c.column_name as "Field", ' +
                    'c.column_default as "Default",' +
                    'c.is_nullable as "Null", ' +
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 1 hr to fix
              src/dialects/postgres/query-generator.js on lines 896..912

              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 65.

              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

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

                    if (attribute.references.key) {
                      referencesKey = this.quoteIdentifiers(attribute.references.key);
                    } else {
                      referencesKey = this.quoteIdentifier('id');
                    }
              Severity: Major
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 1 hr to fix
              src/dialects/sqlite/query-generator.js on lines 313..317

              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 57.

              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

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

                    if (definition.includes('NOT NULL')) {
                      attrSql += query(`${this.quoteIdentifier(attributeName)} SET NOT NULL`);
              
                      definition = definition.replace('NOT NULL', '').trim();
                    } else if (!definition.includes('REFERENCES')) {
              Severity: Minor
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 35 mins to fix
              src/dialects/postgres/query-generator.js on lines 296..304

              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 46.

              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

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

                    if (definition.includes('DEFAULT')) {
                      attrSql += query(
                        `${this.quoteIdentifier(attributeName)} SET DEFAULT ${definition.match(/DEFAULT ([^;]+)/)[1]}`
                      );
              
              
              Severity: Minor
              Found in src/dialects/postgres/query-generator.js and 1 other location - About 35 mins to fix
              src/dialects/postgres/query-generator.js on lines 288..294

              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 46.

              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