Skip to main content

Module db_schema

Module db_schema 

Source
Available on crate features model-mysql or model-postgres or model-sqlite only.
Expand description

The database’s schema as data (18.30.0): DbPool::schema reads every table of the connected schema — columns (type, nullability, default), primary key, indexes and foreign keys — from the backend’s catalog (sqlite_master and the pragma_* functions on SQLite, information_schema and pg_catalog on PostgreSQL, information_schema on MySQL), and DbSchema::to_json writes it for tools: rws db schema --json and the dev MCP server’s db_schema.

“The connected schema” is SQLite’s main database, PostgreSQL’s current_schema() (the first of the search_path), MySQL’s DATABASE(). Views are left out; rws’s _schema_migrations table is listed (it is part of the schema).

use rust_web_server::model::DbPool;

let pool = DbPool::memory().await?;
pool.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE)", &[]).await?;
let schema = pool.schema().await?;
let users = schema.table("users").unwrap();
assert_eq!(vec!["id".to_string()], users.primary_key);
assert!(users.indexes.iter().any(|i| i.unique && i.columns == ["email"]));
println!("{}", schema.to_json());

Structs§

ColumnSchema
One column.
DbSchema
Every table of the connected schema, by name.
ForeignKeySchema
One foreign key.
IndexSchema
One index.
TableSchema
One table.