| 1 |
//! Representation of migrations |
| 2 |
|
| 3 |
use crate::backend::Backend; |
| 4 |
use crate::connection::{BoxableConnection, Connection}; |
| 5 |
use crate::deserialize::{FromSql, FromSqlRow}; |
| 6 |
use crate::expression::AsExpression; |
| 7 |
use crate::result::QueryResult; |
| 8 |
use crate::serialize::ToSql; |
| 9 |
use crate::sql_types::Text; |
| 10 |
use std::borrow::Cow; |
| 11 |
use std::error::Error; |
| 12 |
use std::fmt::Display; |
| 13 |
|
| 14 |
/// A specialized result type representing the result of |
| 15 |
/// a migration operation |
| 16 |
pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>; |
| 17 |
|
| 18 |
/// A migration version identifier |
| 19 |
/// |
| 20 |
/// This is used by the migration harness to place migrations |
| 21 |
/// in order, therefore two different instances of this type |
| 22 |
/// must be sortable |
| 23 |
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, FromSqlRow, AsExpression)] |
| 24 |
#[diesel(sql_type = Text)] |
| 25 |
pub struct MigrationVersion<'a>(Cow<'a, str>); |
| 26 |
|
| 27 |
impl MigrationVersion<'_> { |
| 28 |
/// Convert the current migration version into |
| 29 |
/// an owned variant with static life time |
| 30 |
pub fn as_owned(&self) -> MigrationVersion<'static> { |
| 31 |
MigrationVersion(Cow::Owned(self.0.as_ref().to_owned())) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl<DB> FromSql<Text, DB> for MigrationVersion<'_> |
| 36 |
where |
| 37 |
String: FromSql<Text, DB>, |
| 38 |
DB: Backend, |
| 39 |
{ |
| 40 |
fn from_sql(bytes: DB::RawValue<'_>) -> crate::deserialize::Result<Self> { |
| 41 |
let s = String::from_sql(bytes)?; |
| 42 |
Ok(Self(Cow::Owned(s))) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl<'a, DB> ToSql<Text, DB> for MigrationVersion<'a> |
| 47 |
where |
| 48 |
Cow<'a, str>: ToSql<Text, DB>, |
| 49 |
DB: Backend, |
| 50 |
{ |
| 51 |
fn to_sql<'b>( |
| 52 |
&'b self, |
| 53 |
out: &mut crate::serialize::Output<'b, '_, DB>, |
| 54 |
) -> crate::serialize::Result { |
| 55 |
self.0.to_sql(out) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl From<String> for MigrationVersion<'_> { |
| 60 |
fn from(s: String) -> Self { |
| 61 |
MigrationVersion(Cow::Owned(s)) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl<'a> From<&'a str> for MigrationVersion<'a> { |
| 66 |
fn from(s: &'a str) -> Self { |
| 67 |
MigrationVersion(Cow::Borrowed(s)) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl<'a> From<&'a String> for MigrationVersion<'a> { |
| 72 |
fn from(s: &'a String) -> Self { |
| 73 |
MigrationVersion(Cow::Borrowed(s)) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl Display for MigrationVersion<'_> { |
| 78 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 79 |
f.write_str(self.0.as_ref()) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// Represents the name of a migration |
| 84 |
/// |
| 85 |
/// Users should threat this as `impl Display` type, |
| 86 |
/// for implementors of custom migration types |
| 87 |
/// this opens the possibility to roll out their own versioning |
| 88 |
/// schema. |
| 89 |
pub trait MigrationName: Display { |
| 90 |
/// The version corresponding to the current migration name |
| 91 |
fn version(&self) -> MigrationVersion<'_>; |
| 92 |
} |
| 93 |
|
| 94 |
/// Represents a migration that interacts with diesel |
| 95 |
pub trait Migration<DB: Backend> { |
| 96 |
/// Apply this migration |
| 97 |
fn run(&self, conn: &mut dyn BoxableConnection<DB>) -> Result<()>; |
| 98 |
|
| 99 |
/// Revert this migration |
| 100 |
fn revert(&self, conn: &mut dyn BoxableConnection<DB>) -> Result<()>; |
| 101 |
|
| 102 |
/// Get a the attached metadata for this migration |
| 103 |
fn metadata(&self) -> &dyn MigrationMetadata; |
| 104 |
|
| 105 |
/// Get the name of the current migration |
| 106 |
/// |
| 107 |
/// The provided name is used by migration harness |
| 108 |
/// to get the version of a migration and to |
| 109 |
/// as something to that is displayed and allows |
| 110 |
/// user to identify a specific migration |
| 111 |
fn name(&self) -> &dyn MigrationName; |
| 112 |
} |
| 113 |
|
| 114 |
/// This trait is designed to customize the behaviour |
| 115 |
/// of the default migration harness of diesel |
| 116 |
/// |
| 117 |
/// Any new customization option will be added |
| 118 |
/// as new function here. Each new function |
| 119 |
/// will have a default implementation |
| 120 |
/// returning the old a value corresponding |
| 121 |
/// to the old uncustomized behaviour |
| 122 |
pub trait MigrationMetadata { |
| 123 |
/// Whether the current migration is executed in a transaction or not |
| 124 |
/// |
| 125 |
/// By default each migration is executed in a own transaction, but |
| 126 |
/// certain operations (like creating an index on an existing column) |
| 127 |
/// requires running the migration without transaction. |
| 128 |
/// |
| 129 |
/// By default this function returns true |
| 130 |
fn run_in_transaction(&self) -> bool { |
| 131 |
true |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/// A migration source is an entity that can be used |
| 136 |
/// to receive a number of migrations from. |
| 137 |
pub trait MigrationSource<DB: Backend> { |
| 138 |
/// Get a list of migrations associated with this |
| 139 |
/// migration source. |
| 140 |
fn migrations(&self) -> Result<Vec<Box<dyn Migration<DB>>>>; |
| 141 |
} |
| 142 |
|
| 143 |
impl<DB: Backend> Migration<DB> for Box<dyn Migration<DB> + '_> { |
| 144 |
fn run(&self, conn: &mut dyn BoxableConnection<DB>) -> Result<()> { |
| 145 |
(**self).run(conn) |
| 146 |
} |
| 147 |
|
| 148 |
fn revert(&self, conn: &mut dyn BoxableConnection<DB>) -> Result<()> { |
| 149 |
(**self).revert(conn) |
| 150 |
} |
| 151 |
|
| 152 |
fn metadata(&self) -> &dyn MigrationMetadata { |
| 153 |
(**self).metadata() |
| 154 |
} |
| 155 |
|
| 156 |
fn name(&self) -> &dyn MigrationName { |
| 157 |
(**self).name() |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
impl<DB: Backend> Migration<DB> for &dyn Migration<DB> { |
| 162 |
fn run(&self, conn: &mut dyn BoxableConnection<DB>) -> Result<()> { |
| 163 |
(**self).run(conn) |
| 164 |
} |
| 165 |
|
| 166 |
fn revert(&self, conn: &mut dyn BoxableConnection<DB>) -> Result<()> { |
| 167 |
(**self).revert(conn) |
| 168 |
} |
| 169 |
|
| 170 |
fn metadata(&self) -> &dyn MigrationMetadata { |
| 171 |
(**self).metadata() |
| 172 |
} |
| 173 |
|
| 174 |
fn name(&self) -> &dyn MigrationName { |
| 175 |
(**self).name() |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/// Create table statement for the `__diesel_schema_migrations` used |
| 180 |
/// used by the postgresql, sqlite and mysql backend |
| 181 |
pub const CREATE_MIGRATIONS_TABLE: &str = include_str!("setup_migration_table.sql"); |
| 182 |
|
| 183 |
/// A trait indicating that a connection could be used to manage migrations |
| 184 |
/// |
| 185 |
/// Only custom backend implementations need to think about this trait |
| 186 |
pub trait MigrationConnection: Connection { |
| 187 |
/// Setup the following table: |
| 188 |
/// |
| 189 |
/// ```rust |
| 190 |
/// diesel::table! { |
| 191 |
/// __diesel_schema_migrations(version) { |
| 192 |
/// version -> Text, |
| 193 |
/// /// defaults to `CURRENT_TIMESTAMP` |
| 194 |
/// run_on -> Timestamp, |
| 195 |
/// } |
| 196 |
/// } |
| 197 |
/// ``` |
| 198 |
fn setup(&mut self) -> QueryResult<usize>; |
| 199 |
} |
| 200 |
|
| 201 |
#[cfg(feature = "postgres")] |
| 202 |
impl MigrationConnection for crate::pg::PgConnection { |
| 203 |
fn setup(&mut self) -> QueryResult<usize> { |
| 204 |
use crate::RunQueryDsl; |
| 205 |
crate::sql_query(CREATE_MIGRATIONS_TABLE).execute(self) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
#[cfg(feature = "mysql")] |
| 210 |
impl MigrationConnection for crate::mysql::MysqlConnection { |
| 211 |
fn setup(&mut self) -> QueryResult<usize> { |
| 212 |
use crate::RunQueryDsl; |
| 213 |
crate::sql_query(CREATE_MIGRATIONS_TABLE).execute(self) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
#[cfg(feature = "sqlite")] |
| 218 |
impl MigrationConnection for crate::sqlite::SqliteConnection { |
| 219 |
fn setup(&mut self) -> QueryResult<usize> { |
| 220 |
use crate::RunQueryDsl; |
| 221 |
crate::sql_query(CREATE_MIGRATIONS_TABLE).execute(self) |
| 222 |
} |
| 223 |
} |