migrations_macros/
migrations.rs

1use migrations_internals::search_for_migrations_directory;
2
3use std::env;
4use std::error::Error;
5use std::path::{Path, PathBuf};
6
7pub fn migration_directory_from_given_path(
8    given_path: Option<&str>,
9) -> Result<PathBuf, Box<dyn Error + Send + Sync>> {
10    let cargo_toml_directory = env::var("CARGO_MANIFEST_DIR")?;
11    let cargo_manifest_path = Path::new(&cargo_toml_directory);
12    let migrations_path = given_path.as_ref().map(Path::new);
13    resolve_migrations_directory(cargo_manifest_path, migrations_path)
14}
15
16fn resolve_migrations_directory(
17    cargo_manifest_dir: &Path,
18    relative_path_to_migrations: Option<&Path>,
19) -> Result<PathBuf, Box<dyn Error + Send + Sync>> {
20    let result = match relative_path_to_migrations {
21        Some(dir) => cargo_manifest_dir.join(dir),
22        None => {
23            // People commonly put their migrations in src/migrations
24            // so start the search there rather than the project root
25            let src_dir = cargo_manifest_dir.join("src");
26            search_for_migrations_directory(&src_dir).ok_or_else(|| {
27                format!(
28                    "Failed to find migration directory in {}",
29                    src_dir.display()
30                )
31            })?
32        }
33    };
34
35    result.canonicalize().map_err(Into::into)
36}
37
38#[cfg(test)]
39mod tests {
40    use tempfile;
41
42    use self::tempfile::Builder;
43    use super::resolve_migrations_directory;
44    use std::fs;
45    use std::path::Path;
46
47    #[test]
48    fn migrations_directory_resolved_relative_to_cargo_manifest_dir() {
49        let tempdir = Builder::new().prefix("diesel").tempdir().unwrap();
50        fs::create_dir_all(tempdir.path().join("foo/special_migrations")).unwrap();
51        let cargo_manifest_dir = tempdir.path().join("foo");
52        let relative_path = Some(Path::new("special_migrations"));
53
54        assert_eq!(
55            tempdir
56                .path()
57                .join("foo/special_migrations")
58                .canonicalize()
59                .ok(),
60            resolve_migrations_directory(&cargo_manifest_dir, relative_path).ok()
61        );
62    }
63
64    #[test]
65    fn migrations_directory_canonicalizes_result() {
66        let tempdir = Builder::new().prefix("diesel").tempdir().unwrap();
67        fs::create_dir_all(tempdir.path().join("foo/migrations/bar")).unwrap();
68        fs::create_dir_all(tempdir.path().join("foo/bar")).unwrap();
69        let cargo_manifest_dir = tempdir.path().join("foo/bar");
70        let relative_path = Some(Path::new("../migrations/bar"));
71
72        assert_eq!(
73            tempdir
74                .path()
75                .join("foo/migrations/bar")
76                .canonicalize()
77                .ok(),
78            resolve_migrations_directory(&cargo_manifest_dir, relative_path).ok()
79        );
80    }
81
82    #[test]
83    fn migrations_directory_defaults_to_searching_migrations_path() {
84        let tempdir = Builder::new().prefix("diesel").tempdir().unwrap();
85        fs::create_dir_all(tempdir.path().join("foo/migrations")).unwrap();
86        fs::create_dir_all(tempdir.path().join("foo/bar")).unwrap();
87        let cargo_manifest_dir = tempdir.path().join("foo/bar");
88
89        assert_eq!(
90            tempdir.path().join("foo/migrations").canonicalize().ok(),
91            resolve_migrations_directory(&cargo_manifest_dir, None).ok()
92        );
93    }
94
95    #[test]
96    fn migrations_directory_searches_src_migrations_if_present() {
97        let tempdir = Builder::new().prefix("diesel").tempdir().unwrap();
98        fs::create_dir_all(tempdir.path().join("foo/src/migrations")).unwrap();
99        fs::create_dir_all(tempdir.path().join("foo/migrations")).unwrap();
100        let cargo_manifest_dir = tempdir.path().join("foo");
101
102        assert_eq!(
103            tempdir
104                .path()
105                .join("foo/src/migrations")
106                .canonicalize()
107                .ok(),
108            resolve_migrations_directory(&cargo_manifest_dir, None).ok()
109        );
110    }
111
112    #[test]
113    fn migrations_directory_allows_no_parent_dir_for_cargo_manifest_dir() {
114        let tempdir = Builder::new().prefix("diesel").tempdir().unwrap();
115        fs::create_dir_all(tempdir.path().join("special_migrations")).unwrap();
116        let cargo_manifest_dir = tempdir.path().to_owned();
117        let relative_path = Some(Path::new("special_migrations"));
118        assert_eq!(
119            tempdir
120                .path()
121                .join("special_migrations")
122                .canonicalize()
123                .ok(),
124            resolve_migrations_directory(&cargo_manifest_dir, relative_path).ok()
125        );
126    }
127}