Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix references not null issue #244

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
**v1.0.4**
### Improvements
1. Support functions with schema prefix in `DEFAULT` and `CHECK` statements.
1. Support functions with schema prefix in `DEFAULT` and `CHECK` statements. https://github.com/xnuinside/simple-ddl-parser/issues/240
### Fixes
1. Fix for REFERENCES NOT NULL - https://github.com/xnuinside/simple-ddl-parser/issues/239
2. Fix for snowflake stage name location format bug fix - https://github.com/xnuinside/simple-ddl-parser/pull/241

**v1.0.3**
### Improvements
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,13 @@ for help with debugging & testing support for BigQuery dialect DDLs:


## Changelog
**v1.0.4**
### Improvements
1. Support functions with schema prefix in `DEFAULT` and `CHECK` statements. https://github.com/xnuinside/simple-ddl-parser/issues/240
### Fixes
1. Fix for REFERENCES NOT NULL - https://github.com/xnuinside/simple-ddl-parser/issues/239
2. Fix for snowflake stage name location format bug fix - https://github.com/xnuinside/simple-ddl-parser/pull/241

**v1.0.3**
### Improvements
1. Fixed bug with `CREATE OR REPLACE SCHEMA`.
Expand Down
11 changes: 11 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,17 @@ for help with debugging & testing support for BigQuery dialect DDLs:
Changelog
---------

**v1.0.4**

Improvements
^^^^^^^^^^^^


#. Support functions with schema prefix in ``DEFAULT`` and ``CHECK`` statements. https://github.com/xnuinside/simple-ddl-parser/issues/240
### Fixes
#. Fix for REFERENCES NOT NULL - https://github.com/xnuinside/simple-ddl-parser/issues/239
#. Fix for snowflake stage name location format bug fix - https://github.com/xnuinside/simple-ddl-parser/pull/241

**v1.0.3**

Improvements
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simple-ddl-parser"
version = "1.0.3"
version = "1.0.4"
description = "Simple DDL Parser to parse SQL & dialects like HQL, TSQL (MSSQL), Oracle, AWS Redshift, Snowflake, MySQL, PostgreSQL, etc ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc.; sequences, alters, custom types & other entities from ddl."
authors = ["Iuliia Volkova <[email protected]>"]
license = "MIT"
Expand Down
17 changes: 10 additions & 7 deletions simple_ddl_parser/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Snowflake:

def p_clone(self, p: List) -> None:
"""clone : CLONE id"""
p_list = list(p)
Expand Down Expand Up @@ -34,11 +33,16 @@ def p_multi_id_or_string(self, p: List) -> None:
p[0] = value

def p_fmt_equals(self, p: List) -> None:
"""fmt_equals : id LP multi_id_or_string RP
"""
fmt_split = re.compile(r"\w+\s*=\s*\w+|\w+\s*=\s*'.'|\w+\s*=\s*'..'|\w+\s*=\s*\('.+'\)|\w+\s*=\(\)")
"""fmt_equals : id LP multi_id_or_string RP"""
fmt_split = re.compile(
r"\w+\s*=\s*\w+|\w+\s*=\s*'.'|\w+\s*=\s*'..'|\w+\s*=\s*\('.+'\)|\w+\s*=\(\)"
)
p_list = list(p)
p[0] = {f.split('=')[0].strip(): f.split('=')[1].strip() for f in fmt_split.findall(p_list[3]) if '=' in f}
p[0] = {
f.split("=")[0].strip(): f.split("=")[1].strip()
for f in fmt_split.findall(p_list[3])
if "=" in f
}

def p_table_property_equals(self, p: List) -> None:
"""table_property_equals : id id id_or_string
Expand Down Expand Up @@ -92,8 +96,7 @@ def p_expression_change_tracking(self, p: List) -> None:
p[0]["change_tracking"] = p_list[-1]

def p_comment_equals(self, p: List) -> None:
"""expr : expr option_comment
"""
"""expr : expr option_comment"""
p[0] = p[1]
if p[2]:
p[0].update(p[2])
Expand Down
3 changes: 2 additions & 1 deletion simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ def p_autoincrement(self, p: List) -> None:
def p_defcolumn(self, p: List) -> None:
"""defcolumn : column
| defcolumn comment
| defcolumn null
| defcolumn encode
| defcolumn PRIMARY KEY
| defcolumn UNIQUE KEY
Expand All @@ -405,6 +404,8 @@ def p_defcolumn(self, p: List) -> None:
| defcolumn collate
| defcolumn enforced
| defcolumn ref
| defcolumn null
| defcolumn ref null
| defcolumn foreign ref
| defcolumn encrypt
| defcolumn generated
Expand Down
637 changes: 319 additions & 318 deletions simple_ddl_parser/parsetab.py

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions tests/test_simple_ddl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3335,3 +3335,72 @@ def test_create_empty_table_with_parentheses():
"types": [],
}
assert result == expected


def test_reference_not_null():

ddl = """CREATE TABLE a
(
id UUID PRIMARY KEY
);

CREATE TABLE b
(
id UUID PRIMARY KEY,
a_id UUID REFERENCES a(id) NOT NULL
);

"""
result = DDLParser(ddl).run(group_by_type=True)
expected = {'ddl_properties': [],
'domains': [],
'schemas': [],
'sequences': [],
'tables': [{'alter': {},
'checks': [],
'columns': [{'check': None,
'default': None,
'name': 'id',
'nullable': False,
'references': None,
'size': None,
'type': 'UUID',
'unique': False}],
'index': [],
'partitioned_by': [],
'primary_key': ['id'],
'schema': None,
'table_name': 'a',
'tablespace': None},
{'alter': {},
'checks': [],
'columns': [{'check': None,
'default': None,
'name': 'id',
'nullable': False,
'references': None,
'size': None,
'type': 'UUID',
'unique': False},
{'check': None,
'default': None,
'name': 'a_id',
'nullable': False,
'references': {'columns': ['id'],
'deferrable_initially': None,
'on_delete': None,
'on_update': None,
'schema': None,
'table': 'a'},
'size': None,
'type': 'UUID',
'unique': False}],
'index': [],
'partitioned_by': [],
'primary_key': ['id'],
'schema': None,
'table_name': 'b',
'tablespace': None}],
'types': []}

assert expected == result
Loading