Skip to main content

DDL Generator

Infers SQL DDL (Data Definition Language) from table data.

Use at command line:

$ ddlgenerator -i postgresql '{"Name": "Alfred", "species": "wart hog", "kg": 22}'

DROP TABLE generated_table;
CREATE TABLE generated_table (
        name VARCHAR(6) NOT NULL,
        kg INTEGER NOT NULL,
        species VARCHAR(8) NOT NULL
)
;
INSERT INTO generated_table (kg, Name, species) VALUES (22, 'Alfred', 'wart hog');

Reads data from files:

$ ddlgenerator postgresql mydata.yaml > mytable.sql

Enables one-line creation of tables with their data

$ ddlgenerator –inserts postgresql mydata.json | psql

To use in Python:

>>> from ddlgenerator.ddlgenerator import Table
>>> table = Table({"Name": "Alfred", "species": "wart hog", "kg": 22})
>>> sql = table.sql('postgresql', inserts=True)

Options

  • Free software: MIT license

Supported data formats

  • Pure Python

  • YAML

  • JSON

  • CSV

  • Pickle

Features

  • Supports all SQL dialects supported by SQLAlchemy

  • Coerces data into most specific data type valid on all column’s values

  • Takes table name from file name

  • Guesses format of input data if unspecified by file extension

  • with -i/--inserts flag, adds INSERT statements

  • with -u/--uniques flag, surmises UNIQUE constraints from data

  • Handles nested data, creating child tables as needed

  • Accepts wildcards in filenames (remember quote marks to avoid shell expansion)

Options

-h, --help            show this help message and exit
-k KEY, --key KEY     Field to use as primary key
-r, --reorder         Reorder fields alphabetically, ``key`` first
-u, --uniques         Include UNIQUE constraints where data is unique
-t, --text            Use variable-length TEXT columns instead of VARCHAR
-d, --drops           Include DROP TABLE statements
-i, --inserts         Include INSERT statements
--no-creates          Do not include CREATE TABLE statements
--save-metadata-to FILENAME
                      Save table definition in FILENAME for later --use-
                      saved-metadata run
--use-metadata-from FILENAME
                      Use metadata saved in FROM for table definition, do
                      not re-analyze table structure
-l LOG, --log LOG     log level (CRITICAL, FATAL, ERROR, DEBUG, INFO, WARN)

Large tables

As of now, ddlgenerator is not well-designed for table sizes approaching your system’s available memory. It does accept a –limit keyword that should help when creating DDL from very large tables.

Another (messier) approach is to break your input data into multiple files, then run ddlgenerator with --save-metadata against a small but representative sample. Then run with --no-creates and -use-saved-metadata to generate INSERTs from the remaining files without needing to re-determine the column types each time.

Installing

git clone git clone https://github.com/catherinedevlin/ddl-generator.git
cd ddl-generator
python setup.py install

Credits

  • Mike Bayer for sqlalchemy

  • coldfix and Mark Ransom for their StackOverflow answers

  • Audrey Roy for cookiecutter

History

0.1.1 (2014-05-23)

  • First release on PyPI.

Metadata

Release files for ddlgenerator 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ddlgenerator 0.1.1
File Size Uploaded
ddlgenerator-0.1.1.tar.gz 18.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ddlgenerator 0.1.1
File Interpreter ABI Platform
ddlgenerator-0.1.1-py3.4.egg Legacy Egg format - - Details

Total release size: 55.7 kB

Release files / ddlgenerator-0.1.1.tar.gz

Download URL ddlgenerator-0.1.1.tar.gz
Size 18.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1163341edec50aa14963fc3c3c558920051c6f86a836b0b4de789c3ffb3b0984
BLAKE2b-256 checksum
How to use checksums
0fad32b5b2f27116a8fef0941f1796bd17a562e2ae4fee8aeb1fbda8e62bf331
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ddlgenerator-0.1.1-py3.4.egg

Download URL ddlgenerator-0.1.1-py3.4.egg
Size 37.7 kB
Tags Egg
SHA-256 checksum
How to use checksums
3e405a2aee3960d257b3f381724e7e9c66686db341091d3697eacdad98c2ad48
BLAKE2b-256 checksum
How to use checksums
b0d804d311a42db5c152d7a344f3bba0ec5bf8bcd0b8d2cbc9cb777838939a49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

0.1.4

1 release file

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page