-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathutil.rs
242 lines (210 loc) · 8.41 KB
/
util.rs
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
use crate::error::{Error, Kind};
use crate::runner::Type;
use crate::Migration;
use regex::Regex;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use walkdir::{DirEntry, WalkDir};
const STEM_RE: &'static str = r"^([U|V])(\d+(?:\.\d+)?)__(\w+)";
/// Matches the stem of a migration file.
fn file_stem_re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(STEM_RE).unwrap())
}
/// Matches the stem + extension of a SQL migration file.
fn file_re_sql() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new([STEM_RE, r"\.sql$"].concat().as_str()).unwrap())
}
/// Matches the stem + extension of any migration file.
fn file_re_all() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new([STEM_RE, r"\.(rs|sql)$"].concat().as_str()).unwrap())
}
/// enum containing the migration types used to search for migrations
/// either just .sql files or both .sql and .rs
pub enum MigrationType {
All,
Sql,
}
impl MigrationType {
fn file_match_re(&self) -> &'static Regex {
match self {
MigrationType::All => file_re_all(),
MigrationType::Sql => file_re_sql(),
}
}
}
/// Parse a migration filename stem into a prefix, version, and name.
pub fn parse_migration_name(name: &str) -> Result<(Type, i32, String), Error> {
let captures = file_stem_re()
.captures(name)
.filter(|caps| caps.len() == 4)
.ok_or_else(|| Error::new(Kind::InvalidName, None))?;
let version: i32 = captures[2]
.parse()
.map_err(|_| Error::new(Kind::InvalidVersion, None))?;
let name: String = (&captures[3]).into();
let prefix = match &captures[1] {
"V" => Type::Versioned,
"U" => Type::Unversioned,
_ => unreachable!(),
};
Ok((prefix, version, name))
}
/// find migrations on file system recursively across directories given a location and
/// [MigrationType]
pub fn find_migration_files(
location: impl AsRef<Path>,
migration_type: MigrationType,
) -> Result<impl Iterator<Item = PathBuf>, Error> {
let location: &Path = location.as_ref();
let location = location.canonicalize().map_err(|err| {
Error::new(
Kind::InvalidMigrationPath(location.to_path_buf(), err),
None,
)
})?;
let re = migration_type.file_match_re();
let file_paths = WalkDir::new(location)
.into_iter()
.filter_map(Result::ok)
.map(DirEntry::into_path)
// filter by migration file regex
.filter(
move |entry| match entry.file_name().and_then(OsStr::to_str) {
Some(_) if entry.is_dir() => false,
Some(file_name) if re.is_match(file_name) => true,
Some(file_name) => {
log::warn!(
"File \"{}\" does not adhere to the migration naming convention. Migrations must be named in the format [U|V]{{1}}__{{2}}.sql or [U|V]{{1}}__{{2}}.rs, where {{1}} represents the migration version and {{2}} the name.",
file_name
);
false
}
None => false,
},
);
Ok(file_paths)
}
/// Loads SQL migrations from a path. This enables dynamic migration discovery, as opposed to
/// embedding. The resulting collection is ordered by version.
pub fn load_sql_migrations(location: impl AsRef<Path>) -> Result<Vec<Migration>, Error> {
let migration_files = find_migration_files(location, MigrationType::Sql)?;
let mut migrations = vec![];
for path in migration_files {
let sql = std::fs::read_to_string(path.as_path()).map_err(|e| {
let path = path.to_owned();
let kind = match e.kind() {
std::io::ErrorKind::NotFound => Kind::InvalidMigrationPath(path, e),
_ => Kind::InvalidMigrationFile(path, e),
};
Error::new(kind, None)
})?;
//safe to call unwrap as find_migration_filenames returns canonical paths
let filename = path
.file_stem()
.and_then(|file| file.to_os_string().into_string().ok())
.unwrap();
let migration = Migration::unapplied(&filename, &sql)?;
migrations.push(migration);
}
migrations.sort();
Ok(migrations)
}
#[cfg(test)]
mod tests {
use super::{find_migration_files, load_sql_migrations, MigrationType};
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
#[test]
fn finds_mod_migrations() {
let tmp_dir = TempDir::new().unwrap();
let migrations_dir = tmp_dir.path().join("migrations");
fs::create_dir(&migrations_dir).unwrap();
let sql1 = migrations_dir.join("V1__first.rs");
fs::File::create(&sql1).unwrap();
let sql2 = migrations_dir.join("V2__second.rs");
fs::File::create(&sql2).unwrap();
let mut mods: Vec<PathBuf> = find_migration_files(migrations_dir, MigrationType::All)
.unwrap()
.collect();
mods.sort();
assert_eq!(sql1.canonicalize().unwrap(), mods[0]);
assert_eq!(sql2.canonicalize().unwrap(), mods[1]);
}
#[test]
fn ignores_mod_files_without_migration_regex_match() {
let tmp_dir = TempDir::new().unwrap();
let migrations_dir = tmp_dir.path().join("migrations");
fs::create_dir(&migrations_dir).unwrap();
let sql1 = migrations_dir.join("V1first.rs");
fs::File::create(sql1).unwrap();
let sql2 = migrations_dir.join("V2second.rs");
fs::File::create(sql2).unwrap();
let mut mods = find_migration_files(migrations_dir, MigrationType::All).unwrap();
assert!(mods.next().is_none());
}
#[test]
fn finds_sql_migrations() {
let tmp_dir = TempDir::new().unwrap();
let migrations_dir = tmp_dir.path().join("migrations");
fs::create_dir(&migrations_dir).unwrap();
let sql1 = migrations_dir.join("V1__first.sql");
fs::File::create(&sql1).unwrap();
let sql2 = migrations_dir.join("V2__second.sql");
fs::File::create(&sql2).unwrap();
let mut mods: Vec<PathBuf> = find_migration_files(migrations_dir, MigrationType::All)
.unwrap()
.collect();
mods.sort();
assert_eq!(sql1.canonicalize().unwrap(), mods[0]);
assert_eq!(sql2.canonicalize().unwrap(), mods[1]);
}
#[test]
fn finds_unversioned_migrations() {
let tmp_dir = TempDir::new().unwrap();
let migrations_dir = tmp_dir.path().join("migrations");
fs::create_dir(&migrations_dir).unwrap();
let sql1 = migrations_dir.join("U1__first.sql");
fs::File::create(&sql1).unwrap();
let sql2 = migrations_dir.join("U2__second.sql");
fs::File::create(&sql2).unwrap();
let mut mods: Vec<PathBuf> = find_migration_files(migrations_dir, MigrationType::All)
.unwrap()
.collect();
mods.sort();
assert_eq!(sql1.canonicalize().unwrap(), mods[0]);
assert_eq!(sql2.canonicalize().unwrap(), mods[1]);
}
#[test]
fn ignores_sql_files_without_migration_regex_match() {
let tmp_dir = TempDir::new().unwrap();
let migrations_dir = tmp_dir.path().join("migrations");
fs::create_dir(&migrations_dir).unwrap();
let sql1 = migrations_dir.join("V1first.sql");
fs::File::create(sql1).unwrap();
let sql2 = migrations_dir.join("V2second.sql");
fs::File::create(sql2).unwrap();
let mut mods = find_migration_files(migrations_dir, MigrationType::All).unwrap();
assert!(mods.next().is_none());
}
#[test]
fn loads_migrations_from_path() {
let tmp_dir = TempDir::new().unwrap();
let migrations_dir = tmp_dir.path().join("migrations");
fs::create_dir(&migrations_dir).unwrap();
let sql1 = migrations_dir.join("V1__first.sql");
fs::File::create(&sql1).unwrap();
let sql2 = migrations_dir.join("V2__second.sql");
fs::File::create(&sql2).unwrap();
let rs3 = migrations_dir.join("V3__third.rs");
fs::File::create(&rs3).unwrap();
let migrations = load_sql_migrations(migrations_dir).unwrap();
assert_eq!(migrations.len(), 2);
assert_eq!(&migrations[0].to_string(), "V1__first");
assert_eq!(&migrations[1].to_string(), "V2__second");
}
}