pub struct Schema<'a> { /* private fields */ }Expand description
Emits DDL and runs it.
Implementations§
Source§impl<'a> Schema<'a>
impl<'a> Schema<'a>
pub fn new(db: &'a Database) -> Self
Sourcepub async fn create(
&self,
table: &str,
define: impl FnOnce(&mut Table),
) -> Result<()>
pub async fn create( &self, table: &str, define: impl FnOnce(&mut Table), ) -> Result<()>
Create a table.
Sourcepub async fn create_if_missing(
&self,
table: &str,
define: impl FnOnce(&mut Table),
) -> Result<()>
pub async fn create_if_missing( &self, table: &str, define: impl FnOnce(&mut Table), ) -> Result<()>
Create the table unless it exists, safely under concurrent callers.
For a table an application creates on the way up rather than in a
migration. Not has_table followed by create: two processes
booting at once both see no table, both create, and one fails.
create table if not exists is sent where the dialect has it — and the
error meaning “it exists” is accepted anyway, because PostgreSQL’s
if not exists is not serialised against a concurrent create: two
callers can both pass its check and the loser gets 42P07 relation already exists, or 23505 on pg_type_typname_nsp_index. That is
documented PostgreSQL behaviour, and it was measured here: ten of eleven
tests failed the first time a suite booted against an empty database.
Indexes that already exist are accepted the same way.
Sourcepub async fn alter(
&self,
table: &str,
define: impl FnOnce(&mut Table),
) -> Result<()>
pub async fn alter( &self, table: &str, define: impl FnOnce(&mut Table), ) -> Result<()>
Add or drop columns on an existing table.
pub async fn drop(&self, table: &str) -> Result<()>
pub async fn rename(&self, from: &str, to: &str) -> Result<()>
Sourcepub async fn has_table(&self, table: &str) -> Result<bool>
pub async fn has_table(&self, table: &str) -> Result<bool>
Whether a table exists — how the migration runner decides what to do.