-
Notifications
You must be signed in to change notification settings - Fork 213
/
model.rs
658 lines (580 loc) Β· 21.1 KB
/
model.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use crate::default_value::DefaultKind;
use crate::field::{Field, FieldType, RelationField, ScalarField};
use crate::scalars::ScalarType;
use crate::traits::{Ignorable, WithDatabaseName, WithName};
use indoc::formatdoc;
use psl_core::parser_database::IndexType;
use std::{borrow::Cow, fmt};
/// Represents a model in a prisma schema.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Model {
/// Name of the model.
pub name: String,
/// Fields of the model.
pub fields: Vec<Field>,
/// Comments associated with this model.
pub documentation: Option<String>,
/// The database internal name of this model.
pub database_name: Option<String>,
/// Describes Composite Indexes
pub indices: Vec<IndexDefinition>,
/// Describes the Primary Keys
pub primary_key: Option<PrimaryKeyDefinition>,
/// Indicates if this model is generated.
pub is_generated: bool,
/// Indicates if this model has to be commented out.
pub is_commented_out: bool,
/// Indicates if this model has to be ignored by the Client.
pub is_ignored: bool,
/// The contents of the `@@schema("...")` attribute.
pub schema: Option<String>,
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum IndexAlgorithm {
BTree,
Hash,
Gist,
Gin,
SpGist,
Brin,
}
impl Default for IndexAlgorithm {
fn default() -> Self {
Self::BTree
}
}
impl AsRef<str> for IndexAlgorithm {
fn as_ref(&self) -> &str {
match self {
IndexAlgorithm::BTree => "BTree",
IndexAlgorithm::Hash => "Hash",
IndexAlgorithm::Gist => "Gist",
IndexAlgorithm::Gin => "Gin",
IndexAlgorithm::SpGist => "SpGist",
IndexAlgorithm::Brin => "Brin",
}
}
}
impl fmt::Display for IndexAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_ref())
}
}
/// Represents an index defined via `@@index`, `@unique` or `@@unique`.
#[derive(Debug, PartialEq, Clone)]
pub struct IndexDefinition {
pub name: Option<String>,
pub db_name: Option<String>,
pub fields: Vec<IndexField>,
pub tpe: IndexType,
pub clustered: Option<bool>,
pub algorithm: Option<IndexAlgorithm>,
pub defined_on_field: bool,
}
impl IndexDefinition {
pub fn is_unique(&self) -> bool {
matches!(self.tpe, IndexType::Unique)
}
pub fn is_fulltext(&self) -> bool {
matches!(self.tpe, IndexType::Fulltext)
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum OperatorClass {
// GiST
InetOps,
// GIN
JsonbOps,
JsonbPathOps,
ArrayOps,
// SP-GiST
TextOps,
// BRIN
BitMinMaxOps,
VarBitMinMaxOps,
BpcharBloomOps,
BpcharMinMaxOps,
ByteaBloomOps,
ByteaMinMaxOps,
DateBloomOps,
DateMinMaxOps,
DateMinMaxMultiOps,
Float4BloomOps,
Float4MinMaxOps,
Float4MinMaxMultiOps,
Float8BloomOps,
Float8MinMaxOps,
Float8MinMaxMultiOps,
InetInclusionOps,
InetBloomOps,
InetMinMaxOps,
InetMinMaxMultiOps,
Int2BloomOps,
Int2MinMaxOps,
Int2MinMaxMultiOps,
Int4BloomOps,
Int4MinMaxOps,
Int4MinMaxMultiOps,
Int8BloomOps,
Int8MinMaxOps,
Int8MinMaxMultiOps,
NumericBloomOps,
NumericMinMaxOps,
NumericMinMaxMultiOps,
OidBloomOps,
OidMinMaxOps,
OidMinMaxMultiOps,
TextBloomOps,
TextMinMaxOps,
TimestampBloomOps,
TimestampMinMaxOps,
TimestampMinMaxMultiOps,
TimestampTzBloomOps,
TimestampTzMinMaxOps,
TimestampTzMinMaxMultiOps,
TimeBloomOps,
TimeMinMaxOps,
TimeMinMaxMultiOps,
TimeTzBloomOps,
TimeTzMinMaxOps,
TimeTzMinMaxMultiOps,
UuidBloomOps,
UuidMinMaxOps,
UuidMinMaxMultiOps,
Raw(Cow<'static, str>),
}
impl AsRef<str> for OperatorClass {
fn as_ref(&self) -> &str {
match self {
Self::InetOps => "InetOps",
Self::JsonbOps => "JsonbOps",
Self::JsonbPathOps => "JsonbPathOps",
Self::ArrayOps => "ArrayOps",
Self::TextOps => "TextOps",
Self::BitMinMaxOps => "BitMinMaxOps",
Self::VarBitMinMaxOps => "VarBitMinMaxOps",
Self::BpcharBloomOps => "BpcharBloomOps",
Self::BpcharMinMaxOps => "BpcharMinMaxOps",
Self::ByteaBloomOps => "ByteaBloomOps",
Self::ByteaMinMaxOps => "ByteaMinMaxOps",
Self::DateBloomOps => "DateBloomOps",
Self::DateMinMaxOps => "DateMinMaxOps",
Self::DateMinMaxMultiOps => "DateMinMaxMultiOps",
Self::Float4BloomOps => "Float4BloomOps",
Self::Float4MinMaxOps => "Float4MinMaxOps",
Self::Float4MinMaxMultiOps => "Float4MinMaxMultiOps",
Self::Float8BloomOps => "Float8BloomOps",
Self::Float8MinMaxOps => "Float8MinMaxOps",
Self::Float8MinMaxMultiOps => "Float8MinMaxMultiOps",
Self::InetInclusionOps => "InetInclusionOps",
Self::InetBloomOps => "InetBloomOps",
Self::InetMinMaxOps => "InetMinMaxOps",
Self::InetMinMaxMultiOps => "InetMinMaxMultiOps",
Self::Int2BloomOps => "Int2BloomOps",
Self::Int2MinMaxOps => "Int2MinMaxOps",
Self::Int2MinMaxMultiOps => "Int2MinMaxMultiOps",
Self::Int4BloomOps => "Int4BloomOps",
Self::Int4MinMaxOps => "Int4MinMaxOps",
Self::Int4MinMaxMultiOps => "Int4MinMaxMultiOps",
Self::Int8BloomOps => "Int8BloomOps",
Self::Int8MinMaxOps => "Int8MinMaxOps",
Self::Int8MinMaxMultiOps => "Int8MinMaxMultiOps",
Self::NumericBloomOps => "NumericBloomOps",
Self::NumericMinMaxOps => "NumericMinMaxOps",
Self::NumericMinMaxMultiOps => "NumericMinMaxMultiOps",
Self::OidBloomOps => "OidBloomOps",
Self::OidMinMaxOps => "OidMinMaxOps",
Self::OidMinMaxMultiOps => "OidMinMaxMultiOps",
Self::TextBloomOps => "TextBloomOps",
Self::TextMinMaxOps => "TextMinMaxOps",
Self::TimestampBloomOps => "TimestampBloomOps",
Self::TimestampMinMaxOps => "TimestampMinMaxOps",
Self::TimestampMinMaxMultiOps => "TimestampMinMaxMultiOps",
Self::TimestampTzBloomOps => "TimestampTzBloomOps",
Self::TimestampTzMinMaxOps => "TimestampTzMinMaxOps",
Self::TimestampTzMinMaxMultiOps => "TimestampTzMinMaxMultiOps",
Self::TimeBloomOps => "TimeBloomOps",
Self::TimeMinMaxOps => "TimeMinMaxOps",
Self::TimeMinMaxMultiOps => "TimeMinMaxMultiOps",
Self::TimeTzBloomOps => "TimeTzBloomOps",
Self::TimeTzMinMaxOps => "TimeTzMinMaxOps",
Self::TimeTzMinMaxMultiOps => "TimeTzMinMaxMultiOps",
Self::UuidBloomOps => "UuidBloomOps",
Self::UuidMinMaxOps => "UuidMinMaxOps",
Self::UuidMinMaxMultiOps => "UuidMinMaxOps",
Self::Raw(s) => s.as_ref(),
}
}
}
impl fmt::Display for OperatorClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_ref())
}
}
impl OperatorClass {
pub fn raw(op: impl Into<Cow<'static, str>>) -> Self {
Self::Raw(op.into())
}
pub fn as_raw(&self) -> Option<&str> {
match self {
Self::Raw(s) => Some(s),
_ => None,
}
}
pub fn is_raw(&self) -> bool {
matches!(self, Self::Raw(_))
}
}
/// A field in an index that optionally defines a sort order and length limit.
#[derive(Debug, PartialEq, Clone)]
pub struct IndexField {
pub path: Vec<(String, Option<String>)>,
pub sort_order: Option<SortOrder>,
pub length: Option<u32>,
pub operator_class: Option<OperatorClass>,
}
impl IndexField {
/// Tests only
pub fn new_in_model(name: &str) -> Self {
IndexField {
path: vec![(name.into(), None)],
sort_order: None,
length: None,
operator_class: None,
}
}
pub fn new_in_path(path: &[(&str, Option<&str>)]) -> Self {
IndexField {
path: path
.iter()
.map(|(k, v)| (k.to_string(), v.map(|v| v.to_string())))
.collect(),
sort_order: None,
length: None,
operator_class: None,
}
}
pub fn from_field(&self) -> &str {
&self.path.first().unwrap().0
}
}
/// Represents a primary key defined via `@@id` or `@id`.
#[derive(Debug, PartialEq, Clone)]
pub struct PrimaryKeyDefinition {
pub name: Option<String>,
pub db_name: Option<String>,
pub fields: Vec<PrimaryKeyField>,
pub defined_on_field: bool,
pub clustered: Option<bool>,
}
/// A field in a Primary Key that optionally defines a sort order and length limit.
#[derive(Debug, PartialEq, Clone)]
pub struct PrimaryKeyField {
pub name: String,
pub sort_order: Option<SortOrder>,
pub length: Option<u32>,
}
impl PrimaryKeyField {
/// Tests only
pub fn new(name: &str) -> Self {
PrimaryKeyField {
name: name.to_string(),
sort_order: None,
length: None,
}
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum SortOrder {
Asc,
Desc,
}
impl AsRef<str> for SortOrder {
fn as_ref(&self) -> &str {
match self {
SortOrder::Asc => "Asc",
SortOrder::Desc => "Desc",
}
}
}
/// A unique criterion is a set of fields through which a record can be uniquely identified.
#[derive(Debug)]
pub struct UniqueCriterion<'a> {
pub fields: Vec<&'a ScalarField>,
}
impl<'a> UniqueCriterion<'a> {
pub fn new(fields: Vec<&'a ScalarField>) -> UniqueCriterion<'a> {
UniqueCriterion { fields }
}
}
impl Model {
/// Creates a new model with the given name.
pub fn new(name: String, database_name: Option<String>) -> Model {
Model {
name,
fields: vec![],
indices: vec![],
primary_key: None,
documentation: None,
database_name,
is_generated: false,
is_commented_out: false,
is_ignored: false,
schema: None,
}
}
/// Adds a field to this model.
pub fn add_field(&mut self, field: Field) {
self.fields.push(field)
}
/// Gets an iterator over all fields.
pub fn fields(&self) -> std::slice::Iter<Field> {
self.fields.iter()
}
/// Gets a mutable iterator over all fields.
pub fn fields_mut(&mut self) -> impl Iterator<Item = &mut Field> {
self.fields.iter_mut()
}
/// Gets an iterator over all scalar fields.
pub fn scalar_fields(&self) -> impl Iterator<Item = &ScalarField> {
self.fields.iter().filter_map(|f| f.as_scalar_field())
}
/// Gets an iterator over all relation fields.
pub fn relation_fields(&self) -> impl Iterator<Item = &RelationField> {
self.fields.iter().filter_map(|f| f.as_relation_field())
}
/// Gets a mutable iterator over all scalar fields.
pub fn scalar_fields_mut(&mut self) -> impl Iterator<Item = &mut ScalarField> {
self.fields_mut().filter_map(|fw| match fw {
Field::RelationField(_) => None,
Field::CompositeField(_) => None,
Field::ScalarField(sf) => Some(sf),
})
}
/// Gets a mutable iterator over all relation fields.
pub fn relation_fields_mut(&mut self) -> impl Iterator<Item = &mut RelationField> {
self.fields_mut().filter_map(|fw| match fw {
Field::RelationField(rf) => Some(rf),
Field::CompositeField(_) => None,
Field::ScalarField(_) => None,
})
}
/// Finds a field by name.
pub fn find_field(&self, name: &str) -> Option<&Field> {
self.fields().find(|f| f.name() == name)
}
/// Finds a field by name and returns a mutable reference.
pub fn find_field_mut(&mut self, name: &str) -> &mut Field {
self.fields_mut().find(|f| f.name() == name).unwrap()
}
/// Finds a scalar field by name.
pub fn find_scalar_field(&self, name: &str) -> Option<&ScalarField> {
self.scalar_fields().find(|f| f.name == *name)
}
/// Finds a scalar field by name.
pub fn find_relation_field(&self, name: &str) -> Option<&RelationField> {
self.relation_fields().find(|f| f.name == *name)
}
/// Finds a field by database name.
pub fn find_scalar_field_db_name(&self, db_name: &str) -> Option<&ScalarField> {
self.scalar_fields()
.find(|f| f.database_name.as_deref() == Some(db_name))
}
pub fn has_field(&self, name: &str) -> bool {
self.find_field(name).is_some()
}
/// Finds a field by name and returns a mutable reference.
pub fn find_scalar_field_mut(&mut self, name: &str) -> &mut ScalarField {
let model_name = &self.name.clone();
self.scalar_fields_mut()
.find(|rf| rf.name == *name)
.unwrap_or_else(|| panic!("Could not find scalar field {} on model {}.", name, model_name))
}
/// Finds a relation field by name and returns a mutable reference.
#[track_caller]
pub fn find_relation_field_mut(&mut self, name: &str) -> &mut RelationField {
let model_name = &self.name.clone();
self.relation_fields_mut()
.find(|rf| rf.name == *name)
.unwrap_or_else(|| panic!("Could not find relation field {} on model {}.", name, model_name))
}
/// This should match the logic in `prisma_models::Model::primary_identifier`.
pub fn first_unique_criterion(&self) -> Vec<&ScalarField> {
match self.strict_unique_criterias().first() {
Some(criteria) => criteria.fields.clone(),
None => panic!("Could not find the first unique criteria on model {}", self.name()),
}
}
/// Optional unique fields are NOT considered a unique criterion.
///
/// Used for: A Model must have at least one STRICT unique criteria.
pub fn strict_unique_criterias(&self) -> Vec<UniqueCriterion> {
self.unique_criterias(false)
}
/// Optional unique fields are considered a unique criterion
///
/// Used for: A relation must reference one LOOSE unique criteria. (optional fields are okay in this case)
pub fn loose_unique_criterias(&self) -> Vec<UniqueCriterion> {
self.unique_criterias(true)
}
/// Returns the order of unique criterias ordered based on their precedence
fn unique_criterias(&self, allow_optional: bool) -> Vec<UniqueCriterion> {
let mut result = Vec::new();
// first candidate: primary key
{
if let Some(pk) = &self.primary_key {
let id_fields: Vec<_> = pk
.fields
.iter()
.map(|f| match self.find_scalar_field(&f.name) {
Some(field) => field,
None => {
let error = formatdoc!(
r#"
Hi there! We've been seeing this error in our error reporting backend,
but cannot reproduce it in our own tests. The problem is that we have a
primary key in the model `{}` that uses the column `{}` which we for
some reason don't have in our internal representation. If you see this,
could you please file an issue to https://github.com/prisma/prisma so we
can discuss about fixing this. -- Your friendly prisma developers.
"#,
self.name,
f.name
);
panic!("{}", error.replace('\n', " "));
}
})
.collect();
if !id_fields.is_empty()
&& !id_fields
.iter()
.any(|f| f.is_commented_out || (f.is_optional() && !allow_optional))
{
result.push(UniqueCriterion::new(id_fields));
}
}
}
// second candidate: any unique constraint where all fields are required
{
let mut unique_field_combi: Vec<UniqueCriterion> = self
.indices
.iter()
.filter(|id| id.is_unique())
.filter_map(|id| {
let fields: Vec<_> = id
.fields
.iter()
// TODO: remove this when supporting composite indices on QE
.filter(|f| f.path.len() == 1)
.map(|f| &f.path.first().unwrap().0)
.map(|name| self.find_scalar_field(name).unwrap())
.collect();
let no_fields_are_ineligible = !fields.iter().any(|f| f.is_commented_out);
let all_fields_are_required = fields.iter().all(|f| f.is_required());
((all_fields_are_required || allow_optional) && no_fields_are_ineligible)
.then(|| UniqueCriterion::new(fields))
})
.collect();
unique_field_combi.sort_by_key(|c| c.fields.len());
result.extend(unique_field_combi)
}
result
}
pub fn field_is_indexed(&self, name: &str) -> bool {
let field = self.find_field(name).unwrap();
if self.field_is_primary(field.name()) || self.field_is_unique(field.name()) {
return true;
}
let is_first_in_index = self.indices.iter().any(|index| {
index
.fields
.iter()
.flat_map(|f| &f.path)
.last()
.map(|(field_name, _)| field_name == name)
.unwrap_or(false)
});
let is_first_in_primary_key = matches!(&self.primary_key, Some(PrimaryKeyDefinition{ fields, ..}) if fields.first().unwrap().name == name);
is_first_in_index || is_first_in_primary_key
}
/// Determines whether there is a singular primary key
pub fn has_single_id_field(&self) -> bool {
matches!(&self.primary_key, Some(PrimaryKeyDefinition{fields, ..}) if fields.len() ==1)
}
pub fn add_index(&mut self, index: IndexDefinition) {
self.indices.push(index)
}
pub fn has_created_at_and_updated_at(&self) -> bool {
/// Finds a field by name.
fn has_field(model: &Model, name: &str) -> bool {
match model
.find_scalar_field(name)
.or_else(|| model.find_scalar_field(name.to_lowercase().as_ref()))
{
Some(f) => f.field_type.is_datetime(),
None => false,
}
}
has_field(self, "createdAt") && has_field(self, "updatedAt")
}
pub fn field_is_unique(&self, name: &str) -> bool {
self.indices.iter().any(|i| {
let names_match = i
.fields
.iter()
.flat_map(|f| &f.path)
.last()
.map(|(field_name, _)| field_name == name)
.unwrap_or(false);
i.is_unique() && i.fields.len() == 1 && names_match
})
}
pub fn field_is_unique_and_defined_on_field(&self, name: &str) -> bool {
self.indices.iter().any(|i| {
let names_match = i
.fields
.iter()
.flat_map(|f| &f.path)
.last()
.map(|(field_name, _)| field_name == name)
.unwrap_or(false);
i.is_unique() && i.fields.len() == 1 && names_match && i.defined_on_field
})
}
pub fn field_is_primary(&self, field_name: &str) -> bool {
matches!(&self.primary_key, Some(pk) if pk.fields.len() == 1 && pk.fields.first().unwrap().name == field_name)
}
pub fn field_is_primary_and_defined_on_field(&self, field_name: &str) -> bool {
matches!(&self.primary_key, Some(PrimaryKeyDefinition{ fields, defined_on_field , ..}) if fields.len() == 1 && fields.first().unwrap().name == field_name && *defined_on_field)
}
pub fn field_is_auto_generated_int_id(&self, name: &str) -> bool {
let field = self.find_scalar_field(name).unwrap();
let is_autogenerated_id = matches!(field.default_value.as_ref().map(|val| val.kind()), Some(DefaultKind::Expression(_)) if self.field_is_primary(name));
let is_an_int = matches!(field.field_type, FieldType::Scalar(ScalarType::Int, _));
is_autogenerated_id && is_an_int
}
}
impl WithName for Model {
fn name(&self) -> &String {
&self.name
}
fn set_name(&mut self, name: &str) {
self.name = String::from(name)
}
}
impl WithDatabaseName for Model {
fn database_name(&self) -> Option<&str> {
self.database_name.as_deref()
}
fn set_database_name(&mut self, database_name: Option<String>) {
self.database_name = database_name;
}
}
impl Ignorable for Model {
fn is_ignored(&self) -> bool {
self.is_ignored
}
fn ignore(&mut self) {
self.is_ignored = true;
}
}