Skip to content

Commit

Permalink
Merge pull request #224 from cfhowes/issue_196_fk_name
Browse files Browse the repository at this point in the history
Issue #196: Add the 'name' to the 'reference' block for FOREIGN KEY constraints
  • Loading branch information
xnuinside authored Jan 7, 2024
2 parents 26d5ddb + d36cb5c commit abe2a57
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 102 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**v0.31.4**
### Fixes
1. Include source column names in FOREIGN KEY references. Fix for: https://github.com/xnuinside/simple-ddl-parser/issues/196

**v0.31.3**
### Improvements
#### Snowflake update:
Expand Down
11 changes: 8 additions & 3 deletions simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,17 @@ def process_constraints_and_refs(self, data: Dict, p_list: List) -> Dict:

def add_ref_information_to_table(self, data, p_list):
if len(p_list) > 4 and "constraint" in p_list[3]:
# This is a reference, add the name of the column being referenced
ref_data = p_list[-1]["references"]
ref_col_names = p_list[-2]
if isinstance(ref_col_names, list) and len(ref_col_names) == 1:
ref_col_names = ref_col_names[0]
ref_data["name"] = ref_col_names

data = self.set_constraint(
data,
"references",
p_list[-1]["references"],
ref_data,
p_list[3]["constraint"]["name"],
)
elif isinstance(p_list[-2], list):
Expand All @@ -960,8 +967,6 @@ def set_constraint(
target_dict["constraints"] = {}
if not target_dict["constraints"].get(_type):
target_dict["constraints"][_type] = []
if "name" in constraint:
del constraint["name"]
constraint.update({"constraint_name": constraint_name})
target_dict["constraints"][_type].append(constraint)
return target_dict
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_mssql_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ def test_two_unique_constructs():
"columns": ["PersonID"],
"constraint_name": "FK_Person_Age_under",
"deferrable_initially": None,
"name": "id",
"on_delete": None,
"on_update": None,
"schema": None,
Expand Down Expand Up @@ -1459,6 +1460,7 @@ def test_alter_unique():
"columns": ["PersonID"],
"constraint_name": "FK_Person_Age_under",
"deferrable_initially": None,
"name": "id",
"on_delete": None,
"on_update": None,
"schema": None,
Expand Down Expand Up @@ -1703,6 +1705,7 @@ def test_defaults_in_alter():
"columns": ["PersonID"],
"constraint_name": "FK_Person_Age_under",
"deferrable_initially": None,
"name": "id",
"on_delete": None,
"on_update": None,
"schema": None,
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ def test_partition_by():
"columns": ["order_id"],
"constraint_name": "order_items_fk",
"deferrable_initially": None,
"name": "order_id",
"on_delete": None,
"on_update": None,
"schema": None,
Expand Down Expand Up @@ -725,6 +726,7 @@ def test_organization_index():
"columns": [None],
"constraint_name": "fk_metacritcombo_parent",
"deferrable_initially": None,
"name": "parent_criterion_id",
"on_delete": "CASCADE",
"on_update": None,
"schema": None,
Expand All @@ -734,6 +736,7 @@ def test_organization_index():
"columns": [None],
"constraint_name": "fk_metacritcombo_child",
"deferrable_initially": None,
"name": "child_criterion_id",
"on_delete": None,
"on_update": None,
"schema": None,
Expand Down
106 changes: 55 additions & 51 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ def test_double_single_quotes():

def test_autoincrement_order():
# test for https://github.com/xnuinside/simple-ddl-parser/issues/208
ddl = """CREATE TABLE table (surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1 ORDER COMMENT 'Record Identification Number Ordered')"""
ddl = """CREATE TABLE table (
surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1
ORDER COMMENT 'Record Identification Number Ordered')"""
result = DDLParser(ddl).run(group_by_type=True)
expected = {
"ddl_properties": [],
Expand All @@ -667,9 +669,9 @@ def test_autoincrement_order():
"type": "NUMBER",
"unique": False,
"autoincrement": True,
"start" : '1',
"increment": '1',
"increment_order": True
"start": "1",
"increment": "1",
"increment_order": True,
}
],
"index": [],
Expand All @@ -684,10 +686,13 @@ def test_autoincrement_order():
}
print(result)
assert result == expected



def test_autoincrement_noorder():
# test for https://github.com/xnuinside/simple-ddl-parser/issues/208
ddl = """CREATE TABLE table (surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1 NOORDER COMMENT 'Record Identification Number NoOrdered')"""
ddl = """CREATE TABLE table (
surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1
NOORDER COMMENT 'Record Identification Number NoOrdered')"""
result = DDLParser(ddl).run(group_by_type=True)
expected = {
"ddl_properties": [],
Expand All @@ -710,9 +715,9 @@ def test_autoincrement_noorder():
"type": "NUMBER",
"unique": False,
"autoincrement": True,
"start" : '1',
"increment": '1',
"increment_order": False
"start": "1",
"increment": "1",
"increment_order": False,
}
],
"index": [],
Expand All @@ -728,26 +733,7 @@ def test_autoincrement_noorder():
print(result)
assert result == expected

