sql/
schema.rs

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