https://github.com/ilevn/sql-generator
SQL Generator for Postgresql-backed systems
https://github.com/ilevn/sql-generator
generator postgresql psycopg2 python python39 sql
Last synced: 5 months ago
JSON representation
SQL Generator for Postgresql-backed systems
- Host: GitHub
- URL: https://github.com/ilevn/sql-generator
- Owner: ilevn
- License: mit
- Created: 2020-12-04T19:13:36.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-19T07:43:53.000Z (over 4 years ago)
- Last Synced: 2025-02-15T17:48:25.337Z (11 months ago)
- Topics: generator, postgresql, psycopg2, python, python39, sql
- Language: Python
- Homepage:
- Size: 102 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
sql-generator
=============
This tool lets you generate postgresql insert and copy statements for common data types.
It's pretty minimalistic at the moment but offers support for basic statements
and foreign key relations.
In addition to that, `sql-generator` supports custom generators for table columns
and data types.
Installation
------------
**Python 3.9 or higher is required**
To install the module, run the following command:
.. code:: sh
# Linux/macOS
python3 -m pip install -U sql-generator
# Windows
py -3 -m pip install -U sql-generator
# Poetry
poetry add sql-generator
Usage
-----
.. code:: py
import logging
import psycopg2
from sql_generator import Generator, write_statements_as_copy, write_statements_as_insert
logging.basicConfig(level=logging.INFO)
# Custom generator for all columns with `custom_a` as name.
def gen_column_a(column):
return f"My data type is {column.data_type}!"
conn = psycopg2.connect("your dsn")
gen = Generator(conn, column_generators={"column_a": gen_column_a})
# Amount of inserts per table.
amounts = {"table_a": 50, "table_b": 25, "table_c": 100}
generated_statements: dict = gen.generate_table_data_for_all(amounts)
# Write generated statements as INSERTs to file.
# You can optionally truncate your db with `should_truncate=True`.
write_statements_as_insert(generated_statements, dest="insert_output.sql")
# Write generated statements as COPYs to file.
write_statements_as_copy(generated_statements, dest="copy_output.sql")