#![cfg_attr(
feature = "sqlite",
doc = r#"
To create own implement migration trait for type
### Example
```rust,no_run
use sqlx_migrator::error::Error;
use sqlx_migrator::migration::Migration;
use sqlx_migrator::operation::Operation;
use sqlx::Sqlite;
struct ExampleMigration;
impl Migration<Sqlite> for ExampleMigration {
fn app(&self) -> &str {
"example"
}
fn name(&self) -> &str {
"first_migration"
}
fn parents(&self) -> Vec<Box<dyn Migration<Sqlite>>> {
vec![]
}
fn operations(&self) -> Vec<Box<dyn Operation<Sqlite>>> {
vec![]
}
fn replaces(&self) -> Vec<Box<dyn Migration<Sqlite>>> {
vec![]
}
fn run_before(&self) -> Vec<Box<dyn Migration<Sqlite>>> {
vec![]
}
fn is_atomic(&self) -> bool {
true
}
fn is_virtual(&self) -> bool {
false
}
}
```
"#
)]
use std::hash::Hash;
use crate::operation::Operation;
pub trait Migration<DB>: Send + Sync {
fn app(&self) -> &str;
fn name(&self) -> &str;
fn parents(&self) -> Vec<Box<dyn Migration<DB>>>;
fn operations(&self) -> Vec<Box<dyn Operation<DB>>>;
fn replaces(&self) -> Vec<Box<dyn Migration<DB>>> {
vec![]
}
fn run_before(&self) -> Vec<Box<dyn Migration<DB>>> {
vec![]
}
fn is_atomic(&self) -> bool {
true
}
fn is_virtual(&self) -> bool {
false
}
}
impl<DB> PartialEq for dyn Migration<DB> {
fn eq(&self, other: &Self) -> bool {
self.app() == other.app() && self.name() == other.name()
}
}
impl<DB> Eq for dyn Migration<DB> {}
impl<DB> Hash for dyn Migration<DB> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.app().hash(state);
self.name().hash(state);
}
}
impl<DB, A, N> Migration<DB> for (A, N)
where
A: AsRef<str> + Send + Sync,
N: AsRef<str> + Send + Sync,
{
fn app(&self) -> &str {
self.0.as_ref()
}
fn name(&self) -> &str {
self.1.as_ref()
}
fn parents(&self) -> Vec<Box<dyn Migration<DB>>> {
vec![]
}
fn operations(&self) -> Vec<Box<dyn Operation<DB>>> {
vec![]
}
fn is_virtual(&self) -> bool {
true
}
}
#[derive(sqlx::FromRow, Clone)]
pub struct AppliedMigrationSqlRow {
id: i32,
app: String,
name: String,
applied_time: String,
}
impl AppliedMigrationSqlRow {
#[cfg(test)]
pub(crate) fn new(id: i32, app: &str, name: &str) -> Self {
Self {
id,
app: app.to_string(),
name: name.to_string(),
applied_time: String::new(),
}
}
}
impl AppliedMigrationSqlRow {
#[must_use]
pub fn id(&self) -> i32 {
self.id
}
#[must_use]
pub fn app(&self) -> &str {
&self.app
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn applied_time(&self) -> &str {
&self.applied_time
}
}
impl<DB> PartialEq<Box<dyn Migration<DB>>> for AppliedMigrationSqlRow {
fn eq(&self, other: &Box<dyn Migration<DB>>) -> bool {
self.app == other.app() && self.name == other.name()
}
}