1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
mod config;
mod drivers;
mod error;
#[cfg(any(feature = "async", feature = "sync"))]
mod traits;
mod utils;
use chrono::{DateTime, Local};
use std::cmp::Ordering;
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
pub use error::{Error, WrapMigrationError};
#[cfg(feature = "async")]
pub use traits::r#async::{AsyncMigrate, AsyncQuery, AsyncTransaction};
#[cfg(feature = "sync")]
pub use traits::sync::{Migrate, Query, Transaction};
use utils::RE;
pub use utils::{file_match_re, find_migrations_filenames, MigrationType};
pub use config::{Config, ConfigDbType};
#[cfg(feature = "sync")]
pub use config::migrate_from_config;
#[cfg(feature = "async")]
pub use config::migrate_from_config_async;
#[derive(Clone, Debug)]
pub enum MigrationPrefix {
Versioned,
}
#[derive(Clone, Debug)]
pub struct Migration {
pub name: String,
pub version: usize,
pub prefix: MigrationPrefix,
pub sql: String,
}
impl Migration {
pub fn from_filename(name: &str, sql: &str) -> Result<Migration, Error> {
let captures = RE
.captures(name)
.filter(|caps| caps.len() == 4)
.ok_or(Error::InvalidName)?;
let version = captures[2].parse().map_err(|_| Error::InvalidVersion)?;
let name = (&captures[3]).into();
let prefix = match &captures[1] {
"V" => MigrationPrefix::Versioned,
_ => unreachable!(),
};
Ok(Migration {
name,
version,
sql: sql.into(),
prefix,
})
}
pub fn checksum(&self) -> u64 {
let mut hasher = DefaultHasher::new();
self.name.hash(&mut hasher);
self.version.hash(&mut hasher);
self.sql.hash(&mut hasher);
hasher.finish()
}
pub fn as_applied(&self) -> AppliedMigration {
AppliedMigration {
name: self.name.clone(),
version: self.version,
checksum: self.checksum().to_string(),
applied_on: Local::now(),
}
}
}
impl fmt::Display for Migration {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "V{}__{}", self.version, self.name)
}
}
impl Eq for Migration {}
impl PartialEq for Migration {
fn eq(&self, other: &Migration) -> bool {
self.version == other.version
&& self.name == other.name
&& self.checksum() == other.checksum()
}
}
impl Ord for Migration {
fn cmp(&self, other: &Migration) -> Ordering {
self.version.cmp(&other.version)
}
}
impl PartialOrd for Migration {
fn partial_cmp(&self, other: &Migration) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Clone, Debug)]
pub struct AppliedMigration {
pub name: String,
pub version: usize,
pub applied_on: DateTime<Local>,
pub checksum: String,
}
impl Eq for AppliedMigration {}
impl PartialEq for AppliedMigration {
fn eq(&self, other: &AppliedMigration) -> bool {
self.version == other.version && self.name == other.name && self.checksum == other.checksum
}
}
impl fmt::Display for AppliedMigration {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "V{}__{}", self.version, self.name)
}
}
pub struct Runner {
grouped: bool,
abort_divergent: bool,
abort_missing: bool,
migrations: Vec<Migration>,
}
impl Runner {
pub fn new(migrations: &[Migration]) -> Runner {
Runner {
grouped: false,
abort_divergent: true,
abort_missing: true,
migrations: migrations.to_vec(),
}
}
pub fn set_grouped(self, grouped: bool) -> Runner {
Runner { grouped, ..self }
}
pub fn set_abort_divergent(self, abort_divergent: bool) -> Runner {
Runner {
abort_divergent,
..self
}
}
pub fn set_abort_missing(self, abort_divergent: bool) -> Runner {
Runner {
abort_divergent,
..self
}
}
#[cfg(feature = "sync")]
pub fn run<'a, C>(&self, conn: &'a mut C) -> Result<(), Error>
where
C: Migrate,
{
Migrate::migrate(
conn,
&self.migrations,
self.abort_divergent,
self.abort_missing,
self.grouped,
)
}
#[cfg(feature = "async")]
pub async fn run_async<'a, C>(&self, conn: &'a mut C) -> Result<(), Error>
where
C: AsyncMigrate + Send,
{
AsyncMigrate::migrate(
conn,
&self.migrations,
self.abort_divergent,
self.abort_missing,
self.grouped,
)
.await
}
}