sqlx_core/migrate/
migrate.rs

1use crate::error::Error;
2use crate::migrate::{AppliedMigration, MigrateError, Migration};
3use futures_core::future::BoxFuture;
4use std::time::Duration;
5
6pub trait MigrateDatabase {
7    // create database in url
8    // uses a maintenance database depending on driver
9    fn create_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
10
11    // check if the database in url exists
12    // uses a maintenance database depending on driver
13    fn database_exists(url: &str) -> BoxFuture<'_, Result<bool, Error>>;
14
15    // drop database in url
16    // uses a maintenance database depending on driver
17    fn drop_database(url: &str) -> BoxFuture<'_, Result<(), Error>>;
18
19    // force drop database in url
20    // uses a maintenance database depending on driver
21    fn force_drop_database(_url: &str) -> BoxFuture<'_, Result<(), Error>> {
22        Box::pin(async { Err(MigrateError::ForceNotSupported)? })
23    }
24}
25
26// 'e = Executor
27pub trait Migrate {
28    // ensure migrations table exists
29    // will create or migrate it if needed
30    fn ensure_migrations_table(&mut self) -> BoxFuture<'_, Result<(), MigrateError>>;
31
32    // Return the version on which the database is dirty or None otherwise.
33    // "dirty" means there is a partially applied migration that failed.
34    fn dirty_version(&mut self) -> BoxFuture<'_, Result<Option<i64>, MigrateError>>;
35
36    // Return the ordered list of applied migrations
37    fn list_applied_migrations(
38        &mut self,
39    ) -> BoxFuture<'_, Result<Vec<AppliedMigration>, MigrateError>>;
40
41    // Should acquire a database lock so that only one migration process
42    // can run at a time. [`Migrate`] will call this function before applying
43    // any migrations.
44    fn lock(&mut self) -> BoxFuture<'_, Result<(), MigrateError>>;
45
46    // Should release the lock. [`Migrate`] will call this function after all
47    // migrations have been run.
48    fn unlock(&mut self) -> BoxFuture<'_, Result<(), MigrateError>>;
49
50    // run SQL from migration in a DDL transaction
51    // insert new row to [_migrations] table on completion (success or failure)
52    // returns the time taking to run the migration SQL
53    fn apply<'e: 'm, 'm>(
54        &'e mut self,
55        migration: &'m Migration,
56    ) -> BoxFuture<'m, Result<Duration, MigrateError>>;
57
58    // run a revert SQL from migration in a DDL transaction
59    // deletes the row in [_migrations] table with specified migration version on completion (success or failure)
60    // returns the time taking to run the migration SQL
61    fn revert<'e: 'm, 'm>(
62        &'e mut self,
63        migration: &'m Migration,
64    ) -> BoxFuture<'m, Result<Duration, MigrateError>>;
65}