Convert pydal define_tables to SQL using pydal's CREATE TABLE logic
Project description
pydal2sql
Convert pydal define_tables to SQL using pydal's CREATE TABLE logic
Table of Contents
Example
from pydal import DAL, Field
from pydal2sql import generate_sql
db = DAL(None, migrate=False) # <- without running database or with a different type of database
db.define_table(
"person",
Field(
"name",
"string",
nullable=False,
),
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.
)
)
CREATE TABLE person
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(512),
age INTEGER,
float NUMERIC(2, 3),
nicknames TEXT,
obj TEXT
);
Installation
pip install pydal2sql
# or
pipx install pydal2sql
cli
Given a Python file:
# model_fragment.py
db.define_table(
"person",
Field(...)
)
# optionally:
# db_type = 'postgres'
# tables = ['person']
Then in the terminal:
cat model_fragment.py | pydal2sql # without cli args if settings are set in code, or
cat model_fragment.py | pydal2sql postgres --table person # with args if settings are not in code
# both output the CREATE TABLE statements to stdout.
# alternatively, you can simply run `pydal2sql` and you will be prompted for the code, which you can then paste.
⚠️ Experimental 🪄✨Magic🌟💻
If you're copy-pasting some define_table
statements 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 --magic
flag.
This flag will replace all missing variables with a special Empty
class, which does nothing but
prevent NameError
, AttributeError
and TypeError
s.
This is of course not production-safe, so it shouldn't be used anywhere else.
Example with pipx
License
pydal2sql
is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydal2sql-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daa139bda4a7b3dd8bacd5d77a84588ec58f619ff86d4ce6a4eef1186a754a71 |
|
MD5 | 1f310a8c9a72d473e656a5a3bc81b3fd |
|
BLAKE2b-256 | 9a381a63faaa526bcea9d88c30e7c60e93def5a67a763847353daf051af42696 |