Skip to content

Commit 6859e25

Browse files
committed
fix issues with unique indexes with multiple columns (wrong attribute setting)
1 parent dd3e4c8 commit 6859e25

File tree

8 files changed

+160
-10
lines changed

8 files changed

+160
-10
lines changed

CHANGELOG.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
**v1.5.0**
2+
3+
### Fixes
4+
5+
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
6+
2. Fixed issue when UNIQUE KEY was identified as primary key - https://github.com/xnuinside/simple-ddl-parser/issues/253
7+
8+
19
**v1.4.0**
210

311
### Fixes

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ for help with debugging & testing support for BigQuery dialect DDLs:
490490

491491

492492
## Changelog
493+
**v1.5.0**
494+
495+
### Fixes
496+
497+
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
498+
2. Fixed issue when UNIQUE KEY was identified as primary key - https://github.com/xnuinside/simple-ddl-parser/issues/253
499+
500+
493501
**v1.4.0**
494502

495503
### Fixes

docs/README.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Yes, library already has about 9000+ downloads per day - https://pypistats.org/
2929

3030
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.
3131

32+
Articles with examples
33+
^^^^^^^^^^^^^^^^^^^^^^
34+
35+
36+
#. SQL Diagram (Part 3): SQL-to-ERD with DDL: https://levelup.gitconnected.com/sql-diagram-part-3-sql-to-erd-with-ddl-4c9840ee86c3
37+
3238
Updates in version 1.x
3339
^^^^^^^^^^^^^^^^^^^^^^
3440

@@ -549,6 +555,15 @@ for help with debugging & testing support for BigQuery dialect DDLs:
549555
Changelog
550556
---------
551557

558+
**v1.5.0**
559+
560+
Fixes
561+
^^^^^
562+
563+
564+
#. 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
565+
#. Fixed issue when UNIQUE KEY was identified as primary key - https://github.com/xnuinside/simple-ddl-parser/issues/253
566+
552567
**v1.4.0**
553568

554569
Fixes

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "simple-ddl-parser"
3-
version = "1.4.0"
3+
version = "1.5.0"
44
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."
55
authors = ["Iuliia Volkova <[email protected]>"]
66
license = "MIT"

simple_ddl_parser/dialects/sql.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,12 @@ def get_column_properties(p_list: List) -> Tuple:
421421
references = None
422422
if isinstance(p_list[-1], str):
423423
if p_list[-1].upper() == "KEY":
424-
pk = True
425-
nullable = False
426-
elif p_list[-1].upper() == "UNIQUE":
424+
if p_list[-2].upper() == "UNIQUE":
425+
unique = True
426+
else:
427+
pk = True
428+
nullable = False
429+
if p_list[-1].upper() == "UNIQUE":
427430
unique = True
428431
elif isinstance(p_list[-1], dict) and "references" in p_list[-1]:
429432
p_list[-1]["references"]["column"] = p_list[-1]["references"]["columns"][0]

simple_ddl_parser/output/base_data.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def set_column_unique_param(self, key: str) -> None:
112112
check_in = []
113113
else:
114114
check_in = getattr(self, key, {})
115-
if column["name"] in check_in:
115+
if len(check_in) == 1 and column["name"] in check_in:
116116
column["unique"] = True
117117

118118
def normalize_ref_columns_in_final_output(self):
@@ -285,9 +285,11 @@ def set_default_columns_from_alter(self, statement: Dict) -> None:
285285

286286
def set_unique_columns_from_alter(self, statement: Dict) -> None:
287287
for column in self.columns:
288-
for column_name in statement["unique"]["columns"]:
289-
if column["name"] == column_name:
290-
column["unique"] = True
288+
if len(statement["unique"]["columns"]) == 1:
289+
# if unique index only on one column
290+
for column_name in statement["unique"]["columns"]:
291+
if column["name"] == column_name:
292+
column["unique"] = True
291293

