Skip to content

Commit abe2a57

Browse files
authored
Merge pull request #224 from cfhowes/issue_196_fk_name
Issue #196: Add the 'name' to the 'reference' block for FOREIGN KEY constraints
2 parents 26d5ddb + d36cb5c commit abe2a57

File tree

7 files changed

+165
-102
lines changed

7 files changed

+165
-102
lines changed

CHANGELOG.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
**v0.31.4**
2+
### Fixes
3+
1. Include source column names in FOREIGN KEY references. Fix for: https://github.com/xnuinside/simple-ddl-parser/issues/196
4+
15
**v0.31.3**
26
### Improvements
37
#### Snowflake update:

simple_ddl_parser/dialects/sql.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,17 @@ def process_constraints_and_refs(self, data: Dict, p_list: List) -> Dict:
934934

935935
def add_ref_information_to_table(self, data, p_list):
936936
if len(p_list) > 4 and "constraint" in p_list[3]:
937+
# This is a reference, add the name of the column being referenced
938+
ref_data = p_list[-1]["references"]
939+
ref_col_names = p_list[-2]
940+
if isinstance(ref_col_names, list) and len(ref_col_names) == 1:
941+
ref_col_names = ref_col_names[0]
942+
ref_data["name"] = ref_col_names
943+
937944
data = self.set_constraint(
938945
data,
939946
"references",
940-
p_list[-1]["references"],
947+
ref_data,
941948
p_list[3]["constraint"]["name"],
942949
)
943950
elif isinstance(p_list[-2], list):
@@ -960,8 +967,6 @@ def set_constraint(
960967
target_dict["constraints"] = {}
961968
if not target_dict["constraints"].get(_type):
962969
target_dict["constraints"][_type] = []
963-
if "name" in constraint:
964-
del constraint["name"]
965970
constraint.update({"constraint_name": constraint_name})
966971
target_dict["constraints"][_type].append(constraint)
967972
return target_dict

tests/dialects/test_mssql_specific.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ def test_two_unique_constructs():
695695
"columns": ["PersonID"],
696696
"constraint_name": "FK_Person_Age_under",
697697
"deferrable_initially": None,
698+
"name": "id",
698699
"on_delete": None,
699700
"on_update": None,
700701
"schema": None,
@@ -1459,6 +1460,7 @@ def test_alter_unique():
14591460
"columns": ["PersonID"],
14601461
"constraint_name": "FK_Person_Age_under",
14611462
"deferrable_initially": None,
1463+
"name": "id",
14621464
"on_delete": None,
14631465
"on_update": None,
14641466
"schema": None,
@@ -1703,6 +1705,7 @@ def test_defaults_in_alter():
17031705
"columns": ["PersonID"],
17041706
"constraint_name": "FK_Person_Age_under",
17051707
"deferrable_initially": None,
1708+
"name": "id",
17061709
"on_delete": None,
17071710
"on_update": None,
17081711
"schema": None,

tests/dialects/test_oracle.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ def test_partition_by():
487487
"columns": ["order_id"],
488488
"constraint_name": "order_items_fk",
489489
"deferrable_initially": None,
490+
"name": "order_id",
490491
"on_delete": None,
491492
"on_update": None,
492493
"schema": None,
@@ -725,6 +726,7 @@ def test_organization_index():
725726
"columns": [None],
726727
"constraint_name": "fk_metacritcombo_parent",
727728
"deferrable_initially": None,
729+
"name": "parent_criterion_id",
728730
"on_delete": "CASCADE",
729731
"on_update": None,
730732
"schema": None,
@@ -734,6 +736,7 @@ def test_organization_index():
734736
"columns": [None],
735737
"constraint_name": "fk_metacritcombo_child",
736738
"deferrable_initially": None,
739+
"name": "child_criterion_id",
737740
"on_delete": None,
738741
"on_update": None,
739742
"schema": None,

tests/dialects/test_snowflake.py

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ def test_double_single_quotes():
644644

645645
def test_autoincrement_order():
646646
# test for https://github.com/xnuinside/simple-ddl-parser/issues/208
647-
ddl = """CREATE TABLE table (surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1 ORDER COMMENT 'Record Identification Number Ordered')"""
647+
ddl = """CREATE TABLE table (
648+
surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1
649+
ORDER COMMENT 'Record Identification Number Ordered')"""
648650
result = DDLParser(ddl).run(group_by_type=True)
649651
expected = {
650652
"ddl_properties": [],
@@ -667,9 +669,9 @@ def test_autoincrement_order():
667669
"type": "NUMBER",
668670
"unique": False,
669671
"autoincrement": True,
670-
"start" : '1',
671-
"increment": '1',
672-
"increment_order": True
672+
"start": "1",
673+
"increment": "1",
674+
"increment_order": True,
673675
}
674676
],
675677
"index": [],
@@ -684,10 +686,13 @@ def test_autoincrement_order():
684686
}
685687
print(result)
686688
assert result == expected
687-
689+
690+
688691
def test_autoincrement_noorder():
689692
# test for https://github.com/xnuinside/simple-ddl-parser/issues/208
690-
ddl = """CREATE TABLE table (surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1 NOORDER COMMENT 'Record Identification Number NoOrdered')"""
693+
ddl = """CREATE TABLE table (
694+
surrogatekey_SK NUMBER(38,0) NOT NULL autoincrement start 1 increment 1
695+
NOORDER COMMENT 'Record Identification Number NoOrdered')"""
691696
result = DDLParser(ddl).run(group_by_type=True)
692697
expected = {
693698
"ddl_properties": [],
@@ -710,9 +715,9 @@ def test_autoincrement_noorder():
710715
"type": "NUMBER",
711716
"unique": False,
712717
"autoincrement": True,
713-
"start" : '1',
714-
"increment": '1',
715-
"increment_order": False
718+
"start": "1",
719+
"increment": "1",
720+
"increment_order": False,
716721
}
717722
],
718723
"index": [],
@@ -728,26 +733,7 @@ def test_autoincrement_noorder():
728733
print(result)
729734
assert result == expected
730735

