{"id":16135290,"url":"https://github.com/xnuinside/simple-ddl-generator","last_synced_at":"2025-03-18T15:31:25.235Z","repository":{"id":44966830,"uuid":"444857625","full_name":"xnuinside/simple-ddl-generator","owner":"xnuinside","description":"(pretty fresh) Generate SQL DDL from simple-ddl-parser data \u0026 various Python models (pydantic, dataclasses, python enums, etc)","archived":false,"fork":false,"pushed_at":"2022-01-15T08:42:25.000Z","size":105,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-05-07T18:11:40.754Z","etag":null,"topics":["dataclasses","ddl","ddl-scripts","generator","models","pydantic","python","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xnuinside.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":"xnuinside","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2022-01-05T15:39:34.000Z","updated_at":"2023-03-02T04:34:18.000Z","dependencies_parsed_at":"2022-09-17T07:50:32.137Z","dependency_job_id":null,"html_url":"https://github.com/xnuinside/simple-ddl-generator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fsimple-ddl-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fsimple-ddl-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fsimple-ddl-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fsimple-ddl-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xnuinside","download_url":"https://codeload.github.com/xnuinside/simple-ddl-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243936385,"owners_count":20371505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dataclasses","ddl","ddl-scripts","generator","models","pydantic","python","sql"],"created_at":"2024-10-09T23:06:47.552Z","updated_at":"2025-03-18T15:31:24.933Z","avatar_url":"https://github.com/xnuinside.png","language":"Python","readme":"## Simple DDL Generator\n\n![badge1](https://img.shields.io/pypi/v/simple-ddl-generator) ![badge2](https://img.shields.io/pypi/l/simple-ddl-generator) ![badge3](https://img.shields.io/pypi/pyversions/simple-ddl-generator) ![workflow](https://github.com/xnuinside/simple-ddl-generator/actions/workflows/main.yml/badge.svg)\n\n## What is it?\n\nSimple DDL Generator generate SQL DDL from 3 different inputs. Idea of the generator same as for parser to support as much as possible DDLs in future.\n\nSimple DDL Generator generate SQL DDL from 3 input formats - 1st from output Simple DDL Parser (https://github.com/xnuinside/simple-ddl-parser), 2nd from py-models-parser - https://github.com/xnuinside/py-models-parser. Or you can directly pass TableMeta classes (https://github.com/xnuinside/table-meta) to generator\n\nNow DDL support pure SQL DDL diclaect and bunch of HQL statements.\n\n### Generate DDL from Django, SQLAlchemy, Dataclasses, Pydantic models and other\n\nGenerator can generate DDL from all models that supported \u0026 parsed by https://github.com/xnuinside/py-models-parser.\n\nIf you need DDL generation from another Python Model types - open issue request to add support for this models in parser. \n\n\n## How to use\n\nAs usually - more samples in tests/ \n\n```bash\n\n    pip install simple-ddl-generator\n\n```\n\n### Generate / Modify using existed DDL with Simple-DDL-Parser\n\n\nSample how you can modify your DDL using Simple DDL Parser \u0026 Simple DDL Parser\n\n```python\n\n    from simple_ddl_generator import DDLGenerator\n    from simple_ddl_parser import DDLParser\n\n    # take initial DDL\n    ddl = \"\"\"CREATE EXTERNAL TABLE IF NOT EXISTS database.table_name\n        (\n            day_long_nm     string,\n            calendar_dt     date,\n            source_batch_id string,\n            field_qty       decimal(10, 0),\n            field_bool      boolean,\n            field_float     float,\n            create_tmst     timestamp,\n            field_double    double,\n            field_long      bigint\n        ) PARTITIONED BY (batch_id int);\"\"\"\n    # get result from parser\n    data = DDLParser(ddl).run(group_by_type=True, output_mode=\"bigquery\")\n\n    # rename, for example, table name\n\n    data[\"tables\"][0][\"table_name\"] = \"new_table_name\"\n    g = DDLGenerator(data)\n    g.generate()\n    print(g.result)\n\n    # and result will be:\n\n    \"\"\"\n    CREATE EXTERNAL TABLE \"database.new_table_name\" (\n    day_long_nm string,\n    calendar_dt date,\n    source_batch_id string,\n    field_qty decimal(10, 0),\n    field_bool boolean,\n    field_float float,\n    create_tmst timestamp,\n    field_double double,\n    field_long bigint)\n    PARTITIONED BY (batch_id int);\n    \"\"\"\n\n```\n\n### Generate DDL from various Python Models with py-models-parser\n\n```python\n\n    from simple_ddl_generator import DDLGenerator\n    from py_models_parser import parse\n\n    # you can also read them from file\n    model_from = \"\"\"\n        class Material(BaseModel):\n\n            id: int\n            title: str\n            description: Optional[str]\n            link: str = 'http://'\n            type: Optional[MaterialType]\n            additional_properties: Optional[Json]\n            created_at: Optional[datetime.datetime] = datetime.datetime.now()\n            updated_at: Optional[datetime.datetime]\n        \"\"\"\n    # get data with parser\n    result = parse(model_from)\n\n    # if you want lower case table name before DDL generation you can just change in the result metadata, like this:\n    # result[0].table_name = \"material\"\n    # pass data to DDL Generator\n    g = DDLGenerator(result)\n    g.generate()\n    print(g.result)  \n\n    # resul will be\n\n    \"\"\"CREATE TABLE \"Material\" (\nid INTEGER,\ntitle VARCHAR,\ndescription VARCHAR,\nlink VARCHAR DEFAULT 'http://',\ntype MaterialType,\nadditional_properties JSON,\ncreated_at DATETIME DEFAULT now(),\nupdated_at DATETIME);\n\"\"\"\n\n```\n\n### Generate DDL Enum types from Python Enum \u0026 DDLs\n\nNow parser also generate CREATE TYPE statements.\n\nFor example (sample for generation DDL from Dataclasses):\n\n```python\n\n    from simple_ddl_generator import DDLGenerator\n    from py_models_parser import parse\n\n    model_from = \"\"\"\n\n    class MaterialType(str, Enum):\n\n        article = 'article'\n        video = 'video'\n\n\n    @dataclass\n    class Material:\n\n        id: int\n        description: str = None\n        additional_properties: Union[dict, list, tuple, anything] = None\n        created_at: datetime.datetime = datetime.datetime.now()\n        updated_at: datetime.datetime = None\n\n    @dataclass\n    class Material2:\n\n        id: int\n        description: str = None\n        additional_properties: Union[dict, list] = None\n        created_at: datetime.datetime = datetime.datetime.now()\n        updated_at: datetime.datetime = None\n\n    \"\"\"\n    result = parse(model_from)\n\n    g = DDLGenerator(result)\n    g.generate()\n    print(g.result)\n\n# result will be:\n\n\"\"\"CREATE TYPE MaterialType AS ENUM  ('article','video');\n\nCREATE TABLE Material (\nid INTEGER,\ndescription VARCHAR DEFAULT NULL,\nadditional_properties JSON DEFAULT NULL,\ncreated_at DATETIME DEFAULT now(),\nupdated_at DATETIME DEFAULT NULL);\n\nCREATE TABLE Material2 (\nid INTEGER,\ndescription VARCHAR DEFAULT NULL,\nadditional_properties JSON DEFAULT NULL,\ncreated_at DATETIME DEFAULT now(),\nupdated_at DATETIME DEFAULT NULL);\n\"\"\"\n```\n\n\n## Changelog\n**v0.4.1**\nNew Features:\n1. Added COMMENT statement to table generation\n\nImprovements:\n1. Added test to catch debug output (reminder: stop release at the middle night)\n\nFixes:\n1. Fixed issue with \n\n**v0.4.0**\nNew Features:\n1. Added base support for REFERENCE statement generation\n2. Added UNIQUE to column\n3. Added PRIMARY KEY to column\n3. To DDLGenerator added param lowercase to lowercase tables name.\n\n\n**v0.3.0**\nNew Features:\n1. Added CREATE TYPE generation from Python Enum \u0026 simple-ddl-parser types metadata\n\nImprovements:\n1. Added more test cases with models into tests\n2. Now output generated with empty line at the end\n\nFixes:\n\n1. Fixed issue with \"\" in names if quotes already exists in table-name in metadata\n\n**v0.2.0**\n\n1. Updated parser version in tests.\n2. Added support for EXTERNAL \u0026 IF NOT EXISTS statetements.\n3. Added support for using py-models-parser output as input and added sample in README.md:\n\nDDL Generation from Pydantic, SQLAlchemy and other python models.\n\n**v0.1.0**\n\nBase Generator Functionality with several test cases.\n","funding_links":["https://ko-fi.com/xnuinside"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fsimple-ddl-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxnuinside%2Fsimple-ddl-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fsimple-ddl-generator/lists"}