Skip to content

taro365-dev/ddl2insert_gen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ddl2insert_gen

Generate basic INSERT statements automatically from PostgreSQL CREATE TABLE DDL.

This tool analyzes table definitions and foreign key relationships, then generates sample INSERT SQL for testing or development environments.

Example

DDL:

CREATE TABLE customers (
  customer_id SERIAL PRIMARY KEY,
  customer_name VARCHAR(50)
);

Generated INSERT:

INSERT INTO customers (customer_id, customer_name)
VALUES (1, 'customer_name_1');

Features

  • Parse PostgreSQL CREATE TABLE DDL
  • Analyze primary and foreign key relationships
  • Generate basic INSERT statements
  • CLI-based usage

Roadmap

  • CTE-based INSERT generation
  • Identity-aware insert chaining
  • Dependency handling

Repository Structure

ddl2insert_gen
├ src
│  └ ddl2insert.py
├ example
│  ├ schema.sql
│  ├ column_value_config.json
│  ├ run_example.sh
│  └ output
│     ├ mountain_01.sql
│     ├ mountain_02.sql
│     └ column_template.json
├ README.md
├ LICENSE
└ requirements.txt

Usage

1. Prepare a DDL file

Example input file:

example/schema.sql

Example output files:

example/output/mountain_01.sql
example/output/mountain_02.sql

2. Run the script

python src/ddl2insert.py example/schema.sql

Windows users can also run:

py src/ddl2insert.py example/schema.sql

You can also run the helper script:

sh example/run_example.sh

Advanced Usage

Column Value Configuration

The file column_value_config.json can be edited to customize the values used when generating INSERT statements.

You can specify values for each column.

The tool also generates a file named output/column_template.json, which contains detected columns and sample values.
You can use this file as a template to create or update column_value_config.json.

Typical workflow

  1. Run the tool once to generate output/column_template.json
  2. Copy or rename output/column_template.json to column_value_config.json
  3. Edit the VALUES field to control generated data
  4. Run the tool again

Rules

  • If a value is defined in VALUES, that value will be used
  • If VALUES is empty, the system automatically generates a sample value based on the column type
  • Columns without configuration are not affected

Example configuration

{
  "COLUMN": "is_deleted",
  "TYPE(S)": "boolean",
  "VALUES": [false]
}

Quick Start

Run the tool with the example schema:

python src/ddl2insert.py example/schema.sql

Generated SQL files will appear in the example/output directory.

Alternatively, run the helper script:

sh example/run_example.sh

Supported DDL

Currently supported elements:

  • CREATE TABLE
  • PRIMARY KEY
  • FOREIGN KEY

Support for additional PostgreSQL features will be added in future updates.


Processing Flow

DDL
 ↓
Parser
 ↓
Column Template JSON (output/column_template.json)
 ↓
User Configuration (column_value_config.json)
 ↓
INSERT SQL Generation

License

This project is licensed under the MIT License.

See the LICENSE file for details.

Packages

 
 
 

Contributors

Languages