Skip to content

Commit

Permalink
Fix RequireColumns to work with irregular casing
Browse files Browse the repository at this point in the history
  • Loading branch information
erik committed Jan 13, 2019
1 parent 2015ad7 commit 0d22caa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Changes
Fixes
~~~~~

- Fix ``RequireColumns`` to work with irregular casing.

v1.1.0 (2019-01-11)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup, find_packages

__version__ = '1.1.0'
__version__ = '1.2.0rc0'


readme_path = os.path.join(os.path.dirname(__file__), 'README.rst')
Expand Down
19 changes: 10 additions & 9 deletions squabble/rules/disallow_foreign_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ def enable(self, root_ctx, config):
@squabble.rule.node_visitor
def _check_for_foreign_key(self, ctx, node, allowed_tables):
"""
Coincidentally, both AlterTableStmt and CreateStmt have a similar
enough structure that we can use the same function for both.
Make sure ``node`` doesn't have a FOREIGN KEY reference.
Coincidentally, both ``AlterTableStmt`` and ``CreateStmt``
have a similar enough structure that we can use the same
function for both.
"""
table_name = node.relation.relname.value.lower()

Expand All @@ -70,12 +73,10 @@ def _check_for_foreign_key(self, ctx, node, allowed_tables):
return

def _check_constraint(child_ctx, constraint):
if constraint.contype != ConstrType.CONSTR_FOREIGN:
return

child_ctx.report(
self.DisallowedForeignKeyConstraint(table=table_name),
node=constraint
)
if constraint.contype == ConstrType.CONSTR_FOREIGN:
child_ctx.report(
self.DisallowedForeignKeyConstraint(table=table_name),
node=constraint
)

ctx.register('Constraint', _check_constraint)
11 changes: 5 additions & 6 deletions squabble/rules/require_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _normalize_columns(table_elts):
>>> import pglast.printer
>>> create_table = pglast.parse_sql(
... 'CREATE TABLE _(col1 foo.bar(baz,123), col2 integer);')
... 'CREATE TABLE _(COL1 foo.bar(baz,123), Col2 integer);')
>>> table_elts = pglast.Node(create_table)[0].stmt.tableElts
>>> _normalize_columns(table_elts)
[('col1', 'foo.bar(baz, 123)'), ('col2', 'integer')]
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_required_columns(config):
Extracts the column name and normalizes types out of the config
value for `RequireColumns`.
>>> get_required_columns(['foo,int', 'bar'])
>>> get_required_columns(['foo,int', 'Bar'])
{'foo': 'integer', 'bar': None}
"""
if not config:
Expand All @@ -100,9 +100,7 @@ class RequireColumns(BaseRule):
"""
Require that newly created tables have specified columns.
Configuration:
::
Configuration ::
{
"RequireColumns": {
Expand All @@ -127,6 +125,7 @@ class ColumnWrongType(Message):
def enable(self, ctx, config):
config = config.get('required', [])
cols = get_required_columns(config)

ctx.register('CreateStmt', self._check_create_table(cols))

@squabble.rule.node_visitor
Expand All @@ -138,7 +137,7 @@ def _check_create_table(self, ctx, node, required):
columns[col] = {'type': typ, 'node': None}

def _attach_column_node(_ctx, col):
name = col.colname.value
name = col.colname.value.lower()
columns[name]['node'] = col

ctx.register('ColumnDef', _attach_column_node)
Expand Down

0 comments on commit 0d22caa

Please sign in to comment.