def test_order_sequence():
parse_results = DDLParser(
"""
CREATE SEQUENCE dev.incremental_ids_order
START 1
INCREMENT 1
ORDER;
"""
).run()
expected = [
{
"schema": "dev",
"sequence_name": "incremental_ids",
"increment": 1,
"start": 1,
"order": True,
}
]
assert expected == parse_results


def test_order_sequence():
parse_results = DDLParser(
"""
Expand All @@ -769,6 +755,7 @@ def test_order_sequence():
]
assert expected == parse_results


def test_virtual_column_ext_table():
ddl = """
create or replace external table if not exists TABLE_DATA_SRC.EXT_PAYLOAD_MANIFEST_WEB (
Expand Down Expand Up @@ -802,8 +789,10 @@ def test_virtual_column_ext_table():
"nullable": True,
"default": None,
"check": None,
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',1),'=',2)" }
} ,
"generated": {
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',1),'=',2)"
},
},
{
"name": "year",
"type": "VARCHAR",
Expand All @@ -813,7 +802,9 @@ def test_virtual_column_ext_table():
"nullable": True,
"default": None,
"check": None,
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',2),'=',2)" }
"generated": {
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',2),'=',2)"
},
},
{
"name": "month",
Expand All @@ -824,7 +815,9 @@ def test_virtual_column_ext_table():
"nullable": True,
"default": None,
"check": None,
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',3),'=',2)"}
"generated": {
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',3),'=',2)"
},
},
{
"name": "day",
Expand All @@ -835,7 +828,9 @@ def test_virtual_column_ext_table():
"nullable": True,
"default": None,
"check": None,
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',4),'=',2)"}
"generated": {
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',4),'=',2)"
},
},
{
"name": "path",
Expand All @@ -846,26 +841,30 @@ def test_virtual_column_ext_table():
"nullable": True,
"default": None,
"check": None,
"generated" : {"as" : "METADATA$FILENAME" }
}
"generated": {"as": "METADATA$FILENAME"},
},
],
"index": [],
"partition_by": { "columns" :["type", "year", "month", "day", "path"], "type" : None},
"partitioned_by" : [],
"partition_by": {
"columns": ["type", "year", "month", "day", "path"],
"type": None,
},
"partitioned_by": [],
"primary_key": [],
"primary_key_enforced": None,
"auto_refresh" : False,
"auto_refresh": False,
"schema": "TABLE_DATA_SRC",
"table_name": "EXT_PAYLOAD_MANIFEST_WEB",
"tablespace": None,
"replace" : True,
"replace": True,
"if_not_exists": True,
"location" : "@ADL_Azure_Storage_Account_Container_Name/",
"location": "@ADL_Azure_Storage_Account_Container_Name/",
}
]

assert result_ext_table == expected_ext_table


def test_virtual_column_table():
ddl = """
create or replace table if not exists TABLE_DATA_SRC.EXT_PAYLOAD_MANIFEST_WEB (
Expand Down Expand Up @@ -907,23 +906,28 @@ def test_virtual_column_table():
"nullable": True,
"default": None,
"check": None,
"generated" : {"as" : "id * 10" }
}
"generated": {"as": "id * 10"},
},
],
"index": [],
"partitioned_by" : [],
"partitioned_by": [],
"primary_key": [],
"primary_key_enforced": None,
"auto_refresh" : False,
"auto_refresh": False,
"schema": "TABLE_DATA_SRC",
"table_name": "EXT_PAYLOAD_MANIFEST_WEB",
"tablespace": None,
"replace" : True,
"replace": True,
"if_not_exists": True,
"location" : "@ADL_Azure_Storage_Account_Container_Name/",
"file_format": ['TYPE=JSON', "NULL_IF=('field')",'DATE_FORMAT=AUTO','TRIM_SPACE=TRUE'],
'stage_file_format': ['TYPE=JSON','NULL_IF=()']
}
"location": "@ADL_Azure_Storage_Account_Container_Name/",
"file_format": [
"TYPE=JSON",
"NULL_IF=('field')",
"DATE_FORMAT=AUTO",
"TRIM_SPACE=TRUE",
],
"stage_file_format": ["TYPE=JSON", "NULL_IF=()"],
}
]

assert result_ext_table == expected_ext_table
assert result_ext_table == expected_ext_table
2 changes: 2 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def test_checks_with_in_works():
"columns": [None],
"constraint_name": "fk_metacritcombo_parent",
"deferrable_initially": None,
"name": "parent_criterion_id",
"on_delete": "CASCADE",
"on_update": None,
"schema": None,
Expand All @@ -144,6 +145,7 @@ def test_checks_with_in_works():
"columns": [None],
"constraint_name": "fk_metacritcombo_child",
"deferrable_initially": None,
"name": "child_criterion_id",
"on_delete": None,
"on_update": None,
"schema": None,
Expand Down
Loading

0 comments on commit abe2a57

Please sign in to comment.