sql/
schema.rs

1mod column;
2mod constraint;
3mod index;
4mod table;
5mod r#type;
6
7pub use column::Column;
8pub use constraint::{Constraint, ForeignKey};
9pub use index::Index;
10pub use table::Table;
11pub use r#type::Type;
12
13use crate::migrate::{Migration, MigrationOptions, migrate};
14use anyhow::Result;
15
16/// Represents a SQL database schema.
17#[derive(Debug, Default, Clone)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct Schema {
20    pub tables: Vec<Table>,
21}
22
23impl Schema {
24    /// Calculate the migration necessary to move from `self: Schema` to the argument `desired: Schema`.
25    pub fn migrate_to(self, desired: Schema, options: &MigrationOptions) -> Result<Migration> {
26        migrate(self, desired, options)
27    }
28
29    /// Propagate the schema name to all tables.
30    pub fn name_schema(&mut self, schema: &str) {
31        for table in &mut self.tables {
32            table.schema = Some(schema.to_string());
33        }
34    }
35}