senax-pgsql-parser
A comprehensive PostgreSQL database schema parser and DDL generator with PostGIS support.
β οΈ Important Notice
This code is AI-generated. The author does not fully understand all implementation details. Use at your own risk and verify the behavior thoroughly before using in production environments.
Features
- π Schema Analysis: Parse PostgreSQL database schemas with detailed information
- π DDL Generation: Generate CREATE and DROP DDL statements from schema data
- πΊοΈ PostGIS Support: Full support for PostGIS geometry and geography types
- π Roundtrip Testing: Verify schema integrity with comprehensive roundtrip tests
- π Constraint Support: Handle primary keys, foreign keys, indexes, and check constraints
- π Serial Types: Proper handling of serial, bigserial, and smallserial types
- π¬ Column Comments: Parse and generate column comments
- π― Type Safety: Strongly typed data structures with comprehensive enum support
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Library Usage
use ;
use Error;
async
Supported PostgreSQL Features
Data Types
- Basic Types: INTEGER, BIGINT, SMALLINT, VARCHAR, TEXT, BOOLEAN, etc.
- Numeric Types: DECIMAL, NUMERIC with precision and scale
- Serial Types: SERIAL, BIGSERIAL, SMALLSERIAL (with sequence detection)
- Date/Time Types: TIMESTAMP, DATE, TIME
- Array Types: Support for all array types
- Custom Types: ENUM types and user-defined types
- PostGIS Types: GEOMETRY, GEOGRAPHY with detailed spatial information
Constraints
- Primary Keys: Single and composite primary keys
- Foreign Keys: Single and composite foreign keys with CASCADE/RESTRICT actions
- Check Constraints: Custom validation constraints
- Unique Constraints: Unique indexes and constraints
Indexes
- Index Types: B-tree, Hash, GIN, GiST, SP-GiST, BRIN, Bloom
- Composite Indexes: Multi-column indexes
- Partial Indexes: Indexes with WHERE conditions
- Unique Indexes: Uniqueness constraints via indexes
PostGIS Support
- Spatial Types: Full support for GEOMETRY and GEOGRAPHY types
- Type Details: SRID, dimensions, and geometry type information
- Array Support: PostGIS array types
- Extension Detection: Automatic PostGIS extension requirement detection
Current Schema Support
The tool automatically detects and uses the current PostgreSQL schema using SELECT current_schema(), ensuring accurate schema analysis within the active database context.
Examples
Generate DDL from existing database
use ;
let pool = connect_to_database.await?;
let schema = get_database_schema.await?;
// Generate CREATE statements
let create_ddl = schema.to_create_ddl;
println!;
// Generate DROP statements
let drop_ddl = schema.to_drop_ddl;
println!;
Access detailed table information
for table in &schema.tables
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.