Skip to main content

Sort SQL table definitions topologically based on foreign key dependencies

Project description

SchemaSorter

SchemaSorter is a Python library that automatically sorts SQL CREATE TABLE statements based on their foreign key dependencies using topological sorting. This ensures that tables are created in the correct order, preventing foreign key constraint violations during schema creation.

Features

  • Automatically sorts CREATE TABLE statements based on dependencies
  • Handles foreign key relationships
  • Detects circular dependencies
  • Optionally formats SQL with uppercase keywords
  • Maintains original SQL formatting and comments

Installation

pip install schemasorter

Usage

from schemasorter import sort_tables

# Example SQL DDL
sql_ddl = """
CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(100)
);
"""

# Sort tables based on dependencies
sorted_ddl = sort_tables(sql_ddl)
print(sorted_ddl)

The above example will output the CREATE TABLE statements in the correct order, with customers table first (since orders depends on it).

How It Works

  1. The library parses the input SQL DDL and extracts table definitions and their foreign key relationships
  2. It builds a dependency graph based on the foreign key constraints
  3. Uses Khan's algorithm to perform topological sorting on the tables
  4. Returns the sorted CREATE TABLE statements in the correct order

Parameters

sort_tables(sql_ddl: str, should_format: bool = True) -> str

  • sql_ddl: SQL DDL string containing CREATE TABLE statements
  • should_format: Whether to format and uppercase SQL keywords (default: True)

Error Handling

The library will raise a ValueError if it detects circular dependencies between tables.

Limitations

  • Currently only handles CREATE TABLE statements
  • Foreign key relationships must be explicitly defined using FOREIGN KEY constraints
  • Does not handle ALTER TABLE statements

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

schemasorter-0.1.0.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

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

schemasorter-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file schemasorter-0.1.0.tar.gz.

File metadata

  • Download URL: schemasorter-0.1.0.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.29

File hashes

Hashes for schemasorter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b2cee2dc100b85bd3eaff08f5e59c6c5a01223f3ad4c8fb445ac4c9045afdfe8
MD5 af2d80fd479c01c878d1108668094b1b
BLAKE2b-256 5702f7ed1297544f4545e44f361947e23cc144fdbf2a6399b4a9d9f5fa8ed981

See more details on using hashes here.

File details

Details for the file schemasorter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for schemasorter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acdad0e9012b7878bc0e2a9ba9cb5a59d6d0a4acce2d3099c85290ae9a92e119
MD5 66923e1890c00b5d542b91a447225760
BLAKE2b-256 df4b2c3beb4669da5463d8cb5510a2b6c1c7b0b166ee9617672825d90904e5dc

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