-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnative_operations.rs
344 lines (317 loc) · 12.7 KB
/
native_operations.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! Handle the creation of Native Operations.
use std::path::PathBuf;
use super::{update, Context};
use ndc_postgres_configuration as configuration;
use ndc_postgres_configuration::environment::Environment;
pub use configuration::version4::native_operations::Kind;
/// Commands on Native Operations.
#[derive(Debug, Clone, clap::Subcommand)]
pub enum Command {
/// List the existing Native Operations.
List,
/// Create a new Native Operation from a SQL file.
Create {
/// Relative path to the SQL file inside the connector configuration directory.
#[arg(long)]
operation_path: PathBuf,
/// Operation kind.
#[arg(long)]
kind: Kind,
/// Override the Native Operation definition if it exists.
#[arg(long)]
r#override: bool,
},
/// Delete an existing Native Operation from the configuration.
Delete {
/// The name of the Native Operation.
#[arg(long)]
name: String,
/// Operation kind.
#[arg(long)]
kind: Kind,
},
}
/// Run a command in a given directory.
pub async fn run(command: Command, context: Context<impl Environment>) -> anyhow::Result<()> {
match command {
Command::List => list(context).await?,
Command::Create {
operation_path,
kind,
r#override,
} => {
create(
context,
operation_path,
kind,
if r#override {
Override::Yes
} else {
Override::No
},
)
.await?;
}
Command::Delete { name, kind } => {
delete(context, name, kind).await?;
}
};
Ok(())
}
/// List all native operations.
async fn list(context: Context<impl Environment>) -> anyhow::Result<()> {
// Read the configuration.
let mut configuration =
configuration::parse_configuration(context.context_path.clone()).await?;
match configuration {
configuration::ParsedConfiguration::Version3(_) => Err(anyhow::anyhow!(
"To use the native operations commands, please upgrade to the latest version."
))?,
configuration::ParsedConfiguration::Version4(ref mut configuration) => {
let operations = &configuration.metadata.native_queries.0;
println!("Native Queries:");
for native_operation in operations.iter().filter(|op| !op.1.is_procedure) {
println!("- {}", native_operation.0);
}
println!("Native Mutations:");
for native_operation in operations.iter().filter(|op| op.1.is_procedure) {
println!("- {}", native_operation.0);
}
}
configuration::ParsedConfiguration::Version5(ref mut configuration) => {
let operations = &configuration.metadata.native_operations;
println!("Native Queries:");
for native_operation in &operations.queries.0 {
println!("- {}", native_operation.0);
}
println!("Native Mutations:");
for native_operation in &operations.mutations.0 {
println!("- {}", native_operation.0);
}
}
};
Ok(())
}
/// Override Native Operation definition if exists?
#[derive(Debug, Clone, clap::ValueEnum)]
enum Override {
Yes,
No,
}
/// Take a SQL file containing a Native Operation, check against the database that it is valid,
/// and add it to the configuration if it is.
async fn create(
context: Context<impl Environment>,
operation_path: PathBuf,
kind: Kind,
override_entry: Override,
) -> anyhow::Result<()> {
// Read the configuration.
let mut configuration =
configuration::parse_configuration(context.context_path.clone()).await?;
// Prepare the Native Operation SQL so it can be checked against the db.
let name = operation_path
.file_stem()
.ok_or(anyhow::anyhow!("SQL file not found"))?
.to_str()
.ok_or(anyhow::anyhow!("Could not convert SQL file name to string"))?
.to_string();
// Read the SQL file.
let file_contents = match std::fs::read_to_string(context.context_path.join(&operation_path)) {
Ok(ok) => ok,
Err(err) => anyhow::bail!("{}: {}", operation_path.display(), err),
};
match configuration {
configuration::ParsedConfiguration::Version3(_) => Err(anyhow::anyhow!(
"To use the native operations commands, please upgrade to the latest version."
))?,
configuration::ParsedConfiguration::Version4(ref mut configuration) => {
let connection_string = configuration.get_connection_uri()?;
let new_native_operation = configuration::version4::native_operations::create(
configuration,
&connection_string,
&operation_path,
&file_contents,
kind,
)
.await?;
// Add the new native operation to the configuration.
match override_entry {
Override::Yes => {
configuration
.metadata
.native_queries
.0
.insert(name, new_native_operation);
}
Override::No => {
// Only insert if vacant.
if let std::collections::btree_map::Entry::Vacant(entry) =
configuration.metadata.native_queries.0.entry(name.clone())
{
entry.insert(new_native_operation);
} else {
anyhow::bail!("A Native Operation with the name '{name}' already exists. To override, use the --override flag.");
}
}
}
}
configuration::ParsedConfiguration::Version5(ref mut configuration) => {
let connection_string = configuration.get_connection_uri()?;
let kind = match kind {
configuration::version4::native_operations::Kind::Query => {
configuration::version5::native_operations::Kind::Query
}
configuration::version4::native_operations::Kind::Mutation => {
configuration::version5::native_operations::Kind::Mutation
}
};
let new_native_operation = configuration::version5::native_operations::create(
configuration,
&connection_string,
&operation_path,
&file_contents,
)
.await?;
// Add the new native operation to the configuration.
match override_entry {
Override::Yes => match kind {
configuration::version5::native_operations::Kind::Query => {
configuration
.metadata
.native_operations
.queries
.0
.insert(name, new_native_operation);
}
configuration::version5::native_operations::Kind::Mutation => {
configuration
.metadata
.native_operations
.mutations
.0
.insert(name, new_native_operation);
}
},
Override::No => {
// Only insert if vacant.
if let std::collections::btree_map::Entry::Vacant(entry) = match kind {
configuration::version5::native_operations::Kind::Query => configuration
.metadata
.native_operations
.queries
.0
.entry(name.clone()),
configuration::version5::native_operations::Kind::Mutation => configuration
.metadata
.native_operations
.mutations
.0
.entry(name.clone()),
} {
entry.insert(new_native_operation);
} else {
anyhow::bail!("A Native Operation with the name '{name}' already exists. To override, use the --override flag.");
}
}
}
}
};
// We write the configuration including the new Native Operation to file.
configuration::write_parsed_configuration(configuration, context.context_path.clone()).await?;
// We update the configuration as well so that the introspection will add missing scalar type entries if necessary.
update(context).await
}
/// Delete a Native Operation by name.
async fn delete(
context: Context<impl Environment>,
name: String,
kind: Kind,
) -> anyhow::Result<()> {
// Read the configuration.
let mut configuration =
configuration::parse_configuration(context.context_path.clone()).await?;
let error_message_not_exist = format!(
"A Native {} with the name '{}' does not exists.",
match kind {
Kind::Mutation => "Mutation",
Kind::Query => "Query",
},
name
);
match configuration {
configuration::ParsedConfiguration::Version3(_) => Err(anyhow::anyhow!(
"To use the native operations commands, please upgrade to the latest version."
))?,
configuration::ParsedConfiguration::Version4(ref mut configuration) => {
// Delete if exists and is of the same type, error if not.
match configuration.metadata.native_queries.0.entry(name.clone()) {
std::collections::btree_map::Entry::Occupied(entry) => {
let value = entry.get();
if value.is_procedure {
match kind {
Kind::Mutation => {
entry.remove_entry();
}
Kind::Query => {
anyhow::bail!(format!("{error_message_not_exist}\n Did you mean the Native Mutation with the same name?"));
}
}
} else {
match kind {
Kind::Mutation => {
anyhow::bail!(format!("{error_message_not_exist}\n Did you mean the Native Query with the same name?"));
}
Kind::Query => {
entry.remove_entry();
}
}
}
}
std::collections::btree_map::Entry::Vacant(_) => {
anyhow::bail!(error_message_not_exist);
}
}
}
configuration::ParsedConfiguration::Version5(ref mut configuration) => {
// Delete if exists and is of the same type, error if not.
match kind {
Kind::Mutation => {
match configuration
.metadata
.native_operations
.mutations
.0
.entry(name.clone())
{
std::collections::btree_map::Entry::Occupied(entry) => {
entry.remove_entry();
}
std::collections::btree_map::Entry::Vacant(_) => {
anyhow::bail!(error_message_not_exist);
}
}
}
Kind::Query => {
match configuration
.metadata
.native_operations
.queries
.0
.entry(name.clone())
{
std::collections::btree_map::Entry::Occupied(entry) => {
entry.remove_entry();
}
std::collections::btree_map::Entry::Vacant(_) => {
anyhow::bail!(error_message_not_exist);
}
}
}
}
}
}
// We write the configuration excluding the deleted Native Operation.
configuration::write_parsed_configuration(configuration, context.context_path.clone()).await?;
Ok(())
}