xnuinside / simple-ddl-parser

Simple DDL Parser to parse SQL (HQL, TSQL, AWS Redshift, BigQuery, Snowflake and other dialects) ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc. & table properties, types, domains, etc.
MIT License
183 stars 41 forks source link

Composite unique index also marks a column unique=true #255

Closed hubg398 closed 4 months ago

hubg398 commented 4 months ago

Example 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");

Output:


from simple_ddl_parser import DDLParser
import json

parse_results = DDLParser("""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");""").run()

[
    {
        "table_name": "\"participations\"",
        "schema": null,
        "primary_key": [],
        "columns": [
            {
                "name": "\"user_id\"",
                "type": "BIGINT",
                "size": null,
                "references": null,
                "unique": true,                 ------- This should probably be false
                "nullable": false,
                "default": null,
                "check": null
            },
            {
                "name": "\"project_id\"",
                "type": "BIGINT",
                "size": null,
                "references": null,
                "unique": false,
                "nullable": false,
                "default": null,
                "check": null
            },
            {
                "name": "\"team_id\"",
                "type": "BIGINT",
                "size": null,
                "references": null,
                "unique": true,            ------- This should probably be false
                "nullable": false,
                "default": null,
                "check": null
            }
        ],
        "alter": {
            "uniques": [
                {
                    "constraint_name": "\"participations_team_id_user_id_unique\"",
                    "columns": [
                        "\"team_id\"",
                        "\"user_id\""
                    ]
                }
            ]
        },
        "checks": [],
        "index": [],
        "partitioned_by": [],
        "tablespace": null
    }
]

It correctly identifies the composite uniques in alter.uniques. But for the individual columns, they also show "unique: true". I believe that's incorrect since there would be no way to know if the column itself has a non-composite unique index.

Looks like it's been recently fixed here: https://github.com/xnuinside/simple-ddl-parser/issues/236. Maybe the ALTER TABLE ... ADD CONSTRAINT UNQIUE were missed out there?

xnuinside commented 4 months ago

@hubg398 I think your words totally make sense, need to fix it

xnuinside commented 4 months ago

fixed in 1.5.0, really appreciate for opening this issue, test added https://github.com/xnuinside/simple-ddl-parser/pull/256, if will be needed anything else - feel free to open new one