forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrations.rs
More file actions
106 lines (91 loc) · 3.94 KB
/
Copy pathmigrations.rs
File metadata and controls
106 lines (91 loc) · 3.94 KB
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
use diesel::migrations::search_for_migrations_directory;
use std::path::{PathBuf, Path};
use std::error::Error;
use std::env;
pub fn migration_directory_from_given_path(given_path: Option<&str>)
-> Result<PathBuf, Box<Error>>
{
let cargo_toml_directory = try!(env::var("CARGO_MANIFEST_DIR"));
let cargo_manifest_path = Path::new(&cargo_toml_directory);
let migrations_path = given_path.as_ref().map(Path::new);
resolve_migrations_directory(cargo_manifest_path, migrations_path)
}
fn resolve_migrations_directory(
cargo_manifest_dir: &Path,
relative_path_to_migrations: Option<&Path>,
) -> Result<PathBuf, Box<Error>> {
let result = match relative_path_to_migrations {
Some(dir) => cargo_manifest_dir.join(dir),
None => {
// People commonly put their migrations in src/migrations
// so start the search there rather than the project root
let src_dir = cargo_manifest_dir.join("src");
try!(search_for_migrations_directory(&src_dir))
}
};
result.canonicalize().map_err(Into::into)
}
#[cfg(test)]
mod tests {
extern crate tempdir;
use self::tempdir::TempDir;
use std::fs;
use std::path::Path;
use super::resolve_migrations_directory;
#[test]
fn migrations_directory_resolved_relative_to_cargo_manifest_dir() {
let tempdir = TempDir::new("diesel").unwrap();
fs::create_dir_all(tempdir.path().join("foo/special_migrations")).unwrap();
let cargo_manifest_dir = tempdir.path().join("foo");
let relative_path = Some(Path::new("special_migrations"));
assert_eq!(
tempdir.path().join("foo/special_migrations").canonicalize().ok(),
resolve_migrations_directory(&cargo_manifest_dir, relative_path).ok()
);
}
#[test]
fn migrations_directory_canonicalizes_result() {
let tempdir = TempDir::new("diesel").unwrap();
fs::create_dir_all(tempdir.path().join("foo/migrations/bar")).unwrap();
fs::create_dir_all(tempdir.path().join("foo/bar")).unwrap();
let cargo_manifest_dir = tempdir.path().join("foo/bar");
let relative_path = Some(Path::new("../migrations/bar"));
assert_eq!(
tempdir.path().join("foo/migrations/bar").canonicalize().ok(),
resolve_migrations_directory(&cargo_manifest_dir, relative_path).ok()
);
}
#[test]
fn migrations_directory_defaults_to_searching_migrations_path() {
let tempdir = TempDir::new("diesel").unwrap();
fs::create_dir_all(tempdir.path().join("foo/migrations")).unwrap();
fs::create_dir_all(tempdir.path().join("foo/bar")).unwrap();
let cargo_manifest_dir = tempdir.path().join("foo/bar");
assert_eq!(
tempdir.path().join("foo/migrations").canonicalize().ok(),
resolve_migrations_directory(&cargo_manifest_dir, None).ok()
);
}
#[test]
fn migrations_directory_searches_src_migrations_if_present() {
let tempdir = TempDir::new("diesel").unwrap();
fs::create_dir_all(tempdir.path().join("foo/src/migrations")).unwrap();
fs::create_dir_all(tempdir.path().join("foo/migrations")).unwrap();
let cargo_manifest_dir = tempdir.path().join("foo");
assert_eq!(
tempdir.path().join("foo/src/migrations").canonicalize().ok(),
resolve_migrations_directory(&cargo_manifest_dir, None).ok()
);
}
#[test]
fn migrations_directory_allows_no_parent_dir_for_cargo_manifest_dir() {
let tempdir = TempDir::new("diesel").unwrap();
fs::create_dir_all(tempdir.path().join("special_migrations")).unwrap();
let cargo_manifest_dir = tempdir.path().to_owned();
let relative_path = Some(Path::new("special_migrations"));
assert_eq!(
tempdir.path().join("special_migrations").canonicalize().ok(),
resolve_migrations_directory(&cargo_manifest_dir, relative_path).ok()
);
}
}