This crate provides a set of functions to generate SQL statements for various PostgreSQL schema objects

Overview

README

This crate provides a set of functions to generate SQL statements for various PostgreSQL schema objects, such as tables, views, materialized views, functions, triggers, and indexes. The generated SQL statements can be useful for schema introspection, documentation, or migration purposes.

Features

The module provides a PgSchema struct that accepts a namespace (schema name) as input and exposes methods for generating SQL statements for the following schema objects:

  • Enums
  • Composite types
  • Tables
  • Views
  • Materialized views
  • Functions
  • Triggers
  • Indexes

Usage

  1. Create an instance of PgSchema with the desired namespace (schema name).

rust

use db_schema::PgSchema;

let schema = PgSchema::new("your_schema_name");
  1. Use the available methods to generate SQL statements for the desired schema objects.

rust

// Get the SQL statements for all enums in the schema
let enums_sql = schema.enums();

// Get the SQL statements for all composite types in the schema
let types_sql = schema.types();

// Get the SQL statements for all tables in the schema
let tables_sql = schema.tables();

// Get the SQL statements for all views in the schema
let views_sql = schema.views();

// Get the SQL statements for all materialized views in the schema
let mviews_sql = schema.mviews();

// Get the SQL statements for all functions in the schema
let functions_sql = schema.functions();

// Get the SQL statements for all triggers in the schema
let triggers_sql = schema.triggers();

// Get the SQL statements for all indexes in the schema
let indexes_sql = schema.indexes();

You can also use the get_* methods to generate SQL statements for the desired schema objects. These methods accept a PgPool instance as input and return a Result, sqlx::Error>.

Example

Here's an example of how to retrieve the SQL statements for all schema objects in a given namespace (schema name):

rust

use db_schema::PgSchema;
use sqlx::PgPool;

async fn generate_sql_statements_for_schema(pool: &PgPool) -> Result<(), sqlx::Error> {
    let schema = PgSchema::new("your_schema_name");

    let enums = schema.get_enums(pool).await?;
    let types = schema.get_types(pool).await?;
    let tables = schema.get_tables(pool).await?;
    let views = schema.get_views(pool).await?;
    let mviews = schema.get_mviews(pool).await?;
    let functions = schema.get_functions(pool).await?;
    let triggers = schema.get_triggers(pool).await?;
    let indexes = schema.get_indexes(pool).await?;

    println!("Enums: {:?}", enums);
    println!("Types: {:?}", types);
    println!("Tables: {:?}", tables);
    println!("Views: {:?}", views);
    println!("Materialized Views: {:?}", mviews);
    println!("Functions: {:?}", functions);
    println!("Triggers: {:?}", triggers);
    println!("Indexes: {:?}", indexes);

    Ok(())
}

Tests

The code also includes tests to validate the functionality of the PgSchema struct. To run the tests, execute the following command:

sh

cargo nextest run
You might also like...
Event-sourcing Schema Definition Language

ESDL Event-sourcing Schema Definition Language Schema definition language for defining aggregates, commands, events & custom types. Heavily inspired b

Crate to generate files in ROFF format (Rust)