731-
def test_order_sequence():
732-
parse_results = DDLParser(
733-
"""
734-
CREATE SEQUENCE dev.incremental_ids_order
735-
START 1
736-
INCREMENT 1
737-
ORDER;
738-
"""
739-
).run()
740-
expected = [
741-
{
742-
"schema": "dev",
743-
"sequence_name": "incremental_ids",
744-
"increment": 1,
745-
"start": 1,
746-
"order": True,
747-
}
748-
]
749-
assert expected == parse_results
750-
736+
751737
def test_order_sequence():
752738
parse_results = DDLParser(
753739
"""
@@ -769,6 +755,7 @@ def test_order_sequence():
769755
]
770756
assert expected == parse_results
771757

758+
772759
def test_virtual_column_ext_table():
773760
ddl = """
774761
create or replace external table if not exists TABLE_DATA_SRC.EXT_PAYLOAD_MANIFEST_WEB (
@@ -802,8 +789,10 @@ def test_virtual_column_ext_table():
802789
"nullable": True,
803790
"default": None,
804791
"check": None,
805-
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',1),'=',2)" }
806-
} ,
792+
"generated": {
793+
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',1),'=',2)"
794+
},
795+
},
807796
{
808797
"name": "year",
809798
"type": "VARCHAR",
@@ -813,7 +802,9 @@ def test_virtual_column_ext_table():
813802
"nullable": True,
814803
"default": None,
815804
"check": None,
816-
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',2),'=',2)" }
805+
"generated": {
806+
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',2),'=',2)"
807+
},
817808
},
818809
{
819810
"name": "month",
@@ -824,7 +815,9 @@ def test_virtual_column_ext_table():
824815
"nullable": True,
825816
"default": None,
826817
"check": None,
827-
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',3),'=',2)"}
818+
"generated": {
819+
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',3),'=',2)"
820+
},
828821
},
829822
{
830823
"name": "day",
@@ -835,7 +828,9 @@ def test_virtual_column_ext_table():
835828
"nullable": True,
836829
"default": None,
837830
"check": None,
838-
"generated" : {"as" : "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',4),'=',2)"}
831+
"generated": {
832+
"as": "SPLIT_PART(SPLIT_PART(METADATA$FILENAME,'/',4),'=',2)"
833+
},
839834
},
840835
{
841836
"name": "path",
@@ -846,26 +841,30 @@ def test_virtual_column_ext_table():
846841
"nullable": True,
847842
"default": None,
848843
"check": None,
849-
"generated" : {"as" : "METADATA$FILENAME" }
850-
}
844+
"generated": {"as": "METADATA$FILENAME"},
845+
},
851846
],
852847
"index": [],
853-
"partition_by": { "columns" :["type", "year", "month", "day", "path"], "type" : None},
854-
"partitioned_by" : [],
848+
"partition_by": {
849+
"columns": ["type", "year", "month", "day", "path"],
850+
"type": None,
851+
},
852+
"partitioned_by": [],
855853
"primary_key": [],
856854
"primary_key_enforced": None,
857-
"auto_refresh" : False,
855+
"auto_refresh": False,
858856
"schema": "TABLE_DATA_SRC",
859857
"table_name": "EXT_PAYLOAD_MANIFEST_WEB",
860858
"tablespace": None,
861-
"replace" : True,
859+
"replace": True,
862860
"if_not_exists": True,
863-
"location" : "@ADL_Azure_Storage_Account_Container_Name/",
861+
"location": "@ADL_Azure_Storage_Account_Container_Name/",
864862
}
865863
]
866864

