{"id":21388365,"url":"https://github.com/robinvandernoord/pydal2sql","last_synced_at":"2025-07-18T07:06:00.843Z","repository":{"id":182561672,"uuid":"668734753","full_name":"robinvandernoord/pydal2sql","owner":"robinvandernoord","description":"Convert pydal define_tables to SQL using pydal's CREATE/ALTER logic","archived":false,"fork":false,"pushed_at":"2024-08-05T17:06:28.000Z","size":170,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-28T04:40:09.236Z","etag":null,"topics":["pydal","python","sql","typedal"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/pydal2sql/","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/robinvandernoord.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-07-20T13:26:02.000Z","updated_at":"2025-06-26T14:09:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"f0900c35-45c4-4f5c-a17a-64d78ce11a64","html_url":"https://github.com/robinvandernoord/pydal2sql","commit_stats":null,"previous_names":["robinvandernoord/pydal2sql"],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/robinvandernoord/pydal2sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fpydal2sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fpydal2sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fpydal2sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fpydal2sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robinvandernoord","download_url":"https://codeload.github.com/robinvandernoord/pydal2sql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fpydal2sql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265716306,"owners_count":23816354,"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":["pydal","python","sql","typedal"],"created_at":"2024-11-22T12:16:54.133Z","updated_at":"2025-07-18T07:06:00.815Z","avatar_url":"https://github.com/robinvandernoord.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pydal2sql\n\n\n[![PyPI - Version](https://img.shields.io/pypi/v/pydal2sql.svg)](https://pypi.org/project/pydal2sql)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pydal2sql.svg)](https://pypi.org/project/pydal2sql)  \n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)  \n[![su6 checks](https://github.com/robinvandernoord/pydal2sql/actions/workflows/su6.yml/badge.svg?branch=development)](https://github.com/robinvandernoord/pydal2sql/actions)\n![coverage.svg](coverage.svg)\n\n-----\n\n\n`pydal2sql` is a command line interface (CLI) tool that translates pydal `define_table` Table definitions into SQL\nstatements. It supports different SQL dialects including SQLite, Postgres and MySQL. The tool generates\nboth `CREATE TABLE` and `ALTER TABLE` SQL statements. It does this using pydal's own logic.\n\nNote: this package is only a Typer-based CLI front-end. The actual logic lives at [`robinvandernoord/pydal2sql-core`](https://github.com/robinvandernoord/pydal2sql-core)!\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Basic Usage](#basic-usage)\n  - [Create](#create)\n    - [Git Integration](#git-integration)\n    - [Options](#options)\n  - [Alter](#alter)\n  - [Global Options](#global-options)\n  - [Configuration](#configuration)\n  - [Magic](#-experimental-magic)\n- [As a Python Library](#as-a-python-library)\n- [License](#license)\n\n## Installation\n\n```bash\npip install pydal2sql\n# or\npipx install pydal2sql\n```\n\n## Basic Usage\n\n### `CREATE`\n\nThe following commands are supported:\n\n- `pydal2sql create [file-name]`: Translate the file into SQL statements.\n- `pydal2sql create [file-name]@[git-branch-or-commit-hash]`: Translate a specific version of the file into SQL\n  statements.\n- `pydal2sql create [file-name]@latest`: Translate the latest version of the file in git into SQL statements.\n- `pydal2sql create [file-name]@current`: Translate the current version of the file on disk into SQL statements.\n- `pydal2sql create` or `pydal2sql create -`: Prompts the user to input the table definitions via stdin.\n\nBash pipe or redirect is also supported:\n\n- `cat [file-name] | pydal2sql create`\n- `pydal2sql create \u003c [file-name]`\n\n#### Git Integration\n\nThe tool allows you to specify a git branch or commit hash when translating a file. Using the 'latest' keyword will\nuse the latest commit, and 'current' will use the file as it currently is on disk.\n\n#### Options\n\n- `--table`, `--tables`, `-t`: Specify which database tables to generate CREATE statements for (default is all).\n- `--db-type`, `--dialect`: Specify the SQL dialect to use (SQLite, Postgres, MySQL). The default is guessed from the\n  code or else the user is queried.\n- `--magic`: If variables are missing, this flag will insert variables with that name so the code does (probably) not\n  crash.\n- `--noop`: Doesn't create the migration code but only shows the Python code that would run to create it.\n\n### `ALTER`\n\n- `pydal2sql alter [file1] [file2]`: Generates the ALTER migration from the state in file1 to the state in file2.\n- `pydal2sql alter [file1]@[branch-or-commit-hash] [file2]@[branch-or-commit-hash]`: Compares the files at those\n  specific versions and generates the ALTER migration.\n\nUsing `-` instead of a file name will prompt the user via stdin to paste the define tables code.\n\n### Global Options\n\nGlobal options that go before the subcommand:\n\n- `--verbosity`: Sets how verbose the program should be, with a number between 1 and 4 (default is 2).\n- `--config`: Path to a specific config toml file. Default is pyproject.toml at the key [tool.pydal2sql].\n- `--version`: Prints the CLI tool version and exits.\n- `--show-config`: Prints the currently used config and exits.\n\nExample:\n\n```bash\npydal2sql --verbosity 3 create\npydal2sql --version\n```\n\n### Configuration\n\nA configuration file (in toml) can be selected with `--config`. By default, `pyproject.toml` is used.\nIn the configuration file, the following keys are supported under the [tool.pydal2sql] section:\n\n- `dialect`/`db-type`: Default database dialect to use.\n- `magic`: A boolean flag to use the `--magic` option (default is False).\n- `noop`: A boolean flag to use the `--noop` option (default is False).\n- `tables`: A list of table names to generate CREATE/ALTER statements for.\n\nThe CLI command options can overwrite the config values. For example, `--no-magic` will still set magic to False even if\nit's set to True in the config file.\n\nExample of the toml configuration:\n\n```toml\n[tool.pydal2sql]\ndialect = \"postgres\" # postgres, mysql or sqlite\nmagic = true\nnoop = false\ntables = [\"table1\", \"table2\"]\n```\n\nAll keys are optional.\n\n### ⚠️ Experimental 🪄✨Magic🌟💻\n\nIf you're copy-pasting some `define_table` statements which have validators or defaults that are defined elsewhere,\nthe SQL generation could crash due to msising variables. However, if these variables are irrelevant to the samentics of\nthe table definition (i.e. only used at runtime, not for the schema definition), you can now try the `--magic` flag.\n\nThis flag will replace all missing variables with a special `Empty` class, which does nothing but\nprevent `NameError`, `AttributeError` and `TypeError`s.   \n\n`Magic` will also remove local imports and imports that could not be found.\n\nThis is of course not production-safe, so it shouldn't be used anywhere else.\n\n#### TODO:\nThe following patterns are currently not supported:\n- `def define_tables(db): ...`\n\n## As a Python Library\n\n`pydal2sql` also exposes a `generate_sql` method that can perform the same actions on one (for CREATE) or two (for\nALTER) `pydal.Table` objects when used within Python.\n\n```python\nfrom pydal import DAL, Field\nfrom pydal2sql import generate_sql\n\ndb = DAL(None, migrate=False)  # \u003c- without running database or with a different type of database\n\nperson_initial = db.define_table(\n    \"person\",\n    Field(\n        \"name\",\n        \"string\",\n        notnull=True,\n    ),\n    Field(\"age\", \"integer\", default=18),\n    Field(\"float\", \"decimal(2,3)\"),\n    Field(\"nicknames\", \"list:string\"),\n    Field(\"obj\", \"json\"),\n)\n\nprint(\n    generate_sql(\n        db.person, db_type=\"psql\"  # or sqlite, or mysql; Optional with fallback to currently using database type.\n    )\n)\n```\n\n```sql\nCREATE TABLE person\n(\n    id        INTEGER PRIMARY KEY AUTOINCREMENT,\n    name      VARCHAR(512),\n    age       INTEGER,\n    float     NUMERIC(2, 3),\n    nicknames TEXT,\n    obj       TEXT\n);\n```\n\n```python\nperson_new = db.define_table(\n    \"person\",\n    Field(\n        \"name\",\n        \"text\",\n    ),\n    Field(\"birthday\", \"datetime\"),\n    redefine=True\n)\n\ngenerate_sql(\n    person_initial,\n    person_new,\n    db_type=\"psql\"\n)\n```\n\n```sql\nALTER TABLE person ADD \"name__tmp\" TEXT;\nUPDATE person SET \"name__tmp\"=name;\nALTER TABLE person DROP COLUMN name;\nALTER TABLE person ADD name TEXT;\nUPDATE person SET name=\"name__tmp\";\nALTER TABLE person DROP COLUMN \"name__tmp\";\nALTER TABLE person ADD birthday TIMESTAMP;\nALTER TABLE person DROP COLUMN age;\nALTER TABLE person DROP COLUMN float;\nALTER TABLE person DROP COLUMN nicknames;\nALTER TABLE person DROP COLUMN obj;\n```\n\n## License\n\n`pydal2sql` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinvandernoord%2Fpydal2sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobinvandernoord%2Fpydal2sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinvandernoord%2Fpydal2sql/lists"}