roffman A crate to generate roff man pages. Usage Add the following to the Cargo.toml: [dependencies] roffman = "0.3" Example use roffman::{Roff, Roff

Cargo-about - 📜 Cargo plugin to generate list of all licenses for a crate 🦀

📜 cargo-about Cargo plugin for generating a license listing for all dependencies of a crate See the book 📕 for in-depth documentation. Please Note:

Cornucopia is a small CLI utility resting on tokio-postgres and designed to facilitate PostgreSQL workflows in Rust

Cornucopia Generate type checked Rust from your SQL Install | Example Cornucopia is a small CLI utility resting on tokio-postgres and designed to faci

Background task processing for Rust applications with Tokio, Diesel, and PostgreSQL.
Background task processing for Rust applications with Tokio, Diesel, and PostgreSQL.

Async persistent background task processing for Rust applications with Tokio. Queue asynchronous tasks to be processed by workers. It's designed to be

Experimental extension that brings OpenAI API to your PostgreSQL to run queries in human language.

Postgres ChatGPT Experimental PostgreSQL extension that enables the use of OpenAI GPT API inside PostgreSQL, allowing for queries to be written usi

A utility for exporting administrative/moderation statistics from your Lemmy instance's PostgreSQL database to InfluxDB!
A utility for exporting administrative/moderation statistics from your Lemmy instance's PostgreSQL database to InfluxDB!

Lemmy (Stats) Data Exporter About This Project This project aims to act as a bridge between Lemmy's PostgreSQL database and InfluxDB, primarily to tra

A PostgreSQL extension for rendering the Tera HTML templating language.

PGTera PGTera is a PostgreSQL extension that provides functions for using Tera to render HTML templates. When used with a tool like Postgrest, you can

Command line tool to extract various data from Blender .blend files

blendtool Command line tool to extract various data from Blender .blend files. Currently supports dumping Eevee irradiance volumes to .dds, new featur

Owner
Tyr Chen
father, programming enthusiast, author and entrepreneur.
Tyr Chen
Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability and productivity.

Sleek: SQL Formatter ✨ Sleek is a CLI tool for formatting SQL. It helps you maintain a consistent style across your SQL code, enhancing readability an

Nick Rempel 40 Apr 20, 2023
tmplt is a command-line interface tool that allows you to quickly and easily set up project templates for various programming languages and frameworks

tmplt A User Friendly CLI Tool For Creating New Projects With Templates About tmplt is a command-line tool that lets users quickly create new projects

Humble Penguin 35 Apr 8, 2023
Given a set of kmers (fasta format) and a set of sequences (fasta format), this tool will extract the sequences containing the kmers.

Kmer2sequences Description Given a set of kmers (fasta / fastq [.gz] format) and a set of sequences (fasta / fastq [.gz] format), this tool will extra

Pierre Peterlongo 22 Sep 16, 2023
Crates.io library that provides high-level APIs for obtaining information on various entertainment media such as books, movies, comic books, anime, manga, and so on.

Crates.io library that provides high-level APIs for obtaining information on various entertainment media such as books, movies, comic books, anime, manga, and so on.

consumet-rs 5 Aug 13, 2023
Generate HTML source files from rust functions!

Htmlificator This crate provides an element struct which can be displayed as HTML. License This crate is licensed under the MIT license Credit This cr

Isotoxal 2 Nov 7, 2022
Schemars is a high-performance Python serialization library, leveraging Rust and PyO3 for efficient handling of complex objects

Schemars Introduction Schemars is a Python package, written in Rust and leveraging PyO3, designed for efficient and flexible serialization of Python c

Michael Gendy 7 Nov 21, 2023
Rust based Kubernetes Operator to deploy K8s objects minimally.

kube-nimble nimble /ˈnɪmbl/ - quick and light in movement or action; agile. This project began from a place of curiosity about Kubernetes CRDs and the

Meet Vasani 3 Feb 26, 2024
Rust library crate providing utility functions for diff and patch of slices

This crate provides the Change enum as an abstraction for diff::Result, lcs_diff::DiffResult, and wu_diff::DiffResult; the diff_changes(), diff_diff()

qtfkwk 5 Oct 19, 2022
Turbine is a toy CLI app for converting Rails schema declarations into equivalent type declarations in other languages.

Turbine Turbine is a toy CLI app for converting Rails schema declarations into equivalent type declarations in other languages. It’s described as a to

Justin 2 Jan 21, 2022
Traverse DMMF of Prisma schema, in your terminal

Let's DMMF Traverse DMMF of Prisma schema, in your terminal. Powered by jless. Installation brew tap yujong-lee/tap brew install letsdmmf Usage # lets

Jayden 6 Dec 9, 2022