Package: pydal2sql
pydal2sqlpydal2sqlis a command line interface (CLI) tool that translates pydaldefine_tableTable definitions into SQL
statements. It supports different SQL dialects including SQLite, Postgres and MySQL. The tool generates
bothCREATE TABLEandALTER TABLESQL statements. It does this using pydal's own logic.Note: this package is only a Typer-based CLI front-end. The actual logic lives atrobinvandernoord/pydal2sql-core!Table of ContentsInstallationBasic UsageCreateGit IntegrationOptionsAlterGlobal OptionsConfigurationMagicAs a Python LibraryLicenseInstallationpipinstallpydal2sql# orpipxinstallpydal2sqlBasic UsageCREATEThe following commands are supported:pydal2sql create [file-name]: Translate the file into SQL statements.pydal2sql create [file-name]@[git-branch-or-commit-hash]: Translate a specific version of the file into SQL
statements.pydal2sql create [file-name]@latest: Translate the latest version of the file in git into SQL statements.pydal2sql create [file-name]@current: Translate the current version of the file on disk into SQL statements.pydal2sql createorpydal2sql create -: Prompts the user to input the table definitions via stdin.Bash pipe or redirect is also supported:cat [file-name] | pydal2sql createpydal2sql create < [file-name]Git IntegrationThe tool allows you to specify a git branch or commit hash when translating a file. Using the 'latest' keyword will
use the latest commit, and 'current' will use the file as it currently is on disk.Options--table,--tables,-t: Specify which database tables to generate CREATE statements for (default is all).--db-type,--dialect: Specify the SQL dialect to use (SQLite, Postgres, MySQL). The default is guessed from the
code or else the user is queried.--magic: If variables are missing, this flag will insert variables with that name so the code does (probably) not
crash.--noop: Doesn't create the migration code but only shows the Python code that would run to create it.ALTERpydal2sql alter [file1] [file2]: Generates the ALTER migration from the state in file1 to the state in file2.pydal2sql alter [file1]@[branch-or-commit-hash] [file2]@[branch-or-commit-hash]: Compares the files at those
specific versions and generates the ALTER migration.Using-instead of a file name will prompt the user via stdin to paste the define tables code.Global OptionsGlobal options that go before the subcommand:--verbosity: Sets how verbose the program should be, with a number between 1 and 4 (default is 2).--config: Path to a specific config toml file. Default is pyproject.toml at the key [tool.pydal2sql].--version: Prints the CLI tool version and exits.--show-config: Prints the currently used config and exits.Example:pydal2sql--verbosity3create
pydal2sql--versionConfigurationA configuration file (in toml) can be selected with--config. By default,pyproject.tomlis used.
In the configuration file, the following keys are supported under the [tool.pydal2sql] section:dialect/db-type: Default database dialect to use.magic: A boolean flag to use the--magicoption (default is False).noop: A boolean flag to use the--noopoption (default is False).tables: A list of table names to generate CREATE/ALTER statements for.The CLI command options can overwrite the config values. For example,--no-magicwill still set magic to False even if
it's set to True in the config file.Example of the toml configuration:[tool.pydal2sql]dialect="postgres"# postgres, mysql or sqlitemagic=truenoop=falsetables=["table1","table2"]All keys are optional.⚠️ Experimental 🪄✨Magic🌟💻If you're copy-pasting somedefine_tablestatements which have validators or defaults that are defined elsewhere,
the SQL generation could crash due to msising variables. However, if these variables are irrelevant to the samentics of
the table definition (i.e. only used at runtime, not for the schema definition), you can now try the--magicflag.This flag will replace all missing variables with a specialEmptyclass, which does nothing but
preventNameError,AttributeErrorandTypeErrors.Magicwill also remove local imports and imports that could not be found.This is of course not production-safe, so it shouldn't be used anywhere else.TODO:The following patterns are currently not supported:def define_tables(db): ...As a Python Librarypydal2sqlalso exposes agenerate_sqlmethod that can perform the same actions on one (for CREATE) or two (for
ALTER)pydal.Tableobjects when used within Python.frompydalimportDAL,Fieldfrompydal2sqlimportgenerate_sqldb=DAL(None,migrate=False)# <- without running database or with a different type of databaseperson_initial=db.define_table("person",Field("name","string",notnull=True,),Field("age","integer",default=18),Field("float","decimal(2,3)"),Field("nicknames","list:string"),Field("obj","json"),)print(generate_sql(db.person,db_type="psql"# or sqlite, or mysql; Optional with fallback to currently using database type.))CREATETABLEperson(idINTEGERPRIMARYKEYAUTOINCREMENT,nameVARCHAR(512),ageINTEGER,floatNUMERIC(2,3),nicknamesTEXT,objTEXT);person_new=db.define_table("person",Field("name","text",),Field("birthday","datetime"),redefine=True)generate_sql(person_initial,person_new,db_type="psql")ALTERTABLEpersonADD"name__tmp"TEXT;UPDATEpersonSET"name__tmp"=name;ALTERTABLEpersonDROPCOLUMNname;ALTERTABLEpersonADDnameTEXT;UPDATEpersonSETname="name__tmp";ALTERTABLEpersonDROPCOLUMN"name__tmp";ALTERTABLEpersonADDbirthdayTIMESTAMP;ALTERTABLEpersonDROPCOLUMNage;ALTERTABLEpersonDROPCOLUMNfloat;ALTERTABLEpersonDROPCOLUMNnicknames;ALTERTABLEpersonDROPCOLUMNobj;Licensepydal2sqlis distributed under the terms of theMITlicense.