Skip to main content

CLI-agnostic pydal2sql code; Convert pydal define_tables to SQL using pydal's CREATE TABLE logic.

Project description

pydal2sql-core

PyPI - Version PyPI - Python Version
Code style: black License: MIT
su6 checks coverage.svg


Companion library for pydal2sql containing the actual logic. The other library only serves as a Typer-based CLI front-end.

More Documentation coming soon!

Table of Contents

Installation

pip install pydal2sql-core

As a Python Library

pydal2sql-core also exposes a generate_sql method that can perform the same actions on one (for CREATE) or two (for ALTER) pydal.Table objects when used within Python.

from pydal import DAL, Field
from pydal2sql_core import generate_sql

db = DAL(None, migrate=False)  # <- without running database or with a different type of database

person_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.
    )
)
CREATE TABLE person
(
    id        INTEGER PRIMARY KEY AUTOINCREMENT,
    name      VARCHAR(512),
    age       INTEGER,
    float     NUMERIC(2, 3),
    nicknames TEXT,
    obj       TEXT
);
person_new = db.define_table(
    "person",
    Field(
        "name",
        "text",
    ),
    Field("birthday", "datetime"),
    redefine=True
)

generate_sql(
    person_initial,
    person_new,
    db_type="psql"
)
ALTER TABLE person
    ADD "name__tmp" TEXT;
UPDATE person
SET "name__tmp"=name;
ALTER TABLE person DROP COLUMN name;
ALTER TABLE person
    ADD name TEXT;
UPDATE person
SET name="name__tmp";
ALTER TABLE person DROP COLUMN "name__tmp";
ALTER TABLE person
    ADD birthday TIMESTAMP;
ALTER TABLE person DROP COLUMN age;
ALTER TABLE person DROP COLUMN float;
ALTER TABLE person DROP COLUMN nicknames;
ALTER TABLE person DROP COLUMN obj;

License

pydal2sql-core is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydal2sql_core-0.6.1.tar.gz (116.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydal2sql_core-0.6.1-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file pydal2sql_core-0.6.1.tar.gz.

File metadata

  • Download URL: pydal2sql_core-0.6.1.tar.gz
  • Upload date:
  • Size: 116.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for pydal2sql_core-0.6.1.tar.gz
Algorithm Hash digest
SHA256 088ed3c2c9042a430a5469c189f002624d37b42814ee25e6bf3891307b490047
MD5 754aab1e062f1e3f172d42f321db6e74
BLAKE2b-256 8fb832d89ca7efe92ebe8b7f647a8bc4b860f2f86a745614742a5e8018d3d918

See more details on using hashes here.

File details

Details for the file pydal2sql_core-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydal2sql_core-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 10535ead6641b0599e709210bbec6d16bfd16128582a9a712fbe3592f233a562
MD5 aa6857a865ad74d3a18e5204207b1d54
BLAKE2b-256 9cdceee4085d6f71966d6cc5fcbacd0834eb82fdb7bc1293e52112a54b7d6d7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page