867865
assert result_ext_table == expected_ext_table
868866

867+
869868
def test_virtual_column_table():
870869
ddl = """
871870
create or replace table if not exists TABLE_DATA_SRC.EXT_PAYLOAD_MANIFEST_WEB (
@@ -907,23 +906,28 @@ def test_virtual_column_table():
907906
"nullable": True,
908907
"default": None,
909908
"check": None,
910-
"generated" : {"as" : "id * 10" }
911-
}
909+
"generated": {"as": "id * 10"},
910+
},
912911
],
913912
"index": [],
914-
"partitioned_by" : [],
913+
"partitioned_by": [],
915914
"primary_key": [],
916915
"primary_key_enforced": None,
917-
"auto_refresh" : False,
916+
"auto_refresh": False,
918917
"schema": "TABLE_DATA_SRC",
919918
"table_name": "EXT_PAYLOAD_MANIFEST_WEB",
920919
"tablespace": None,
921-
"replace" : True,
920+
"replace": True,
922921
"if_not_exists": True,
923-
"location" : "@ADL_Azure_Storage_Account_Container_Name/",
924-
"file_format": ['TYPE=JSON', "NULL_IF=('field')",'DATE_FORMAT=AUTO','TRIM_SPACE=TRUE'],
925-
'stage_file_format': ['TYPE=JSON','NULL_IF=()']
926-
}
922+
"location": "@ADL_Azure_Storage_Account_Container_Name/",
923+
"file_format": [
924+
"TYPE=JSON",
925+
"NULL_IF=('field')",
926+
"DATE_FORMAT=AUTO",
927+
"TRIM_SPACE=TRUE",
928+
],
929+
"stage_file_format": ["TYPE=JSON", "NULL_IF=()"],
930+
}
927931
]
928932

929-
assert result_ext_table == expected_ext_table
933+
assert result_ext_table == expected_ext_table

tests/test_checks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def test_checks_with_in_works():
135135
"columns": [None],
136136
"constraint_name": "fk_metacritcombo_parent",
137137
"deferrable_initially": None,
138+
"name": "parent_criterion_id",
138139
"on_delete": "CASCADE",
139140
"on_update": None,
140141
"schema": None,
@@ -144,6 +145,7 @@ def test_checks_with_in_works():
144145
"columns": [None],
145146
"constraint_name": "fk_metacritcombo_child",
146147
"deferrable_initially": None,
148+
"name": "child_criterion_id",
147149
"on_delete": None,
148150
"on_update": None,
149151
"schema": None,

0 commit comments

Comments
 (0)