sqlx_core_guts/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
20// 'e = Executor
21pub trait Migrate {
22 // ensure migrations table exists
23 // will create or migrate it if needed
24 fn ensure_migrations_table(&mut self) -> BoxFuture<'_, Result<(), MigrateError>>;
25
26 // Return the version on which the database is dirty or None otherwise.
27 // "dirty" means there is a partially applied migration that failed.
28 fn dirty_version(&mut self) -> BoxFuture<'_, Result<Option<i64>, MigrateError>>;
29
30 // Return the current version and if the database is "dirty".
31 // "dirty" means there is a partially applied migration that failed.
32 #[deprecated]
33 fn version(&mut self) -> BoxFuture<'_, Result<Option<(i64, bool)>, MigrateError>>;
34
35 // validate the migration
36 // checks that it does exist on the database and that the checksum matches
37 #[deprecated]
38 fn validate<'e: 'm, 'm>(
39 &'e mut self,
40 migration: &'m Migration,
41 ) -> BoxFuture<'m, Result<(), MigrateError>>;
42
43 // Return the ordered list of applied migrations
44 fn list_applied_migrations(
45 &mut self,
46 ) -> BoxFuture<'_, Result<Vec<AppliedMigration>, MigrateError>>;
47
48 // Should acquire a database lock so that only one migration process
49 // can run at a time. [`Migrate`] will call this function before applying
50 // any migrations.
51 fn lock(&mut self) -> BoxFuture<'_, Result<(), MigrateError>>;
52
53 // Should release the lock. [`Migrate`] will call this function after all
54 // migrations have been run.
55 fn unlock(&mut self) -> BoxFuture<'_, Result<(), MigrateError>>;
56
57 // run SQL from migration in a DDL transaction
58 // insert new row to [_migrations] table on completion (success or failure)
59 // returns the time taking to run the migration SQL
60 fn apply<'e: 'm, 'm>(
61 &'e mut self,
62 migration: &'m Migration,
63 ) -> BoxFuture<'m, Result<Duration, MigrateError>>;
64
65 // run a revert SQL from migration in a DDL transaction
66 // deletes the row in [_migrations] table with specified migration version on completion (success or failure)
67 // returns the time taking to run the migration SQL
68 fn revert<'e: 'm, 'm>(
69 &'e mut self,
70 migration: &'m Migration,
71 ) -> BoxFuture<'m, Result<Duration, MigrateError>>;
72}