Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _check_constraint(child_ctx, constraint):
if constraint.contype == ConstrType.CONSTR_FOREIGN:
child_ctx.report(
self.DisallowedForeignKeyConstraint(table=table_name),
node=constraint
)
>>> _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
)
{
"AddColumnDisallowConstraints": {
"disallowed": ["DEFAULT", "FOREIGN"]
}
}
Valid constraint types:
- DEFAULT
- NULL
- NOT NULL
- FOREIGN
- UNIQUE
"""
_CONSTRAINT_MAP = {
'DEFAULT': ConstrType.CONSTR_DEFAULT,
'NULL': ConstrType.CONSTR_NULL,
'NOT NULL': ConstrType.CONSTR_NOTNULL,
'FOREIGN': ConstrType.CONSTR_FOREIGN,
'UNIQUE': ConstrType.CONSTR_UNIQUE,
}
class ConstraintNotAllowed(Message):
"""
When adding a column to an existing table, certain constraints can have
unintentional side effects, like locking the table or introducing
performance issues.
For example, adding a ``DEFAULT`` constraint may hold a lock on the
table while all existing rows are modified to fill in the default
value.
def constraint(node, output):
if node.conname:
output.swrite('CONSTRAINT ')
output.print_name(node.conname)
ct = enums.ConstrType
if node.contype == ct.CONSTR_NULL:
output.swrite('NULL')
elif node.contype == ct.CONSTR_DEFAULT:
output.swrite('DEFAULT ')
# """
# we may have the expression in either "raw" form [...] or "cooked" form [...]
# should never have both in the same node!
# """
output.print_node(node.raw_expr or node.cooked_expr)
elif node.contype == ct.CONSTR_NOTNULL:
output.swrite('NOT NULL')
elif node.contype == ct.CONSTR_CHECK:
output.swrite('CHECK (')
output.print_node(node.raw_expr or node.cooked_expr)
output.write(')')
if node.is_no_inherit:
"disallowed": ["DEFAULT", "FOREIGN"]
}
}
Valid constraint types:
- DEFAULT
- NULL
- NOT NULL
- FOREIGN
- UNIQUE
"""
_CONSTRAINT_MAP = {
'DEFAULT': ConstrType.CONSTR_DEFAULT,
'NULL': ConstrType.CONSTR_NULL,
'NOT NULL': ConstrType.CONSTR_NOTNULL,
'FOREIGN': ConstrType.CONSTR_FOREIGN,
'UNIQUE': ConstrType.CONSTR_UNIQUE,
}
class ConstraintNotAllowed(Message):
"""
When adding a column to an existing table, certain constraints can have
unintentional side effects, like locking the table or introducing
performance issues.
For example, adding a ``DEFAULT`` constraint may hold a lock on the
table while all existing rows are modified to fill in the default
value.
A ``UNIQUE`` constraint will require scanning the table to confirm
there are no duplicates.
def _check_constraint(_ctx, constraint):
nonlocal seen_pk
if constraint.contype == ConstrType.CONSTR_PRIMARY:
seen_pk = True