292294
def alter_modify_columns(self, statement) -> None:
293295
alter_key = "columns_to_modify"

tests/dialects/test_mssql_specific.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ def test_alter_unique():
13261326
"references": None,
13271327
"size": (38, 20),
13281328
"type": "DECIMAL",
1329-
"unique": True,
1329+
"unique": False,
13301330
},
13311331
{
13321332
"check": None,
@@ -1386,7 +1386,7 @@ def test_alter_unique():
13861386
"references": None,
13871387
"size": 7,
13881388
"type": "DATETIME2",
1389-
"unique": True,
1389+
"unique": False,
13901390
},
13911391
{
13921392
"check": None,

tests/test_unique.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,117 @@ def test_unique_key_statement():
259259
}
260260
]
261261
assert DDLParser(ddl).run() == expected
262+
263+
264+
def test_unique_alter_sql():
265+
ddl = """
266+
CREATE TABLE "participations"(
267+
"user_id" BIGINT NOT NULL,
268+
"project_id" BIGINT NOT NULL,
269+
"team_id" BIGINT NOT NULL,
270+
);
271+
ALTER TABLE
272+
"participations" ADD CONSTRAINT "participations_team_id_user_id_unique" UNIQUE("team_id", "user_id");
273+
"""
274+
275+
result = DDLParser(ddl).run()
276+
expected = [
277+
{
278+
"alter": {
279+
"uniques": [
280+
{
281+
"columns": ['"team_id"', '"user_id"'],
282+
"constraint_name": '"participations_team_id_user_id_unique"',
283+
}
284+
]
285+
},
286+
"checks": [],
287+
"columns": [
288+
{
289+
"check": None,
290+
"default": None,
291+
"name": '"user_id"',
292+
"nullable": False,
293+
"references": None,
294+
"size": None,
295+
"type": "BIGINT",
296+
"unique": False,
297+
},
298+
{
299+
"check": None,
300+
"default": None,
301+
"name": '"project_id"',
302+
"nullable": False,
303+
"references": None,
304+
"size": None,
305+
"type": "BIGINT",
306+
"unique": False,
307+
},
308+
{
309+
"check": None,
310+
"default": None,
311+
"name": '"team_id"',
312+
"nullable": False,
313+
"references": None,
314+
"size": None,
315+
"type": "BIGINT",
316+
"unique": False,
317+
},
318+
],
319+
"index": [],
320+
"partitioned_by": [],
321+
"primary_key": [],
322+
"schema": None,
323+
"table_name": '"participations"',
324+
"tablespace": None,
325+
}
326+
]
327+
assert expected == result
328+
329+
330+
def test_unique_key():
331+
ddl = """
332+
CREATE TABLE `posts`(
333+
`integer_column__unique` INT NOT NULL AUTO_INCREMENT UNIQUE,
334+
`integer_column__unique_key` INT NOT NULL AUTO_INCREMENT UNIQUE KEY
335+
);
336+
"""
337+
338+
result = DDLParser(ddl).run()
339+
expected = [
340+
{
341+
"alter": {},
342+
"checks": [],
343+
"columns": [
344+
{
345+
"autoincrement": True,
346+
"check": None,
347+
"default": None,
348+
"name": "`integer_column__unique`",
349+
"nullable": False,
350+
"references": None,
351+
"size": None,
352+
"type": "INT",
353+
"unique": True,
354+
},
355+
{
356+
"autoincrement": True,
357+
"check": None,
358+
"default": None,
359+
"name": "`integer_column__unique_key`",
360+
"nullable": False,
361+
"references": None,
362+
"size": None,
363+
"type": "INT",
364+
"unique": True,
365+
},
366+
],
367+
"index": [],
368+
"partitioned_by": [],
369+
"primary_key": [],
370+
"schema": None,
371+
"table_name": "`posts`",
372+
"tablespace": None,
373+
}
374+
]
375+
assert expected == result

0 commit comments

Comments
 (0)