rustyroad 1.8.1

Rusty Road is a framework written in Rust that is based on Ruby on Rails. It is designed to provide the familiar conventions and ease of use of Ruby on Rails, while also taking advantage of the performance and efficiency of Rust.
Documentation
//! Publishing a schema version as views over the physical tables.

use crate::database::migrations::CustomMigrationError;
use crate::database::versions::{exec, introspect, view};
use crate::database::DatabaseConnection;

/// Returns `true` when the backend supports versioned schemas.
///
/// Versioned schemas rely on Postgres schema namespaces and views; MySQL and
/// SQLite have no equivalent, so their migrations run without version publishing.
pub fn supports_versions(connection: &DatabaseConnection) -> bool {
    matches!(connection, DatabaseConnection::Pg(_))
}

/// Creates the versioned schema and its views from the current physical schema.
///
/// `renames` let the new version expose a renamed column straight away, while the
/// physical column still carries its original name until completion.
pub(super) async fn publish(
    connection: &DatabaseConnection,
    schema: &str,
    version: &str,
    renames: &[(String, String, String)],
) -> Result<(), CustomMigrationError> {
    if !supports_versions(connection) {
        return Ok(());
    }

    let snapshot = introspect::read_schema(connection, schema, renames).await?;
    if snapshot.tables.is_empty() {
        // Nothing to project yet.
        return Ok(());
    }

    let major = introspect::major_version(connection).await;
    for statement in view::create_version(schema, version, &snapshot, major) {
        exec::run(connection, &statement, &[]).await?;
    }
    Ok(())
}

/// Drops the schema serving `version`, when the backend has one.
pub(super) async fn unpublish(
    connection: &DatabaseConnection,
    schema: &str,
    version: &str,
) -> Result<(), CustomMigrationError> {
    if !supports_versions(connection) {
        return Ok(());
    }
    exec::run(connection, &view::drop_schema(schema, version), &[]).await
}