This crate provides a simple way to dump a database structure to a file, in SQL format.
It takes inspiration by the ruby on rails [schema dump].
```rust use std::path::PathBuf;
databaseschema::generatewithoutruntimeusing_defaults!(); ```
database-schema uses a set of [feature flags] to reduce the size of the libray and
therefore your binary. The way one should use this package is to pick the right
combination of feature flags for their use case. Below is a list of the available
feature flags and the combinations that are recommended for each use case.
sqlite: Enables SQLite support.postgres: Enables PostgreSQL support.mysql: Enables MySQL support.sqlx: Enables [sqlx] support.diesel: Enables [diesel] support.| Database | Query builder | Runtime |
|----------|---------------|---------|
| sqlite | sqlx | runtime-async-std |
| sqlite | sqlx | runtime-tokio |
| sqlite | diesel | |
| mysql | sqlx | runtime-async-std |
| mysql | sqlx | runtime-tokio |
| mysql | diesel | |
| postgres | sqlx | runtime-async-std |
| postgres | sqlx | runtime-tokio |
| postgres | diesel | |
The following are the recommended feature flag combinations for each use case.
First pick one of the following database feature flags:
sqlitemysqlpostgresThen pick one of the following database query building feature flags:
sqlxdieselIf you're using sqlx, you also have to pick one of the following runtime feature flags:
runtime-async-stdruntime-tokiotoml
[dependencies]
database-schema = { version = "0.1", features = ["sqlite", "sqlx", "runtime-async-std"] }
alternatively, if you're using diesel:
toml
[dependencies]
database-schema = { version = "0.1", features = ["sqlite", "diesel"] }
This crate also provides a set of macros that can be used to generate the SQL
structure of a database at compile time. This is useful for generating the SQL from
build.rs.
toml
[dependencies]
database-schema = { version = "0.1", features = ["sqlite", "diesel", "macros"] }
```rust use databaseschema::macros::generatewithout_runtime;
let sql = generatewithoutruntime!("./migrations", "structure.sql"); ```
The above is strictly equivalent to calling:
```rust use databaseschema::macros::generatewithoutruntimeusing_defaults;
let sql = generatewithoutruntime!(); ```
```rust use database_schema::DatabaseSchemaBuilder;
let migrationspath = "db/migrations"; let destinationpath = "db/structure.sql";
// This assumes you're using SQLite in memory.
//
// If you need to set up a connection_url you can use
// DatabaseSchemaBuilder::connection_url before calling
// build().
DatabaseSchemaBuilder::new() .migrationsdir(migrationspath)? .destinationpath(destinationpath) .build() .dump() .await ```