How to use the pglast.Missing function in pglast

To help you get started, we’ve selected a few pglast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lelit / pglast / tests / test_node.py View on Github external
assert len(root) == 2
    assert repr(root) == '[2*{Foo}]'
    assert str(root) == 'None=[2*{Foo}]'
    with pytest.raises(AttributeError):
        root.not_there

    foo1 = root[0]
    assert foo1 != root
    assert foo1.node_tag == 'Foo'
    assert foo1.parse_tree == {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}
    assert foo1.parent_node is None
    assert foo1.parent_attribute == (None, 0)
    assert repr(foo1) == '{Foo}'
    assert str(foo1) == 'None[0]={Foo}'
    assert foo1.attribute_names == {'bar'}
    assert foo1.not_there is Missing
    assert not foo1.not_there
    assert repr(foo1.not_there) == 'MISSING'
    with pytest.raises(ValueError):
        foo1[1.0]

    bar1 = foo1.bar
    assert bar1 != foo1
    assert bar1.node_tag == 'Bar'
    assert bar1.parent_node is foo1
    assert bar1.parent_attribute == 'bar'
    assert bar1.attribute_names == {'a', 'b', 'c', 'd'}
    assert foo1[bar1.parent_attribute] == bar1
    assert repr(bar1.a) == '<1>'
    assert repr(bar1.b) == "<'b'>"
    assert repr(bar1.c) == ''
    assert bar1.a & 1
github erik / squabble / squabble / rules / add_column_disallow_constraints.py View on Github external
'colname': 'bar',
                    'constraints': [{'Constraint': {'contype': 2}}]
                  }
                }
              }
            }
        """

        # We only care about adding a column
        if node.subtype != AlterTableType.AT_AddColumn:
            return

        constraints = node['def'].constraints

        # No constraints imposed, nothing to do.
        if constraints == pglast.Missing:
            return

        for constraint in constraints:
            if constraint.contype.value in disallowed_constraints:
                col = node['def'].colname.value

                ctx.report(
                    self.ConstraintNotAllowed(col=col),
                    node=constraint)
github erik / squabble / squabble / rules / require_concurrent_index.py View on Github external
def _create_index(self, ctx, node, tables):
        index_name = 'unnamed'
        if node.idxname != pglast.Missing:
            index_name = node.idxname.value

        concurrent = node.concurrent

        # Index was created concurrently, nothing to do here
        if concurrent != pglast.Missing and concurrent.value is True:
            return

        table = node.relation.relname.value.lower()

        # This is a new table, don't alert on it
        if table in tables:
            return

        ctx.report(
            self.IndexNotConcurrent(name=index_name),
github erik / squabble / squabble / rules / require_foreign_key.py View on Github external
...         'colname': 'post_id',
    ...         'constraints': [{'Constraint': {'contype': 8}}]
    ...      }}
    ... }
    >>> _column_needs_foreign_key(fk_regex, pglast.Node(cols['email']))
    False
    >>> _column_needs_foreign_key(fk_regex, pglast.Node(cols['users_id']))
    True
    >>> _column_needs_foreign_key(fk_regex, pglast.Node(cols['post_id']))
    False
    """
    name = column_def.colname.value
    if not fk_regex.match(name):
        return False

    if column_def.constraints == pglast.Missing:
        return True

    return not any(
        e.contype == ConstrType.CONSTR_FOREIGN
        for e in column_def.constraints
    )
github erik / squabble / squabble / rules / require_primary_key.py View on Github external
def _check_column(_ctx, col):
            if col.constraints == pglast.Missing:
                return

            for c in col.constraints:
                _check_constraint(_ctx, c)
github erik / squabble / squabble / reporter.py View on Github external
def _location_for_issue(issue):
    """
    Return the offset into the file for this issue, or None if it
    cannot be determined.
    """
    if issue.node and issue.node.location != pglast.Missing:
        return issue.node.location.value

    return issue.location
github erik / squabble / squabble / rules / disallow_timestamp_precision.py View on Github external
def _check_column_def(self, ctx, node, min_precision):
        col_type = format_type_name(node.typeName)

        if col_type not in self._CHECKED_TYPES:
            return

        modifiers = node.typeName.typmods
        if modifiers == pglast.Missing or \
           len(modifiers) != 1 or \
           modifiers[0].val.node_tag != 'Integer':
            return

        if modifiers[0].val.ival.value <= min_precision:
            ctx.report(
                self.NoTimestampPrecision(),
                node=node.typeName,
                severity=Severity.LOW)
github erik / squabble / squabble / rules / disallow_rename_enum_value.py View on Github external
def _check_enum(self, ctx, node):
        """
        Node is an 'AlterEnumStmt' value

        {
            'AlterEnumStmt': {
                'newVal': 'bar',
                'oldVal': 'foo',   # present if we're renaming
            }
        }
        """

        # Nothing to do if this isn't a rename
        if node.oldVal == pglast.Missing:
            return

        renamed = node.oldVal.value

        ctx.report(self.RenameNotAllowed(value=renamed))