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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
};
use async_trait::async_trait;
use sqlx::migrate::{
AppliedMigration, Migrate, MigrateError, Migration, MigrationSource, Migrator,
};
use crate::error::ValidateError;
#[async_trait(?Send)]
pub trait Validate {
/// Validate previously applied migrations against the migration source.
/// Depending on the migration source this can be used to check if all migrations
/// for the current version of the application have been applied.
/// Use [`Validator::from_migrator`] to use the migrations available during compilation.
///
/// # Examples
///
/// ```rust,no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # sqlx_rt::block_on(async move {
/// # use sqlx_migrate_validate::Validator;
/// // Use migrations that were in a local folder during build: ./tests/migrations-1
/// let v = Validator::from_migrator(sqlx::migrate!("./tests/migrations-1"));
///
/// // Create a connection pool
/// let pool = sqlx_core::sqlite::SqlitePoolOptions::new().connect("sqlite::memory:").await?;
/// let mut conn = pool.acquire().await?;
///
/// // Validate the migrations
/// v.validate(&mut *conn).await?;
/// # Ok(())
/// # })
/// # }
/// ```
async fn validate<'c, C>(&self, conn: &mut C) -> Result<(), ValidateError>
where
C: Migrate;
}
/// Validate previously applied migrations against the migration source.
/// Depending on the migration source this can be used to check if all migrations
/// for the current version of the application have been applied.
/// Use [`Validator::from_migrator`] to use the migrations available during compilation.
///
/// # Examples
///
/// ```rust,no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # sqlx_rt::block_on(async move {
/// # use sqlx_migrate_validate::Validator;
/// // Use migrations that were in a local folder during build: ./tests/migrations-1
/// let v = Validator::from_migrator(sqlx::migrate!("./tests/migrations-1"));
///
/// // Create a connection pool
/// let pool = sqlx_core::sqlite::SqlitePoolOptions::new().connect("sqlite::memory:").await?;
/// let mut conn = pool.acquire().await?;
///
/// // Validate the migrations
/// v.validate(&mut *conn).await?;
/// # Ok(())
/// # })
/// # }
/// ```
#[derive(Debug)]
pub struct Validator {
pub migrations: Cow<'static, [Migration]>,
pub ignore_missing: bool,
pub locking: bool,
}
impl Validator {
/// Creates a new instance with the given source. Please note that the source
/// is resolved at runtime and not at compile time.
/// You can use [`Validator::from<sqlx::Migrator>`] and the [`sqlx::migrate!`] macro
/// to embed the migrations into the binary during compile time.
///
/// # Examples
///
/// ```rust,no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # sqlx_rt::block_on(async move {
/// # use sqlx_migrate_validate::Validator;
/// use std::path::Path;
///
/// // Read migrations from a local folder: ./tests/migrations-1
/// let v = Validator::new(Path::new("./tests/migrations-1")).await?;
///
/// // Create a connection pool
/// let pool = sqlx::PgPool::connect("postgres://postgres:postgres@localhost:5432/postgres").await?;
/// let mut conn = pool.acquire().await?;
///
/// // Validate the migrations
/// v.validate(&mut *conn).await?;
/// # Ok(())
/// # })
/// # }
/// ```
///
/// See [MigrationSource] for details on structure of the `./tests/migrations-1` directory.
pub async fn new<'s, S>(source: S) -> Result<Self, MigrateError>
where
S: MigrationSource<'s>,
{
Ok(Self {
migrations: Cow::Owned(source.resolve().await.map_err(MigrateError::Source)?),
ignore_missing: false,
locking: true,
})
}
/// Creates a new instance with the migrations from the given migrator.
/// You can combine this with the [`sqlx::migrate!`] macro
/// to embed the migrations into the binary during compile time.
///
/// # Examples
///
/// ```rust,no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # sqlx_rt::block_on(async move {
/// # use sqlx_migrate_validate::Validator;
/// // Use migrations that were in a local folder during build: ./tests/migrations-1
/// let v = Validator::from_migrator(sqlx::migrate!("./tests/migrations-1"));
///
/// // Create a connection pool
/// let pool = sqlx::PgPool::connect("postgres://postgres:postgres@localhost:5432/postgres").await?;
/// let mut conn = pool.acquire().await?;
///
/// // Validate the migrations
/// v.validate(&mut *conn).await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub fn from_migrator(migrator: Migrator) -> Self {
Self {
migrations: migrator.migrations.clone(),
ignore_missing: migrator.ignore_missing,
locking: migrator.locking,
}
}
pub async fn validate<'c, C>(&self, conn: &mut C) -> Result<(), ValidateError>
where
C: Migrate,
{
// lock the migrator to prevent other migrators from running
if self.locking {
conn.lock().await?;
}
let version = conn.dirty_version().await?;
if let Some(version) = version {
return Err(ValidateError::MigrateError(MigrateError::Dirty(version)));
}
let applied_migrations = conn.list_applied_migrations().await?;
validate_applied_migrations(&applied_migrations, self)?;
let applied_migrations: HashMap<_, _> = applied_migrations
.into_iter()
.map(|m| (m.version, m))
.collect();
for migration in self.migrations.iter() {
if migration.migration_type.is_down_migration() {
continue;
}
match applied_migrations.get(&migration.version) {
Some(applied_migration) => {
if migration.checksum != applied_migration.checksum {
return Err(ValidateError::MigrateError(MigrateError::VersionMismatch(
migration.version,
)));
}
}
None => {
return Err(ValidateError::VersionNotApplied(migration.version));
// conn.apply(migration).await?;
}
}
}
// unlock the migrator to allow other migrators to run
// but do nothing as we already migrated
if self.locking {
conn.unlock().await?;
}
Ok(())
}
}
impl From<&Migrator> for Validator {
fn from(migrator: &Migrator) -> Self {
Self {
migrations: migrator.migrations.clone(),
ignore_missing: migrator.ignore_missing,
locking: migrator.locking,
}
}
}
impl From<Migrator> for Validator {
fn from(migrator: Migrator) -> Self {
Self::from(&migrator)
}
}
#[async_trait(?Send)]
impl Validate for Migrator {
async fn validate<'c, C>(&self, conn: &mut C) -> Result<(), ValidateError>
where
C: Migrate,
{
Validator::from(self).validate(conn).await
}
}
fn validate_applied_migrations(
applied_migrations: &[AppliedMigration],
migrator: &Validator,
) -> Result<(), MigrateError> {
if migrator.ignore_missing {
return Ok(());
}
let migrations: HashSet<_> = migrator.migrations.iter().map(|m| m.version).collect();
for applied_migration in applied_migrations {
if !migrations.contains(&applied_migration.version) {
return Err(MigrateError::VersionMissing(applied_migration.version));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use sqlx::migrate::MigrationType;
use super::*;
#[test]
fn validate_applied_migrations_returns_ok_when_nothing_was_applied() {
let applied_migrations = vec![];
let mut validator = Validator {
migrations: Cow::Owned(vec![]),
ignore_missing: false,
locking: true,
};
assert!(validate_applied_migrations(&applied_migrations, &validator).is_ok());
validator.ignore_missing = true;
assert!(validate_applied_migrations(&applied_migrations, &validator).is_ok());
}
#[test]
fn validate_applied_migrations_returns_err_when_applied_migration_not_in_source() {
let applied_migrations = vec![AppliedMigration {
version: 1,
// only the version is relevant for this method
checksum: Cow::Owned(vec![]),
}];
let validator = Validator {
migrations: Cow::Owned(vec![]),
ignore_missing: false,
locking: true,
};
match validate_applied_migrations(&applied_migrations, &validator) {
Err(MigrateError::VersionMissing(i)) => assert_eq!(i, 1),
_ => panic!("Unexpected error"),
}
}
#[test]
fn validate_applied_migrations_returns_ok_when_applied_migration_in_source() {
let applied_migrations = vec![AppliedMigration {
version: 1,
// only the version is relevant for this method
checksum: Cow::Owned(vec![]),
}];
let validator = Validator {
migrations: Cow::Owned(vec![Migration {
version: 1,
// only the version is relevant for this method
migration_type: MigrationType::ReversibleUp,
checksum: Cow::Owned(vec![]),
sql: Cow::Owned("".to_string()),
description: Cow::Owned("".to_string()),
}]),
ignore_missing: false,
locking: true,
};
match validate_applied_migrations(&applied_migrations, &validator) {
Ok(_) => {}
_ => panic!("Unexpected error"),
}
}
}