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
use migrations_internals::search_for_migrations_directory;

use std::env;
use std::error::Error;
use std::path::{Path, PathBuf};

pub fn migration_directory_from_given_path(
    given_path: Option<&str>,
) -> Result<PathBuf, Box<dyn Error + Send + Sync>> {
    let cargo_toml_directory = 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<dyn Error + Send + Sync>> {
    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");
            search_for_migrations_directory(&src_dir).ok_or_else(|| {
                format!(
                    "Failed to find migration directory in {}",
                    src_dir.display()
                )
            })?
        }
    };

    result.canonicalize().map_err(Into::into)
}

#[cfg(test)]
mod tests {
    use tempfile;

    use self::tempfile::Builder;
    use super::resolve_migrations_directory;
    use std::fs;
    use std::path::Path;

    #[test]
    fn migrations_directory_resolved_relative_to_cargo_manifest_dir() {
        let tempdir = Builder::new().prefix("diesel").tempdir().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 = Builder::new().prefix("diesel").tempdir().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 = Builder::new().prefix("diesel").tempdir().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 = Builder::new().prefix("diesel").tempdir().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 = Builder::new().prefix("diesel").tempdir().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()
        );
    }
}