sqlx_core_oldapi/migrate/migrate.rs
1use crate::error::Result;
2use crate::migrate::{AppliedMigration, MigrateResult, 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<()>>;
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>>;
14
15 // drop database in url
16 // uses a maintenance database depending on driver
17 fn drop_database(url: &str) -> BoxFuture<'_, Result<()>>;
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<()>>;
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>>>;
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)>>>;
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, MigrateResult<()>>;
42
43 // Return the ordered list of applied migrations
44 fn list_applied_migrations(&mut self) -> BoxFuture<'_, Result<Vec<AppliedMigration>>>;
45
46 // Should acquire a database lock so that only one migration process
47 // can run at a time. [`Migrate`] will call this function before applying
48 // any migrations.
49 fn lock(&mut self) -> BoxFuture<'_, Result<()>>;
50
51 // Should release the lock. [`Migrate`] will call this function after all
52 // migrations have been run.
53 fn unlock(&mut self) -> BoxFuture<'_, Result<()>>;
54
55 // run SQL from migration in a DDL transaction
56 // insert new row to [_migrations] table on completion (success or failure)
57 // returns the time taking to run the migration SQL
58 fn apply<'e: 'm, 'm>(&'e mut self, migration: &'m Migration)
59 -> BoxFuture<'m, Result<Duration>>;
60
61 // run a revert SQL from migration in a DDL transaction
62 // deletes the row in [_migrations] table with specified migration version on completion (success or failure)
63 // returns the time taking to run the migration SQL
64 fn revert<'e: 'm, 'm>(
65 &'e mut self,
66 migration: &'m Migration,
67 ) -> BoxFuture<'m, Result<Duration>>;
68}