18F/rdbms-subsetter

View on GitHub
rdbms_subsetter/subsetter.py

Summary

Maintainability
F
5 days
Test Coverage

File subsetter.py has 569 lines of code (exceeds 250 allowed). Consider refactoring.
Open

"""
Generate a random sample of rows from a relational database that preserves
referential integrity - so long as constraints are defined, all parent rows
will exist for child rows.

Severity: Major
Found in rdbms_subsetter/subsetter.py - About 1 day to fix

    Function create_row_in has a Cognitive Complexity of 56 (exceeds 5 allowed). Consider refactoring.
    Open

        def create_row_in(self, source_row, target_db, target, prioritized=False):
            logging.debug('create_row_in %s:%s ' %
                          (target.name, target.pk_val(source_row)))
    
            pks = hashable((source_row[key] for key in target.pk))
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - About 1 day 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 __init__ has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring.
    Open

        def __init__(self, sqla_conn, args, schemas=[None]):
            self.args = args
            self.sqla_conn = sqla_conn
            self.schemas = schemas
            self.engine = sa.create_engine(sqla_conn)
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - 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

    Function create_subset_in has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

        def create_subset_in(self, target_db):
    
            for (tbl_name, pks) in self.args.force_rows.items():
                if '.' in tbl_name:
                    (tbl_schema, tbl_name) = tbl_name.split('.', 1)
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - 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

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

    def _random_row_gen_fn(self):
        """
        Random sample of *approximate* size n
        """
        if self.n_rows:
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - 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 assign_target has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
    Open

        def assign_target(self, target_db):
            for ((tbl_schema, tbl_name), tbl) in self.tables.items():
                tbl._random_row_gen_fn = types.MethodType(_random_row_gen_fn, tbl)
                tbl.random_rows = tbl._random_row_gen_fn()
                tbl.next_row = types.MethodType(_next_row, tbl)
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - 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 _find_n_rows has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

    def _find_n_rows(self, estimate=False):
        self.n_rows = 0
        if estimate:
            try:
                if self.db.engine.driver in ('psycopg2', 'pg8000', ):
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - 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 update_sequences has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

    def update_sequences(source, target, schemas, tables, exclude_tables):
        """Set database sequence values to match the source db
    
           Needed to avoid subsequent unique key violations after DB build.
           Currently only implemented for postgresql -> postgresql."""
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - 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 _completeness_score has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

    def _completeness_score(self):
        """Scores how close a target table is to being filled enough to quit"""
        table = (self.schema if self.schema else "") + self.name
        fetch_all = self.fetch_all
        requested = len(self.requested)
    Severity: Minor
    Found in rdbms_subsetter/subsetter.py - About 55 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

                            if source_referred_row:
                                self.create_row_in(source_referred_row, target_db,
                                                   target_referred)
    
    
    Severity: Major
    Found in rdbms_subsetter/subsetter.py - About 45 mins to fix

      Function update_sequences has 5 arguments (exceeds 4 allowed). Consider refactoring.
      Open

      def update_sequences(source, target, schemas, tables, exclude_tables):
      Severity: Minor
      Found in rdbms_subsetter/subsetter.py - About 35 mins to fix

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

        def generate():
            args = argparser.parse_args()
            _import_modules(args.import_list)
            args.force_rows = {}
            for force_row in (args.force or []):
        Severity: Minor
        Found in rdbms_subsetter/subsetter.py - 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

                        for (parent_col, child_col) in zip(fk['referred_columns'],
                                                           fk['constrained_columns']):
                            slct = slct.where(target_parent.c[parent_col] ==
                                              source_row[child_col])
                            if source_row[child_col] is not None:
        Severity: Major
        Found in rdbms_subsetter/subsetter.py and 1 other location - About 4 hrs to fix
        rdbms_subsetter/subsetter.py on lines 364..371

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

        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

                        for (referred_col, constrained_col) in zip(
                                constraint['referred_columns'],
                                constraint['constrained_columns']):
                            slct = slct.where(target_referred.c[referred_col] ==
                                              source_row[constrained_col])
        Severity: Major
        Found in rdbms_subsetter/subsetter.py and 1 other location - About 4 hrs to fix
        rdbms_subsetter/subsetter.py on lines 344..350

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

        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