Skip to content

Commit

Permalink
fix issues with unique indexes with multiple columns (wrong attribute…
Browse files Browse the repository at this point in the history
… setting)
  • Loading branch information
xnuinside committed May 18, 2024
1 parent dd3e4c8 commit 6859e25
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
**v1.5.0**

### Fixes

1. Now, `unique` set up to column only if it was only one column in unique constraint/index. Issue - https://github.com/xnuinside/simple-ddl-parser/issues/255
2. Fixed issue when UNIQUE KEY was identified as primary key - https://github.com/xnuinside/simple-ddl-parser/issues/253


**v1.4.0**

### Fixes
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@ for help with debugging & testing support for BigQuery dialect DDLs:


## Changelog
**v1.5.0**

### Fixes

1. Now, `unique` set up to column only if it was only one column in unique constraint/index. Issue - https://github.com/xnuinside/simple-ddl-parser/issues/255
2. Fixed issue when UNIQUE KEY was identified as primary key - https://github.com/xnuinside/simple-ddl-parser/issues/253


**v1.4.0**

### Fixes
Expand Down
15 changes: 15 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Yes, library already has about 9000+ downloads per day - https://pypistats.org/

As maintainer, I guarantee that any backward incompatible changes will not be done in patch or minor version. But! Pay attention that sometimes output in keywords can be changed in minor version because of fixing wrong behaviour in past.

Articles with examples
^^^^^^^^^^^^^^^^^^^^^^


#. SQL Diagram (Part 3): SQL-to-ERD with DDL: https://levelup.gitconnected.com/sql-diagram-part-3-sql-to-erd-with-ddl-4c9840ee86c3

Updates in version 1.x
^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -549,6 +555,15 @@ for help with debugging & testing support for BigQuery dialect DDLs:
Changelog
---------

**v1.5.0**

Fixes
^^^^^


#. Now, ``unique`` set up to column only if it was only one column in unique constraint/index. Issue - https://github.com/xnuinside/simple-ddl-parser/issues/255
#. Fixed issue when UNIQUE KEY was identified as primary key - https://github.com/xnuinside/simple-ddl-parser/issues/253

**v1.4.0**

Fixes
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.4.0"
version = "1.5.0"
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
9 changes: 6 additions & 3 deletions simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,12 @@ def get_column_properties(p_list: List) -> Tuple:
references = None
if isinstance(p_list[-1], str):
if p_list[-1].upper() == "KEY":
pk = True
nullable = False
elif p_list[-1].upper() == "UNIQUE":
if p_list[-2].upper() == "UNIQUE":
unique = True
else:
pk = True
nullable = False
if p_list[-1].upper() == "UNIQUE":
unique = True
elif isinstance(p_list[-1], dict) and "references" in p_list[-1]:
p_list[-1]["references"]["column"] = p_list[-1]["references"]["columns"][0]
Expand Down
10 changes: 6 additions & 4 deletions simple_ddl_parser/output/base_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def set_column_unique_param(self, key: str) -> None:
check_in = []
else:
check_in = getattr(self, key, {})
if column["name"] in check_in:
if len(check_in) == 1 and column["name"] in check_in:
column["unique"] = True

def normalize_ref_columns_in_final_output(self):
Expand Down Expand Up @@ -285,9 +285,11 @@ def set_default_columns_from_alter(self, statement: Dict) -> None:

def set_unique_columns_from_alter(self, statement: Dict) -> None:
for column in self.columns:
for column_name in statement["unique"]["columns"]:
if column["name"] == column_name:
column["unique"] = True
if len(statement["unique"]["columns"]) == 1:
# if unique index only on one column
for column_name in statement["unique"]["columns"]:
if column["name"] == column_name:
column["unique"] = True

def alter_modify_columns(self, statement) -> None:
alter_key = "columns_to_modify"
Expand Down
4 changes: 2 additions & 2 deletions tests/dialects/test_mssql_specific.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ def test_alter_unique():
"references": None,
"size": (38, 20),
"type": "DECIMAL",
"unique": True,
"unique": False,
},
{
"check": None,
Expand Down Expand Up @@ -1386,7 +1386,7 @@ def test_alter_unique():
"references": None,
"size": 7,
"type": "DATETIME2",
"unique": True,
"unique": False,
},
{
"check": None,
Expand Down
114 changes: 114 additions & 0 deletions tests/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,117 @@ def test_unique_key_statement():
}
]
assert DDLParser(ddl).run() == expected


def test_unique_alter_sql():
ddl = """
CREATE TABLE "participations"(
"user_id" BIGINT NOT NULL,
"project_id" BIGINT NOT NULL,
"team_id" BIGINT NOT NULL,
);
ALTER TABLE
"participations" ADD CONSTRAINT "participations_team_id_user_id_unique" UNIQUE("team_id", "user_id");
"""

result = DDLParser(ddl).run()
expected = [
{
"alter": {
"uniques": [
{
"columns": ['"team_id"', '"user_id"'],
"constraint_name": '"participations_team_id_user_id_unique"',
}
]
},
"checks": [],
"columns": [
{
"check": None,
"default": None,
"name": '"user_id"',
"nullable": False,
"references": None,
"size": None,
"type": "BIGINT",
"unique": False,
},
{
"check": None,
"default": None,
"name": '"project_id"',
"nullable": False,
"references": None,
"size": None,
"type": "BIGINT",
"unique": False,
},
{
"check": None,
"default": None,
"name": '"team_id"',
"nullable": False,
"references": None,
"size": None,
"type": "BIGINT",
"unique": False,
},
],
"index": [],
"partitioned_by": [],
"primary_key": [],
"schema": None,
"table_name": '"participations"',
"tablespace": None,
}
]
assert expected == result


def test_unique_key():
ddl = """
CREATE TABLE `posts`(
`integer_column__unique` INT NOT NULL AUTO_INCREMENT UNIQUE,
`integer_column__unique_key` INT NOT NULL AUTO_INCREMENT UNIQUE KEY
);
"""

result = DDLParser(ddl).run()
expected = [
{
"alter": {},
"checks": [],
"columns": [
{
"autoincrement": True,
"check": None,
"default": None,
"name": "`integer_column__unique`",
"nullable": False,
"references": None,
"size": None,
"type": "INT",
"unique": True,
},
{
"autoincrement": True,
"check": None,
"default": None,
"name": "`integer_column__unique_key`",
"nullable": False,
"references": None,
"size": None,
"type": "INT",
"unique": True,
},
],
"index": [],
"partitioned_by": [],
"primary_key": [],
"schema": None,
"table_name": "`posts`",
"tablespace": None,
}
]
assert expected == result

0 comments on commit 6859e25

Please sign in to comment.