/
datasource_loader.rs
275 lines (238 loc) Β· 10.1 KB
/
datasource_loader.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::{
ast::{self, SourceConfig, Span},
common::preview_features::PreviewFeature,
configuration::StringFromEnvVar,
diagnostics::{DatamodelError, Diagnostics},
Datasource,
};
use datamodel_connector::ReferentialIntegrity;
use enumflags2::BitFlags;
use parser_database::{ast::WithDocumentation, coerce, coerce_array, coerce_opt};
use std::{borrow::Cow, collections::HashMap};
const PREVIEW_FEATURES_KEY: &str = "previewFeatures";
const SCHEMAS_KEY: &str = "schemas";
const SHADOW_DATABASE_URL_KEY: &str = "shadowDatabaseUrl";
const URL_KEY: &str = "url";
/// Is responsible for loading and validating Datasources defined in an AST.
pub(crate) struct DatasourceLoader;
impl DatasourceLoader {
/// Loads all datasources from the provided schema AST.
/// - `ignore_datasource_urls`: datasource URLs are not parsed. They are replaced with dummy values.
/// - `datasource_url_overrides`: datasource URLs are not parsed and overridden with the provided ones.
pub fn load_datasources_from_ast(
&self,
ast_schema: &ast::SchemaAst,
preview_features: BitFlags<PreviewFeature>,
diagnostics: &mut Diagnostics,
connectors: crate::builtin_connectors::ConnectorRegistry,
) -> Vec<Datasource> {
let mut sources = Vec::new();
for src in ast_schema.sources() {
if let Some(source) = self.lift_datasource(src, preview_features, diagnostics, connectors) {
sources.push(source)
}
}
if sources.len() > 1 {
for src in ast_schema.sources() {
diagnostics.push_error(DatamodelError::new_source_validation_error(
"You defined more than one datasource. This is not allowed yet because support for multiple databases has not been implemented yet.",
&src.name.name,
src.span,
));
}
}
sources
}
fn lift_datasource(
&self,
ast_source: &ast::SourceConfig,
preview_features: BitFlags<PreviewFeature>,
diagnostics: &mut Diagnostics,
connectors: crate::builtin_connectors::ConnectorRegistry,
) -> Option<Datasource> {
let source_name = &ast_source.name.name;
let mut args: HashMap<_, _> = ast_source
.properties
.iter()
.map(|arg| (arg.name.name.as_str(), (arg.span, &arg.value)))
.collect();
let (_, provider_arg) = match args.remove("provider") {
Some(provider) => provider,
None => {
diagnostics.push_error(DatamodelError::new_source_argument_not_found_error(
"provider",
&ast_source.name.name,
ast_source.span,
));
return None;
}
};
if provider_arg.is_env_expression() {
let msg = Cow::Borrowed("A datasource must not use the env() function in the provider argument.");
diagnostics.push_error(DatamodelError::new_functional_evaluation_error(msg, ast_source.span));
return None;
}
let provider = match coerce_opt::string(provider_arg) {
Some("") => {
diagnostics.push_error(DatamodelError::new_source_validation_error(
"The provider argument in a datasource must not be empty",
source_name,
provider_arg.span(),
));
return None;
}
None => {
diagnostics.push_error(DatamodelError::new_source_validation_error(
"The provider argument in a datasource must be a string literal",
source_name,
provider_arg.span(),
));
return None;
}
Some(provider) => provider,
};
let (_, url_arg) = match args.remove(URL_KEY) {
Some(url_arg) => url_arg,
None => {
diagnostics.push_error(DatamodelError::new_source_argument_not_found_error(
URL_KEY,
&ast_source.name.name,
ast_source.span,
));
return None;
}
};
let url = StringFromEnvVar::coerce(url_arg, diagnostics)?;
let shadow_database_url_arg = args.remove(SHADOW_DATABASE_URL_KEY);
let shadow_database_url: Option<(StringFromEnvVar, Span)> =
if let Some((_, shadow_database_url_arg)) = shadow_database_url_arg.as_ref() {
match StringFromEnvVar::coerce(shadow_database_url_arg, diagnostics) {
Some(shadow_database_url) => Some(shadow_database_url)
.filter(|s| !s.as_literal().map(|lit| lit.is_empty()).unwrap_or(false))
.map(|url| (url, shadow_database_url_arg.span())),
None => None,
}
} else {
None
};
preview_features_guardrail(&args, diagnostics);
let documentation = ast_source.documentation().map(String::from);
let referential_integrity = get_referential_integrity(&args, preview_features, ast_source, diagnostics);
let active_connector: &'static dyn datamodel_connector::Connector =
match connectors.iter().find(|c| c.is_provider(provider)) {
Some(c) => *c,
None => {
diagnostics.push_error(DatamodelError::new_datasource_provider_not_known_error(
provider,
provider_arg.span(),
));
return None;
}
};
if let Some(integrity) = referential_integrity {
if !active_connector
.allowed_referential_integrity_settings()
.contains(integrity)
{
let span = args
.get("referentialIntegrity")
.map(|(_, v)| v.span())
.unwrap_or_else(Span::empty);
let supported_values = active_connector
.allowed_referential_integrity_settings()
.iter()
.map(|v| format!(r#""{}""#, v))
.collect::<Vec<_>>()
.join(", ");
let message = format!(
"Invalid referential integrity setting: \"{}\". Supported values: {}",
integrity, supported_values,
);
let error = DatamodelError::new_source_validation_error(&message, "referentialIntegrity", span);
diagnostics.push_error(error);
}
}
let (schemas, schemas_span) = args
.remove(SCHEMAS_KEY)
.and_then(|(_, expr)| coerce_array(expr, &coerce::string_with_span, diagnostics).map(|b| (b, expr.span())))
.map(|(mut schemas, span)| {
schemas.sort_by(|(a, _), (b, _)| a.cmp(b));
for pair in schemas.windows(2) {
if pair[0].0 == pair[1].0 {
diagnostics.push_error(DatamodelError::new_static(
"Duplicated schema names are not allowed",
pair[0].1,
))
}
}
(schemas, Some(span))
})
.unwrap_or_default();
// we handle these elsewhere
args.remove("previewFeatures");
args.remove("referentialIntegrity");
for (name, (span, _)) in args.into_iter() {
diagnostics.push_error(DatamodelError::new_property_not_known_error(name, span));
}
Some(Datasource {
schemas: schemas.into_iter().map(|(s, span)| (s.to_owned(), span)).collect(),
schemas_span,
name: source_name.to_string(),
provider: provider.to_owned(),
active_provider: active_connector.provider_name(),
url,
url_span: url_arg.span(),
documentation,
active_connector,
shadow_database_url,
referential_integrity,
})
}
}
const REFERENTIAL_INTEGRITY_PREVIEW_FEATURE_ERR: &str = r#"
The `referentialIntegrity` option can only be set if the preview feature is enabled in a generator block.
Example:
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}
"#;
fn get_referential_integrity(
args: &HashMap<&str, (Span, &ast::Expression)>,
preview_features: BitFlags<PreviewFeature>,
source: &SourceConfig,
diagnostics: &mut Diagnostics,
) -> Option<ReferentialIntegrity> {
args.get("referentialIntegrity").and_then(|(span, value)| {
if !preview_features.contains(PreviewFeature::ReferentialIntegrity) {
diagnostics.push_error(DatamodelError::new_source_validation_error(
REFERENTIAL_INTEGRITY_PREVIEW_FEATURE_ERR,
&source.name.name,
*span,
));
None
} else {
match coerce::string(value, diagnostics)? {
"prisma" => Some(ReferentialIntegrity::Prisma),
"foreignKeys" => Some(ReferentialIntegrity::ForeignKeys),
s => {
let message = format!(
"Invalid referential integrity setting: \"{}\". Supported values: \"prisma\", \"foreignKeys\"",
s
);
let error =
DatamodelError::new_source_validation_error(&message, "referentialIntegrity", value.span());
diagnostics.push_error(error);
None
}
}
}
})
}
fn preview_features_guardrail(args: &HashMap<&str, (Span, &ast::Expression)>, diagnostics: &mut Diagnostics) {
let arg = args.get(PREVIEW_FEATURES_KEY);
if let Some((span, _)) = arg {
let msg = "Preview features are only supported in the generator block. Please move this field to the generator block.";
diagnostics.push_error(DatamodelError::new_static(msg, *span));
}
}