Skip to main content

schema_reverse_engineer/postgres/
views.rs

1use crate::error::SchemaReverseEngineerError;
2use schema_model::model::types::DatabaseType;
3use schema_model::model::view::View;
4use sqlx::PgPool;
5
6pub async fn list_views(pool: &PgPool, db_schema: &str) -> Result<Vec<View>, SchemaReverseEngineerError> {
7    let rows: Vec<(String, String)> = sqlx::query_as(
8        "SELECT table_name, view_definition \
9         FROM information_schema.views \
10         WHERE table_schema = $1 \
11         ORDER BY table_name",
12    )
13    .bind(db_schema)
14    .fetch_all(pool)
15    .await
16    .map_err(|e| SchemaReverseEngineerError::Introspection(e.to_string()))?;
17
18    Ok(rows
19        .into_iter()
20        .map(|(name, sql)| View::new(None, name.as_str(), sql.trim(), Some(DatabaseType::Postgresql)))
21        .collect())
22}