sqlx_migrate/
db.rs

1//! Database-specific items.
2
3#[cfg(feature = "postgres")]
4mod postgres;
5
6#[cfg(feature = "sqlite")]
7mod sqlite;
8
9use async_trait::async_trait;
10use sqlx::Connection;
11use std::{borrow::Cow, time::Duration};
12
13#[derive(Debug, Clone)]
14pub struct AppliedMigration<'m> {
15    pub version: u64,
16    pub name: Cow<'m, str>,
17    pub checksum: Cow<'m, [u8]>,
18    pub execution_time: Duration,
19}
20
21#[async_trait(?Send)]
22pub trait Migrations: Connection {
23    #[must_use]
24    async fn ensure_migrations_table(&mut self, table_name: &str) -> Result<(), sqlx::Error>;
25
26    // Should acquire a database lock so that only one migration process
27    // can run at a time. [`Migrate`] will call this function before applying
28    // any migrations.
29    #[must_use]
30    async fn lock(&mut self) -> Result<(), sqlx::Error>;
31
32    // Should release the lock. [`Migrate`] will call this function after all
33    // migrations have been run.
34    #[must_use]
35    async fn unlock(&mut self) -> Result<(), sqlx::Error>;
36
37    // Return the ordered list of applied migrations
38    #[must_use]
39    async fn list_migrations(
40        &mut self,
41        table_name: &str,
42    ) -> Result<Vec<AppliedMigration<'static>>, sqlx::Error>;
43
44    #[must_use]
45    async fn add_migration(
46        &mut self,
47        table_name: &str,
48        migration: AppliedMigration<'static>,
49    ) -> Result<(), sqlx::Error>;
50
51    #[must_use]
52    async fn remove_migration(&mut self, table_name: &str, version: u64)
53        -> Result<(), sqlx::Error>;
54
55    #[must_use]
56    async fn clear_migrations(&mut self, table_name: &str) -> Result<(), sqlx::Error>;
57}