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
use super::{Datasource, Generator};
use crate::{
    datamodel_connector::RelationMode,
    diagnostics::{DatamodelError, Diagnostics},
    PreviewFeature,
};
use enumflags2::BitFlags;

#[derive(Debug)]
pub struct Configuration {
    pub generators: Vec<Generator>,
    pub datasources: Vec<Datasource>,
    pub warnings: Vec<diagnostics::DatamodelWarning>,
}

impl Configuration {
    pub fn validate_that_one_datasource_is_provided(&self) -> Result<(), Diagnostics> {
        if self.datasources.is_empty() {
            Err(DatamodelError::new_validation_error(
                "You defined no datasource. You must define exactly one datasource.",
                schema_ast::ast::Span::new(0, 0),
            )
            .into())
        } else {
            Ok(())
        }
    }

    pub fn relation_mode(&self) -> Option<RelationMode> {
        self.datasources.first().map(|source| source.relation_mode())
    }

    pub fn max_identifier_length(&self) -> usize {
        self.datasources
            .first()
            .map(|source| source.active_connector.max_identifier_length())
            .unwrap_or(usize::MAX)
    }

    pub fn preview_features(&self) -> BitFlags<PreviewFeature> {
        self.generators.iter().fold(BitFlags::empty(), |acc, generator| {
            acc | generator.preview_features.unwrap_or_default()
        })
    }

    /// Resolve datasource url for query engine.
    ///
    /// The main interesting thing here is we want to ignore any error that may arise from resolving
    /// direct_url.
    pub fn resolve_datasource_urls_query_engine<F>(
        &mut self,
        url_overrides: &[(String, String)],
        env: F,
        ignore_env_errors: bool,
    ) -> Result<(), Diagnostics>
    where
        F: Fn(&str) -> Option<String> + Copy,
    {
        for datasource in &mut self.datasources {
            if let Some((_, url)) = url_overrides.iter().find(|(name, _url)| name == &datasource.name) {
                datasource.url.value = Some(url.clone());
                datasource.url.from_env_var = None;
            }

            if datasource.url.from_env_var.is_some() && datasource.url.value.is_none() {
                datasource.url.value = match datasource.load_url(env) {
                    Ok(url) => Some(url),
                    Err(_) if ignore_env_errors => None,
                    Err(error) => return Err(error),
                };
            }

            if let Some(direct_url) = &datasource.direct_url {
                let result = match super::from_url(direct_url, env) {
                    Err(_) => None, // ignore errors because we don't really need direct_url in QE
                    Ok(res) => Some(res),
                };

                datasource.direct_url = Some(crate::StringFromEnvVar {
                    from_env_var: direct_url.from_env_var.clone(),
                    value: result,
                });
            }
        }

        Ok(())
    }

    /// Resolve datasource URL's for getConfig.
    /// The main reason this exists is:
    ///   - we want to error if we can't resolve direct_url
    ///   - we want to skip validation for url IF we have a direct_url
    ///
    /// For that last bit, we only do this currently because our validation errors on URL's starting
    /// with 'prisma://'. We would ideally like to do the other validations and ignore in this case.
    pub fn resolve_datasource_urls_prisma_fmt<F>(
        &mut self,
        url_overrides: &[(String, String)],
        env: F,
    ) -> Result<(), Diagnostics>
    where
        F: Fn(&str) -> Option<String> + Copy,
    {
        for datasource in &mut self.datasources {
            if let Some((_, url)) = url_overrides.iter().find(|(name, _url)| name == &datasource.name) {
                datasource.url.value = Some(url.clone());
                datasource.url.from_env_var = None;
            }

            let mut has_direct_url = false;

            if let (Some(direct_url), Some(span)) = (&datasource.direct_url, &datasource.direct_url_span) {
                let result = match super::from_url(direct_url, env) {
                        Err(err) => {
                            match err {
                        super::UrlValidationError::EmptyUrlValue => {
                            let msg = "You must provide a nonempty direct URL";
                            Err(DatamodelError::new_source_validation_error(msg, &datasource.name, *span))
                        }
                        super::UrlValidationError::EmptyEnvValue(env_var) => {
                            Err(DatamodelError::new_source_validation_error(
                                &format!(
                                    "You must provide a nonempty direct URL. The environment variable `{env_var}` resolved to an empty string."
                                ),
                                &datasource.name,
                                *span,
                            ))
                        },
                        super::UrlValidationError::NoEnvValue(env_var) => {
                            Err(DatamodelError::new_environment_functional_evaluation_error(
                                env_var,
                                *span,
                            ))
                        },
                        super::UrlValidationError::NoUrlOrEnv => {
                          Ok(None)
                        },
                    }
                        },
                        Ok(res) => Ok(Some(res)),
                    }?;

                has_direct_url = true;

                datasource.direct_url = Some(crate::StringFromEnvVar {
                    from_env_var: direct_url.from_env_var.clone(),
                    value: result,
                });
            }

            // We probably just need to improve validation, especially around allowing 'prisma://'
            // urls.
            if datasource.url.from_env_var.is_some() && datasource.url.value.is_none() {
                if has_direct_url {
                    datasource.url.value = Some(datasource.load_url_no_validation(env)?);
                } else {
                    datasource.url.value = Some(datasource.load_url(env)?);
                }
            }
        }

        Ok(())
    }
}