Crates.io | sqlmo |
lib.rs | sqlmo |
version | 0.22.7 |
source | src |
created_at | 2023-01-16 00:47:50.103813 |
updated_at | 2024-11-20 21:56:08.731187 |
description | SQL data primitives. Use it to generate SQL queries, auto-generate SQL migrations, and more. |
homepage | https://github.com/kurtbuilds/sqlmo |
repository | https://github.com/kurtbuilds/sqlmo |
max_upload_size | |
id | 759771 |
size | 71,932 |
sqlmo
sqlmo
is a set of primitives to represent SQL tables and queries. Use these primitives to:
sqlmo::Schema
), calculate differences between
schemas (sqlmo::Migration
), and generate SQL to apply the migration (sqlmo::Migration::to_sql
).For auto-generating migrations, there are a few built-in schema sources:
sqlmo_sqlx
sqlmo_openapi
If you need another source, you should define a way to build a sqlmo::Schema
from your data source, then use sqlmo
to auto-generate migrations.
Current tools that support this:
If you use this library, submit a PR to be added to this list.
This example reads the schema from a postgres database, defines an empty schema (which should be filled in), and then computes the migration to apply to the database.
use sqlmo_sqlx::FromPostgres;
#[tokio::main]
async fn main() {
let url = std::env::var("DATABASE_URL").unwrap();
let mut conn = sqlx::postgres::PgConnection::connect(&url).await?;
let current = Schema::try_from_postgres(&mut conn, schema_name).await?;
let end_state = Schema::default(); // Load your end-state by manually defining it, or building it from another source
let migration = current.migrate_to(end_state, &sqlmo::Options::default());
for statement in migration.statements {
let statement = statement.to_sql(Dialect::Postgres);
println!("{}", statement);
}
}