dinoco_compiler 1.2.1

The Dinoco schema compiler for parsing, validating, and analyzing database schemas.
Documentation
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
use std::collections::HashSet;

use pest::Parser;
use pest::iterators::Pair;
use pest_derive::Parser;

use crate::ast::{
    Attribute, AttributeArgument, AttributeValue, ConfigBlock, ConfigEntry, ConfigValue, EnumDef, FieldType, Model,
    ModelField, Schema, SchemaItem, WorkspaceConfig,
};
use crate::error::CompileError;

pub type CompileResult<T> = Result<T, CompileError>;

#[derive(Parser)]
#[grammar = "schema.pest"]
struct DinocoPestParser;

pub fn parse_schema(source: &str) -> CompileResult<Schema> {
    let mut pairs = DinocoPestParser::parse(Rule::schema, source).map_err(CompileError::from)?;
    let schema = pairs.next().expect("schema pair");
    let mut items = Vec::new();

    for pair in schema.into_inner() {
        match pair.as_rule() {
            Rule::config_block => items.push(SchemaItem::Config(parse_config(pair)?)),
            Rule::enum_block => items.push(SchemaItem::Enum(parse_enum(pair)?)),
            Rule::model_block => items.push(SchemaItem::Model(parse_model(pair)?)),
            Rule::EOI => {}
            _ => return Err(pair_error(&pair, "unexpected schema item")),
        }
    }

    Ok(Schema { items })
}

fn parse_config(pair: Pair<'_, Rule>) -> CompileResult<ConfigBlock> {
    let mut entries = Vec::new();
    let mut workspaces = Vec::new();

    for pair in pair.into_inner() {
        match pair.as_rule() {
            Rule::config_entry => entries.push(parse_config_entry(pair)?),
            Rule::workspace_block => {
                for workspace in pair.into_inner() {
                    if workspace.as_rule() == Rule::workspace_entry {
                        workspaces.push(parse_workspace(workspace)?);
                    }
                }
            }
            _ => return Err(pair_error(&pair, "unexpected config item")),
        }
    }

    Ok(ConfigBlock { entries, workspaces })
}

fn parse_workspace(pair: Pair<'_, Rule>) -> CompileResult<WorkspaceConfig> {
    let mut inner = pair.into_inner();
    let name = expect_rule(&mut inner, Rule::ident, "expected workspace name")?.as_str().to_string();
    let entries = inner
        .filter(|pair| pair.as_rule() == Rule::config_entry)
        .map(parse_config_entry)
        .collect::<CompileResult<Vec<_>>>()?;
    Ok(WorkspaceConfig { name, entries })
}

fn parse_config_entry(pair: Pair<'_, Rule>) -> CompileResult<ConfigEntry> {
    let mut inner = pair.into_inner();
    let key = expect_rule(&mut inner, Rule::ident, "expected config key")?.as_str().to_string();
    let value = parse_config_value(expect_rule(&mut inner, Rule::config_value, "expected config value")?)?;

    Ok(ConfigEntry { key, value })
}

pub(crate) fn validate_schema(schema: &Schema) -> CompileResult<()> {
    validate_declarations(schema)?;
    validate_config_values(schema)?;
    validate_model_attributes(schema)?;
    validate_index_attributes(schema)?;
    validate_relation_attributes(schema)?;
    validate_relation_pairs(schema)?;
    validate_primary_keys(schema)?;

    let mut uses_snowflake = false;

    for model in schema.models() {
        for field in &model.fields {
            let Some(default) = field.attributes.iter().find(|attribute| attribute.name == "default") else {
                continue;
            };
            let Some(AttributeArgument::Value(value)) = default.arguments.first() else {
                continue;
            };

            if let AttributeValue::Call { name, .. } = value {
                match name.as_str() {
                    "uuid" if field.ty.name != "String" => {
                        return Err(CompileError::new("uuid() defaults are only supported for String fields", 1, 1));
                    }
                    "snowflake" if field.ty.name != "Integer" => {
                        return Err(CompileError::new(
                            "snowflake() defaults are only supported for Integer fields",
                            1,
                            1,
                        ));
                    }
                    "autoincrement" if field.ty.name != "Integer" => {
                        return Err(CompileError::new(
                            "autoincrement() defaults are only supported for Integer fields",
                            1,
                            1,
                        ));
                    }
                    "now" if field.ty.name != "DateTime" && field.ty.name != "Date" => {
                        return Err(CompileError::new(
                            "now() defaults are only supported for DateTime or Date fields",
                            1,
                            1,
                        ));
                    }
                    "snowflake" => uses_snowflake = true,
                    "uuid" | "autoincrement" | "now" => {}
                    _ => {
                        return Err(CompileError::new(
                            "unsupported @default() function. Supported: autoincrement(), uuid(), snowflake(), now()",
                            1,
                            1,
                        ));
                    }
                }
            }
        }
    }

    if uses_snowflake {
        let configs = schema.config().into_iter().flat_map(config_entry_scopes).collect::<Vec<_>>();
        if configs.is_empty() {
            return Err(CompileError::new("snowflake() requires config.snowflake_node_id = env(\"...\")", 1, 1));
        }
        for (scope, entries) in configs {
            let has_node_id = entries
                .iter()
                .any(|entry| entry.key == "snowflake_node_id" && matches!(entry.value, ConfigValue::Env(_)));
            if !has_node_id {
                return Err(CompileError::new(
                    format!("snowflake() requires {scope}.snowflake_node_id = env(\"...\")"),
                    1,
                    1,
                ));
            }
        }
    }

    Ok(())
}

fn validate_declarations(schema: &Schema) -> CompileResult<()> {
    const SCALAR_TYPES: [&str; 7] = ["String", "Boolean", "Integer", "Float", "DateTime", "Date", "Json"];

    if schema.items.iter().filter(|item| matches!(item, SchemaItem::Config(_))).count() > 1 {
        return schema_error("Only one `config` block is allowed");
    }

    for config in schema.items.iter().filter_map(|item| match item {
        SchemaItem::Config(config) => Some(config),
        _ => None,
    }) {
        let mut keys = HashSet::new();
        for entry in &config.entries {
            if !keys.insert(entry.key.as_str()) {
                return schema_error(format!("Config key `{}` is declared more than once", entry.key));
            }
        }

        if !config.entries.is_empty() && !config.workspaces.is_empty() {
            return schema_error(
                "A `config` block cannot mix top-level database settings with `workspace` settings; remove the top-level `database` and `database_url` entries",
            );
        }

        let mut workspace_names = HashSet::new();
        for workspace in &config.workspaces {
            if !workspace_names.insert(workspace.name.as_str()) {
                return schema_error(format!("Workspace `{}` is declared more than once", workspace.name));
            }
            let mut keys = HashSet::new();
            for entry in &workspace.entries {
                if !keys.insert(entry.key.as_str()) {
                    return schema_error(format!(
                        "Config key `{}` is declared more than once in workspace `{}`",
                        entry.key, workspace.name
                    ));
                }
            }
        }
    }

    let mut type_names = HashSet::new();
    for item in &schema.items {
        let name = match item {
            SchemaItem::Enum(item) => Some(item.name.as_str()),
            SchemaItem::Model(item) => Some(item.name.as_str()),
            SchemaItem::Config(_) => None,
        };
        if let Some(name) = name {
            if SCALAR_TYPES.contains(&name) {
                return schema_error(format!("Type `{name}` conflicts with a built-in scalar type"));
            }
            if !type_names.insert(name) {
                return schema_error(format!("Type `{name}` is declared more than once"));
            }
        }
    }

    for item in schema.enums() {
        if item.values.is_empty() {
            return schema_error(format!("Enum `{}` must contain at least one value", item.name));
        }
        let mut values = HashSet::new();
        for value in &item.values {
            if !values.insert(value.as_str()) {
                return schema_error(format!("Enum value `{}.{value}` is declared more than once", item.name));
            }
        }
    }

    for model in schema.models() {
        if model.fields.is_empty() {
            return schema_error(format!("Model `{}` must contain at least one field", model.name));
        }
        let mut field_names = HashSet::new();
        for field in &model.fields {
            if is_rust_keyword(&field.name) {
                return schema_error(format!(
                    "Field `{}.{}` conflicts with a reserved Rust keyword; rename it (for example, use `_{}`)",
                    model.name, field.name, field.name
                ));
            }
            if !field_names.insert(field.name.as_str()) {
                return schema_error(format!("Field `{}.{}` is declared more than once", model.name, field.name));
            }

            let scalar = SCALAR_TYPES.contains(&field.ty.name.as_str());
            let enum_type = schema.enums().any(|item| item.name == field.ty.name);
            let model_type = schema.models().any(|item| item.name == field.ty.name);
            if !scalar && !enum_type && !model_type {
                return schema_error(format!(
                    "Field `{}.{}` uses unknown type `{}`",
                    model.name, field.name, field.ty.name
                ));
            }
            if field.ty.list && !model_type {
                return schema_error(format!(
                    "Field `{}.{}` is a list of `{}`, but only model relation fields may be lists",
                    model.name, field.name, field.ty.name
                ));
            }
            if field.ty.list && field.ty.optional {
                return schema_error(format!(
                    "Relation list `{}.{}` cannot be optional; remove `?` from the field type",
                    model.name, field.name
                ));
            }

            let mut attributes = HashSet::new();
            for attribute in &field.attributes {
                if matches!(attribute.name.as_str(), "id" | "unique" | "default" | "relation" | "index" | "fulltext")
                    && !attributes.insert(attribute.name.as_str())
                {
                    return schema_error(format!(
                        "Field `{}.{}` declares @{} more than once",
                        model.name, field.name, attribute.name
                    ));
                }
            }
        }
    }

    Ok(())
}

fn validate_index_attributes(schema: &Schema) -> CompileResult<()> {
    let mut mapped_names = HashSet::new();

    for model in schema.models() {
        let composite_indexes = model
            .attributes("indexes")
            .flat_map(|attribute| attribute.field_names().unwrap_or_default())
            .collect::<HashSet<_>>();
        let composite_fulltexts = model
            .attributes("fulltexts")
            .flat_map(|attribute| attribute.field_names().unwrap_or_default())
            .collect::<HashSet<_>>();
        if let Some(field) = composite_indexes.intersection(&composite_fulltexts).next() {
            return schema_error(format!(
                "Field `{}.{field}` cannot combine @index and @fulltext (including @@indexes/@@fulltexts) because both \
                 declare an index",
                model.name
            ));
        }

        for field in &model.fields {
            if let Some(fulltext) = field.attributes.iter().find(|attribute| attribute.name == "fulltext") {
                if field.ty.name != "String" || field.ty.list || field.is_relation(schema) {
                    return schema_error(format!(
                        "Field `{}.{}` uses @fulltext, but full-text search is only supported on String fields",
                        model.name, field.name
                    ));
                }
                if !fulltext.arguments.is_empty() {
                    return schema_error(format!(
                        "@fulltext on `{}.{}` does not accept arguments",
                        model.name, field.name
                    ));
                }
                if field.attributes.iter().any(|attribute| attribute.name == "index")
                    || composite_indexes.contains(field.name.as_str())
                {
                    return schema_error(format!(
                        "Field `{}.{}` cannot combine @index and @fulltext (including @@indexes/@@fulltexts) because \
                         both declare an index",
                        model.name, field.name
                    ));
                }
            }

            if composite_fulltexts.contains(field.name.as_str())
                && field.attributes.iter().any(|attribute| attribute.name == "index")
            {
                return schema_error(format!(
                    "Field `{}.{}` cannot combine @index and @fulltext (including @@indexes/@@fulltexts) because both \
                     declare an index",
                    model.name, field.name
                ));
            }

            let Some(index) = field.attributes.iter().find(|attribute| attribute.name == "index") else {
                continue;
            };

            if field.is_relation(schema) || field.ty.list {
                return schema_error(format!(
                    "Field `{}.{}` uses @index, but indexes must be declared on scalar or enum fields",
                    model.name, field.name
                ));
            }

            if index.arguments.len() > 1 {
                return schema_error(format!(
                    "@index on `{}.{}` accepts only the optional `map` argument",
                    model.name, field.name
                ));
            }

            let Some(argument) = index.arguments.first() else {
                continue;
            };
            let AttributeArgument::Named { key, value } = argument else {
                return schema_error(format!(
                    "@index on `{}.{}` accepts only `map: \"index_name\"`",
                    model.name, field.name
                ));
            };
            if key != "map" {
                return schema_error(format!(
                    "@index on `{}.{}` does not support `{key}`; use `map: \"index_name\"`",
                    model.name, field.name
                ));
            }
            let Some(name) = attribute_string_or_ident(value) else {
                return schema_error(format!(
                    "@index map on `{}.{}` must be a string or identifier",
                    model.name, field.name
                ));
            };
            if name.trim().is_empty() {
                return schema_error(format!("@index map on `{}.{}` cannot be empty", model.name, field.name));
            }
            if !mapped_names.insert(name) {
                return schema_error(format!("Index name `{name}` is declared more than once"));
            }
        }
    }

    Ok(())
}

fn validate_model_attributes(schema: &Schema) -> CompileResult<()> {
    for model in schema.models() {
        let known = ["ids", "uniques", "indexes", "fulltexts", "table_name"];
        for attribute in &model.attributes {
            if !known.contains(&attribute.name.as_str()) {
                return schema_error(format!(
                    "Unknown model attribute `@@{}` on model `{}`",
                    attribute.name, model.name
                ));
            }
        }

        let ids = model.attributes("ids").collect::<Vec<_>>();
        if ids.len() > 1 {
            return schema_error(format!(
                "Model `{}` declares @@ids more than once; exactly one primary key is allowed",
                model.name
            ));
        }
        if model.attributes("table_name").count() > 1 {
            return schema_error(format!("Model `{}` declares @@table_name more than once", model.name));
        }

        if let Some(table_name) = model.attribute("table_name") {
            let [AttributeArgument::Value(AttributeValue::String(name))] = table_name.arguments.as_slice() else {
                return schema_error(format!(
                    "@@table_name on model `{}` requires exactly one non-empty string",
                    model.name
                ));
            };
            if name.trim().is_empty() {
                return schema_error(format!("@@table_name on model `{}` cannot be empty", model.name));
            }
        }

        let mut fulltext_fields = model
            .fields
            .iter()
            .filter(|field| field.attributes.iter().any(|attribute| attribute.name == "fulltext"))
            .map(|field| field.name.as_str())
            .collect::<HashSet<_>>();
        let mut declarations = HashSet::new();

        for attribute in &model.attributes {
            if !matches!(attribute.name.as_str(), "ids" | "uniques" | "indexes" | "fulltexts") {
                continue;
            }

            let Some(fields) = attribute.field_names() else {
                return schema_error(format!(
                    "@@{} on model `{}` requires exactly one array of field identifiers",
                    attribute.name, model.name
                ));
            };
            if fields.is_empty() {
                return schema_error(format!("@@{} on model `{}` cannot be empty", attribute.name, model.name));
            }

            let mut seen = HashSet::new();
            for name in &fields {
                if !seen.insert(*name) {
                    return schema_error(format!(
                        "@@{} on model `{}` contains duplicate field `{name}`",
                        attribute.name, model.name
                    ));
                }
                let Some(field) = model.fields.iter().find(|field| field.name == *name) else {
                    return schema_error(format!(
                        "@@{} on model `{}` refers to missing field `{name}`",
                        attribute.name, model.name
                    ));
                };
                if field.ty.list || field.is_relation(schema) {
                    return schema_error(format!(
                        "@@{} on model `{}` may only contain scalar or enum fields; `{name}` is a relation/list",
                        attribute.name, model.name
                    ));
                }
                if attribute.name == "ids" && field.ty.optional {
                    return schema_error(format!(
                        "Composite primary key field `{}.{name}` must be required",
                        model.name
                    ));
                }
                if attribute.name == "fulltexts" && field.ty.name != "String" {
                    return schema_error(format!(
                        "@@fulltexts on model `{}` may only contain String fields; `{name}` is `{}`",
                        model.name, field.ty.name
                    ));
                }
            }

            let signature = format!("{}:{}", attribute.name, fields.join(","));
            if !declarations.insert(signature) {
                return schema_error(format!(
                    "Model `{}` declares @@{}([{}]) more than once",
                    model.name,
                    attribute.name,
                    fields.join(", ")
                ));
            }

            if attribute.name == "fulltexts" {
                for name in fields {
                    if !fulltext_fields.insert(name) {
                        return schema_error(format!(
                            "Field `{}.{name}` belongs to more than one full-text index",
                            model.name
                        ));
                    }
                }
            }
        }
    }

    Ok(())
}

fn validate_primary_keys(schema: &Schema) -> CompileResult<()> {
    for model in schema.models() {
        let field_ids = model
            .fields
            .iter()
            .filter(|field| field.attributes.iter().any(|attribute| attribute.name == "id"))
            .collect::<Vec<_>>();
        let primary_key_declarations = field_ids.len() + model.attributes("ids").count();

        match primary_key_declarations {
            0 => {
                return schema_error(format!(
                    "Model `{}` must declare exactly one primary key using @id or @@ids([...])",
                    model.name
                ));
            }
            1 => {}
            _ => {
                return schema_error(format!(
                    "Model `{}` declares multiple primary keys; use exactly one @id or one @@ids([...])",
                    model.name
                ));
            }
        }

        if let [field] = field_ids.as_slice()
            && (field.ty.optional || field.ty.list || field.is_relation(schema))
        {
            return schema_error(format!(
                "Primary key `{}.{}` must be a required scalar or enum field",
                model.name, field.name
            ));
        }
    }

    Ok(())
}

fn validate_config_values(schema: &Schema) -> CompileResult<()> {
    for config in schema.items.iter().filter_map(|item| match item {
        SchemaItem::Config(config) => Some(config),
        _ => None,
    }) {
        for (scope, entries) in config_entry_scopes(config) {
            validate_config_scope(&scope, entries)?;
        }
    }

    Ok(())
}

fn validate_config_scope(scope: &str, entries: &[ConfigEntry]) -> CompileResult<()> {
    for entry in entries {
        match (entry.key.as_str(), &entry.value) {
            ("database_url", ConfigValue::Env(name)) if !name.trim().is_empty() => {}
            ("database_url", ConfigValue::Env(_)) => {
                return schema_error(format!("`{scope}.database_url` env name cannot be empty"));
            }
            ("database_url", _) => {
                return schema_error(format!("`{scope}.database_url` only accepts env(\"DATABASE_URL\")"));
            }
            ("read_replicas", ConfigValue::Array(values))
                if values.iter().all(|value| matches!(value, ConfigValue::Env(name) if !name.trim().is_empty())) => {}
            ("read_replicas", ConfigValue::Array(_)) => {
                return schema_error(format!("`{scope}.read_replicas` only accepts non-empty env(...) values"));
            }
            ("read_replicas", _) => {
                return schema_error(format!("`{scope}.read_replicas` must be an array of env(...) values"));
            }
            ("snowflake_node_id", ConfigValue::Env(name)) if !name.trim().is_empty() => {}
            ("snowflake_node_id", ConfigValue::Env(_)) => {
                return schema_error(format!("`{scope}.snowflake_node_id` env name cannot be empty"));
            }
            ("snowflake_node_id", _) => {
                return schema_error(format!("`{scope}.snowflake_node_id` only accepts env(...)"));
            }
            ("with_logger", ConfigValue::Boolean(_)) => {}
            ("with_logger", _) => {
                return schema_error(format!("`{scope}.with_logger` must be `true` or `false`"));
            }
            ("min_connection" | "max_connection", ConfigValue::Integer(value)) if *value > 0 => {}
            ("min_connection" | "max_connection", _) => {
                return schema_error(format!("`{scope}.{}` must be a positive integer", entry.key));
            }
            _ => {}
        }
    }

    let database = entries
        .iter()
        .find(|entry| entry.key == "database")
        .ok_or_else(|| CompileError::new(format!("`{scope}.database` is required"), 1, 1))?;
    let database = match &database.value {
        ConfigValue::String(value) | ConfigValue::Ident(value)
            if matches!(value.as_str(), "postgresql" | "postgres" | "mysql" | "sqlite") =>
        {
            value.as_str()
        }
        _ => {
            return schema_error(format!("`{scope}.database` must be `postgresql`, `mysql`, or `sqlite`"));
        }
    };

    if !entries.iter().any(|entry| entry.key == "database_url") {
        return schema_error(format!("`{scope}.database_url = env(\"DATABASE_URL\")` is required"));
    }

    let connection = entries.iter().find(|entry| entry.key == "connection");
    let connection = match connection.map(|entry| &entry.value) {
        None => "direct",
        Some(ConfigValue::String(value) | ConfigValue::Ident(value))
            if matches!(value.as_str(), "direct" | "pgbouncer") =>
        {
            value
        }
        Some(_) => {
            return schema_error(format!("`{scope}.connection` must be `direct` or `pgbouncer`"));
        }
    };

    let min_connection = config_positive_integer(entries, "min_connection").unwrap_or(2);
    let max_connection = config_positive_integer(entries, "max_connection").unwrap_or(10);
    let configures_pool = entries.iter().any(|entry| matches!(entry.key.as_str(), "min_connection" | "max_connection"));
    if configures_pool && !(matches!(database, "postgresql" | "postgres") && connection == "direct") {
        return schema_error(format!(
            "`{scope}.min_connection` and `{scope}.max_connection` are supported only for PostgreSQL with `connection = \"direct\"`"
        ));
    }
    if min_connection > max_connection {
        return schema_error(format!(
            "`{scope}.min_connection` ({min_connection}) cannot be greater than `{scope}.max_connection` ({max_connection})"
        ));
    }

    Ok(())
}

fn config_positive_integer(entries: &[ConfigEntry], key: &str) -> Option<usize> {
    entries.iter().find(|entry| entry.key == key).and_then(|entry| match &entry.value {
        ConfigValue::Integer(value) if *value > 0 => usize::try_from(*value).ok(),
        _ => None,
    })
}

fn config_entry_scopes(config: &ConfigBlock) -> Vec<(String, &[ConfigEntry])> {
    if config.workspaces.is_empty() {
        vec![("config".to_string(), config.entries.as_slice())]
    } else {
        config
            .workspaces
            .iter()
            .map(|workspace| (format!("config.workspace.{}", workspace.name), workspace.entries.as_slice()))
            .collect()
    }
}

#[derive(Debug, Clone)]
struct RelationDefinition {
    name: Option<String>,
    fields: Option<Vec<String>>,
    references: Option<Vec<String>>,
    has_map: bool,
    actions: Vec<(String, String)>,
}

impl RelationDefinition {
    fn has_keys(&self) -> bool {
        self.fields.is_some()
    }
}

fn validate_relation_attributes(schema: &Schema) -> CompileResult<()> {
    for model in schema.models() {
        for field in &model.fields {
            let relation_count = field.attributes.iter().filter(|attribute| attribute.name == "relation").count();
            if relation_count > 1 {
                return schema_error(format!(
                    "Relation field `{}.{}` declares @relation more than once",
                    model.name, field.name
                ));
            }
            if relation_count == 0 {
                continue;
            }
            if !field.is_relation(schema) {
                return schema_error(format!(
                    "Field `{}.{}` uses @relation, but `{}` is not a model",
                    model.name, field.name, field.ty.name
                ));
            }

            relation_definition(model, field)?;
        }
    }

    Ok(())
}

fn validate_relation_pairs(schema: &Schema) -> CompileResult<()> {
    for model in schema.models() {
        for (field_index, field) in model.fields.iter().enumerate() {
            if !field.is_relation(schema) {
                continue;
            }

            let target =
                schema.models().find(|candidate| candidate.name == field.ty.name).expect("relation targets are models");
            let definition = relation_definition(model, field)?;
            let relation_name = definition.name.as_deref();
            let candidates = target
                .fields
                .iter()
                .enumerate()
                .filter(|(candidate_index, candidate)| {
                    candidate.ty.name == model.name
                        && (model.name != target.name || *candidate_index != field_index)
                        && field_relation_name(candidate).as_deref() == relation_name
                })
                .map(|(_, candidate)| candidate)
                .collect::<Vec<_>>();

            match candidates.as_slice() {
                [opposite] => {
                    let opposite_definition = relation_definition(target, opposite)?;
                    validate_relation_cardinality(
                        schema,
                        model,
                        field,
                        &definition,
                        target,
                        opposite,
                        &opposite_definition,
                    )?;
                }
                [] => {
                    let relation_suffix =
                        relation_name.map(|name| format!(" using @relation(name: \"{name}\")")).unwrap_or_default();
                    return schema_error(format!(
                        "Relation field `{}.{}` targets model `{}`{relation_suffix}, but `{}` has no compatible \
                         opposite relation field pointing back to `{}`",
                        model.name, field.name, target.name, target.name, model.name
                    ));
                }
                _ => {
                    return schema_error(format!(
                        "Ambiguous relation field `{}.{}`: model `{}` has multiple possible opposite fields \
                         pointing back to `{}`; add matching @relation(name: \"...\") attributes to both sides",
                        model.name, field.name, target.name, model.name
                    ));
                }
            }
        }
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn validate_relation_cardinality(
    schema: &Schema,
    model: &Model,
    field: &ModelField,
    definition: &RelationDefinition,
    target: &Model,
    opposite: &ModelField,
    opposite_definition: &RelationDefinition,
) -> CompileResult<()> {
    if model.name == target.name && definition.name.is_none() {
        return schema_error(format!(
            "Self relation `{}.{}` must declare the same non-empty @relation(name: \"...\") on both sides",
            model.name, field.name
        ));
    }

    match (field.ty.list, opposite.ty.list) {
        (true, true) => {
            if definition.has_keys() || opposite_definition.has_keys() {
                return schema_error(format!(
                    "Many-to-many relation `{}.{}` <-> `{}.{}` cannot declare fields/references; implicit join \
                     relations require two unmapped list fields",
                    model.name, field.name, target.name, opposite.name
                ));
            }
            reject_non_owning_options(model, field, definition)?;
            reject_non_owning_options(target, opposite, opposite_definition)?;
            validate_many_to_many_id(schema, model)?;
            if model.name != target.name {
                validate_many_to_many_id(schema, target)?;
            }
        }
        (true, false) => {
            validate_one_to_many_pair(schema, model, field, definition, target, opposite, opposite_definition)?
        }
        (false, true) => {
            validate_one_to_many_pair(schema, target, opposite, opposite_definition, model, field, definition)?
        }
        (false, false) => {
            validate_one_to_one_pair(schema, model, field, definition, target, opposite, opposite_definition)?
        }
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn validate_one_to_many_pair(
    schema: &Schema,
    list_model: &Model,
    list_field: &ModelField,
    list_definition: &RelationDefinition,
    owner_model: &Model,
    owner_field: &ModelField,
    owner_definition: &RelationDefinition,
) -> CompileResult<()> {
    if !owner_definition.has_keys() {
        return schema_error(format!(
            "One-to-many relation `{}.{}` <-> `{}.{}` requires fields/references on the singular FK-owning side \
             `{}.{}`",
            list_model.name, list_field.name, owner_model.name, owner_field.name, owner_model.name, owner_field.name
        ));
    }

    validate_owner_keys(schema, owner_model, owner_field, list_model, owner_definition)?;
    if relation_is_unique(owner_model, owner_field, owner_definition) {
        return schema_error(format!(
            "Relation `{}.{}` is unique, so its opposite `{}.{}` must be singular instead of a list",
            owner_model.name, owner_field.name, list_model.name, list_field.name
        ));
    }

    reject_non_owning_options(list_model, list_field, list_definition)?;
    if list_definition.has_keys() {
        let list_fields = list_definition.fields.as_deref().expect("checked relation fields");
        let list_references = list_definition.references.as_deref().expect("checked relation references");
        let owner_fields = owner_definition.fields.as_deref().expect("checked owner fields");
        let owner_references = owner_definition.references.as_deref().expect("checked owner references");
        if list_fields != owner_references || list_references != owner_fields {
            return schema_error(format!(
                "List relation `{}.{}` must mirror the owning side: expected fields: [{}], references: [{}]",
                list_model.name,
                list_field.name,
                owner_references.join(", "),
                owner_fields.join(", ")
            ));
        }
        validate_key_field_names(schema, list_model, list_field, owner_model, list_definition, false)?;
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn validate_one_to_one_pair(
    schema: &Schema,
    model: &Model,
    field: &ModelField,
    definition: &RelationDefinition,
    target: &Model,
    opposite: &ModelField,
    opposite_definition: &RelationDefinition,
) -> CompileResult<()> {
    let (owner_model, owner_field, owner_definition, inverse_model, inverse_field, inverse_definition) =
        match (definition.has_keys(), opposite_definition.has_keys()) {
            (true, false) => (model, field, definition, target, opposite, opposite_definition),
            (false, true) => (target, opposite, opposite_definition, model, field, definition),
            (false, false) => {
                return schema_error(format!(
                    "One-to-one relation `{}.{}` <-> `{}.{}` requires fields/references on exactly one FK-owning side",
                    model.name, field.name, target.name, opposite.name
                ));
            }
            (true, true) => {
                return schema_error(format!(
                    "One-to-one relation `{}.{}` <-> `{}.{}` declares fields/references on both sides; only one side \
                     may own the foreign key",
                    model.name, field.name, target.name, opposite.name
                ));
            }
        };

    validate_owner_keys(schema, owner_model, owner_field, inverse_model, owner_definition)?;
    reject_non_owning_options(inverse_model, inverse_field, inverse_definition)?;
    if owner_field.attributes.iter().any(|attribute| attribute.name == "unique")
        && owner_definition.fields.as_ref().is_some_and(|fields| fields.len() != 1)
    {
        return schema_error(format!(
            "Composite one-to-one relation `{}.{}` cannot place @unique on the relation field; declare \
             @@uniques([...]) for the complete local foreign-key tuple",
            owner_model.name, owner_field.name
        ));
    }
    if !relation_is_unique(owner_model, owner_field, owner_definition) {
        return schema_error(format!(
            "One-to-one relation `{}.{}` requires @unique on the relation field or its local foreign-key field",
            owner_model.name, owner_field.name
        ));
    }
    if !inverse_field.ty.optional {
        return schema_error(format!(
            "The non-owning side `{}.{}` of a one-to-one relation must be optional because it has no local foreign key",
            inverse_model.name, inverse_field.name
        ));
    }

    Ok(())
}

fn validate_owner_keys(
    schema: &Schema,
    model: &Model,
    field: &ModelField,
    target: &Model,
    definition: &RelationDefinition,
) -> CompileResult<()> {
    let local_fields = validate_key_field_names(schema, model, field, target, definition, true)?;
    let relation_is_optional = local_fields.iter().any(|local| local.ty.optional);
    if field.ty.optional != relation_is_optional {
        return schema_error(format!(
            "Relation `{}.{}` optionality must match its local foreign key: use `{}` because fields [{}] are {}",
            model.name,
            field.name,
            if relation_is_optional { format!("{}?", field.ty.name) } else { field.ty.name.clone() },
            definition.fields.as_deref().unwrap_or_default().join(", "),
            if relation_is_optional { "nullable" } else { "required" }
        ));
    }

    for (action_name, action) in &definition.actions {
        if action == "SetNull" && local_fields.iter().any(|local| !local.ty.optional) {
            return schema_error(format!(
                "{action_name}: SetNull on `{}.{}` requires every local foreign-key field to be optional",
                model.name, field.name
            ));
        }
        if action == "SetDefault"
            && local_fields.iter().any(|local| !local.attributes.iter().any(|attribute| attribute.name == "default"))
        {
            return schema_error(format!(
                "{action_name}: SetDefault on `{}.{}` requires every local foreign-key field to define @default(...)",
                model.name, field.name
            ));
        }
    }

    Ok(())
}

fn validate_key_field_names<'a>(
    schema: &Schema,
    model: &'a Model,
    field: &ModelField,
    target: &Model,
    definition: &RelationDefinition,
    require_unique_reference: bool,
) -> CompileResult<Vec<&'a ModelField>> {
    let fields = definition.fields.as_deref().expect("relation key fields were checked");
    let references = definition.references.as_deref().expect("relation key references were checked");
    if fields.len() != references.len() {
        return schema_error(format!(
            "Relation `{}.{}` must have the same number of fields ({}) and references ({})",
            model.name,
            field.name,
            fields.len(),
            references.len()
        ));
    }

    let mut local_fields = Vec::with_capacity(fields.len());
    for (local_name, reference_name) in fields.iter().zip(references) {
        let Some(local) = model.fields.iter().find(|candidate| candidate.name == *local_name) else {
            return schema_error(format!(
                "Relation `{}.{}` refers to missing local field `{}.{local_name}`",
                model.name, field.name, model.name
            ));
        };
        let Some(reference) = target.fields.iter().find(|candidate| candidate.name == *reference_name) else {
            return schema_error(format!(
                "Relation `{}.{}` refers to missing target field `{}.{reference_name}`",
                model.name, field.name, target.name
            ));
        };
        if local.ty.list || local.is_relation(schema) {
            return schema_error(format!(
                "Relation key `{}.{}` must be a scalar or enum field, not a relation/list field",
                model.name, local.name
            ));
        }
        if reference.ty.list || reference.is_relation(schema) {
            return schema_error(format!(
                "Referenced key `{}.{}` must be a scalar or enum field, not a relation/list field",
                target.name, reference.name
            ));
        }
        if local.ty.name != reference.ty.name {
            return schema_error(format!(
                "Relation `{}.{}` has incompatible key types: `{}.{}` is `{}`, but `{}.{}` is `{}`",
                model.name,
                field.name,
                model.name,
                local.name,
                local.ty.name,
                target.name,
                reference.name,
                reference.ty.name
            ));
        }
        local_fields.push(local);
    }

    if require_unique_reference && !model_has_unique_key(target, references) {
        return schema_error(format!(
            "Relation `{}.{}` references `{}.[{}]`, which must declare @id or @unique, or match @@ids([...]) or \
             @@uniques([...])",
            model.name,
            field.name,
            target.name,
            references.join(", ")
        ));
    }

    Ok(local_fields)
}

fn reject_non_owning_options(model: &Model, field: &ModelField, definition: &RelationDefinition) -> CompileResult<()> {
    if definition.has_map || !definition.actions.is_empty() {
        return schema_error(format!(
            "Relation `{}.{}` does not own a foreign key, so map/onDelete/onUpdate must be declared on the singular \
             side with fields/references",
            model.name, field.name
        ));
    }
    Ok(())
}

fn validate_many_to_many_id(schema: &Schema, model: &Model) -> CompileResult<()> {
    let ids = model
        .fields
        .iter()
        .filter(|field| field.attributes.iter().any(|attribute| attribute.name == "id"))
        .collect::<Vec<_>>();
    if ids.len() != 1 || ids[0].ty.list || ids[0].ty.optional || ids[0].is_relation(schema) {
        return schema_error(format!(
            "Implicit many-to-many relations require model `{}` to have exactly one scalar @id field",
            model.name
        ));
    }
    Ok(())
}

fn relation_is_unique(model: &Model, field: &ModelField, definition: &RelationDefinition) -> bool {
    field.attributes.iter().any(|attribute| attribute.name == "unique")
        || definition.fields.as_ref().is_some_and(|fields| model_has_unique_key(model, fields))
}

fn model_has_unique_key(model: &Model, fields: &[String]) -> bool {
    if fields.len() == 1
        && model.fields.iter().find(|field| field.name == fields[0]).is_some_and(|field| {
            field.attributes.iter().any(|attribute| matches!(attribute.name.as_str(), "id" | "unique"))
        })
    {
        return true;
    }
    if fields.iter().all(|name| {
        model.fields.iter().find(|field| field.name == *name).is_some_and(|field| {
            field.attributes.iter().any(|attribute| matches!(attribute.name.as_str(), "id" | "unique"))
        })
    }) {
        return true;
    }

    model
        .attributes
        .iter()
        .filter(|attribute| matches!(attribute.name.as_str(), "ids" | "uniques"))
        .filter_map(Attribute::field_names)
        .any(|candidate| candidate.iter().copied().eq(fields.iter().map(String::as_str)))
}

fn relation_definition(model: &Model, field: &ModelField) -> CompileResult<RelationDefinition> {
    let Some(relation) = field.attributes.iter().find(|attribute| attribute.name == "relation") else {
        return Ok(RelationDefinition {
            name: None,
            fields: None,
            references: None,
            has_map: false,
            actions: Vec::new(),
        });
    };

    let mut named = HashSet::new();
    let mut positional_name = None;
    for argument in &relation.arguments {
        match argument {
            AttributeArgument::Named { key, .. } => {
                if !matches!(key.as_str(), "name" | "fields" | "references" | "onDelete" | "onUpdate" | "map") {
                    return schema_error(format!(
                        "Unknown @relation argument `{key}` on `{}.{}`",
                        model.name, field.name
                    ));
                }
                if !named.insert(key.as_str()) {
                    return schema_error(format!(
                        "Duplicate @relation argument `{key}` on `{}.{}`",
                        model.name, field.name
                    ));
                }
            }
            AttributeArgument::Value(value) => {
                if positional_name.is_some() {
                    return schema_error(format!(
                        "Relation `{}.{}` accepts at most one positional name",
                        model.name, field.name
                    ));
                }
                positional_name = Some(non_empty_string_or_ident(value, "relation name", model, field)?);
            }
        }
    }

    let named_name = relation
        .argument("name")
        .map(|value| non_empty_string_or_ident(value, "relation name", model, field))
        .transpose()?;
    if positional_name.is_some() && named_name.is_some() {
        return schema_error(format!(
            "Relation `{}.{}` declares its name both positionally and with `name:`",
            model.name, field.name
        ));
    }
    let fields = relation
        .argument("fields")
        .map(|value| relation_identifier_array(value, "fields", model, field))
        .transpose()?;
    let references = relation
        .argument("references")
        .map(|value| relation_identifier_array(value, "references", model, field))
        .transpose()?;
    if fields.is_some() != references.is_some() {
        return schema_error(format!(
            "Relation `{}.{}` must declare `fields` and `references` together",
            model.name, field.name
        ));
    }
    if fields.as_ref().is_some_and(|fields| fields.len() != references.as_ref().map_or(0, Vec::len)) {
        return schema_error(format!(
            "Relation `{}.{}` must have the same number of fields and references",
            model.name, field.name
        ));
    }

    if let Some(value) = relation.argument("map") {
        non_empty_string_or_ident(value, "constraint map", model, field)?;
    }

    let mut actions = Vec::new();
    for action_name in ["onDelete", "onUpdate"] {
        let Some(value) = relation.argument(action_name) else {
            continue;
        };
        let action = non_empty_string_or_ident(value, action_name, model, field)?;
        if !matches!(action.as_str(), "Cascade" | "Restrict" | "NoAction" | "SetNull" | "SetDefault") {
            return schema_error(format!(
                "{action_name} on `{}.{}` must be one of: Cascade, Restrict, NoAction, SetNull, SetDefault",
                model.name, field.name
            ));
        }
        actions.push((action_name.to_string(), action));
    }

    Ok(RelationDefinition {
        name: named_name.or(positional_name),
        fields,
        references,
        has_map: relation.argument("map").is_some(),
        actions,
    })
}

fn relation_identifier_array(
    value: &AttributeValue,
    argument: &str,
    model: &Model,
    field: &ModelField,
) -> CompileResult<Vec<String>> {
    let AttributeValue::Array(values) = value else {
        return schema_error(format!(
            "@relation({argument}: ...) on `{}.{}` must be a non-empty array of field identifiers",
            model.name, field.name
        ));
    };
    if values.is_empty() {
        return schema_error(format!("@relation({argument}: ...) on `{}.{}` cannot be empty", model.name, field.name));
    }

    let mut seen = HashSet::new();
    let mut names = Vec::with_capacity(values.len());
    for value in values {
        let AttributeValue::Ident(name) = value else {
            return schema_error(format!(
                "@relation({argument}: ...) on `{}.{}` only accepts field identifiers",
                model.name, field.name
            ));
        };
        if !seen.insert(name.as_str()) {
            return schema_error(format!(
                "@relation({argument}: ...) on `{}.{}` contains duplicate field `{name}`",
                model.name, field.name
            ));
        }
        names.push(name.clone());
    }
    Ok(names)
}

fn non_empty_string_or_ident(
    value: &AttributeValue,
    label: &str,
    model: &Model,
    field: &ModelField,
) -> CompileResult<String> {
    let Some(value) = attribute_string_or_ident(value) else {
        return schema_error(format!("{label} on `{}.{}` must be a string or identifier", model.name, field.name));
    };
    if value.trim().is_empty() {
        return schema_error(format!("{label} on `{}.{}` cannot be empty", model.name, field.name));
    }
    Ok(value.to_string())
}

fn schema_error<T>(message: impl Into<String>) -> CompileResult<T> {
    Err(CompileError::new(message, 1, 1))
}

fn is_rust_keyword(name: &str) -> bool {
    matches!(
        name,
        "Self"
            | "abstract"
            | "as"
            | "async"
            | "await"
            | "become"
            | "box"
            | "break"
            | "const"
            | "continue"
            | "crate"
            | "do"
            | "dyn"
            | "else"
            | "enum"
            | "extern"
            | "false"
            | "final"
            | "fn"
            | "for"
            | "gen"
            | "if"
            | "impl"
            | "in"
            | "let"
            | "loop"
            | "macro"
            | "match"
            | "mod"
            | "move"
            | "mut"
            | "override"
            | "priv"
            | "pub"
            | "ref"
            | "return"
            | "self"
            | "static"
            | "struct"
            | "super"
            | "trait"
            | "true"
            | "try"
            | "type"
            | "typeof"
            | "unsafe"
            | "unsized"
            | "use"
            | "virtual"
            | "where"
            | "while"
            | "yield"
    )
}

fn field_relation_name(field: &ModelField) -> Option<String> {
    let relation = field.attributes.iter().find(|attribute| attribute.name == "relation")?;
    relation.argument("name").and_then(attribute_string_or_ident).map(str::to_string).or_else(|| {
        relation.arguments.iter().find_map(|argument| match argument {
            AttributeArgument::Value(value) => attribute_string_or_ident(value).map(str::to_string),
            AttributeArgument::Named { .. } => None,
        })
    })
}

fn attribute_string_or_ident(value: &AttributeValue) -> Option<&str> {
    match value {
        AttributeValue::String(value) | AttributeValue::Ident(value) => Some(value),
        _ => None,
    }
}

fn parse_config_value(pair: Pair<'_, Rule>) -> CompileResult<ConfigValue> {
    let error = pair_position_error(&pair, "expected config value");
    let value = pair.into_inner().next().ok_or(error)?;

    match value.as_rule() {
        Rule::config_array => {
            let values = value.into_inner().map(parse_config_value).collect::<CompileResult<Vec<_>>>()?;
            Ok(ConfigValue::Array(values))
        }
        Rule::env_call => {
            let error = pair_position_error(&value, "expected env name");
            let raw = value.into_inner().find(|pair| pair.as_rule() == Rule::string_literal).ok_or(error)?;
            Ok(ConfigValue::Env(unquote(raw.as_str())))
        }
        Rule::string_literal => Ok(ConfigValue::String(unquote(value.as_str()))),
        Rule::boolean_literal => Ok(ConfigValue::Boolean(value.as_str() == "true")),
        Rule::number_literal => value
            .as_str()
            .parse::<i64>()
            .map(ConfigValue::Integer)
            .map_err(|_| pair_error(&value, "config numbers must be integers")),
        Rule::ident => Ok(ConfigValue::Ident(value.as_str().to_string())),
        _ => Err(pair_error(&value, "unexpected config value")),
    }
}

fn parse_enum(pair: Pair<'_, Rule>) -> CompileResult<EnumDef> {
    let mut inner = pair.into_inner();
    let name = expect_rule(&mut inner, Rule::ident, "expected enum name")?.as_str().to_string();
    let values = inner
        .filter(|pair| pair.as_rule() == Rule::enum_value)
        .map(|pair| {
            pair.into_inner()
                .next()
                .map(|pair| pair.as_str().to_string())
                .ok_or_else(|| CompileError::new("expected enum value", 1, 1))
        })
        .collect::<CompileResult<Vec<_>>>()?;

    Ok(EnumDef { name, values })
}

fn parse_model(pair: Pair<'_, Rule>) -> CompileResult<Model> {
    let mut inner = pair.into_inner();
    let name = expect_rule(&mut inner, Rule::ident, "expected model name")?.as_str().to_string();
    let mut fields = Vec::new();
    let mut attributes = Vec::new();

    for pair in inner {
        match pair.as_rule() {
            Rule::model_field => fields.push(parse_model_field(pair)?),
            Rule::model_attribute => attributes.push(parse_attribute(pair)?),
            _ => return Err(pair_error(&pair, "unexpected model token")),
        }
    }

    Ok(Model { name, fields, attributes })
}

fn parse_model_field(pair: Pair<'_, Rule>) -> CompileResult<ModelField> {
    let mut inner = pair.into_inner();
    let name = expect_rule(&mut inner, Rule::ident, "expected field name")?.as_str().to_string();
    let ty_name = expect_rule(&mut inner, Rule::field_type, "expected field type")?
        .into_inner()
        .next()
        .ok_or_else(|| CompileError::new("expected field type", 1, 1))?
        .as_str()
        .to_string();
    let mut optional = false;
    let mut list = false;
    let mut attributes = Vec::new();

    for pair in inner {
        match pair.as_rule() {
            Rule::field_optional => optional = true,
            Rule::field_list => list = true,
            Rule::attribute => attributes.push(parse_attribute(pair)?),
            _ => return Err(pair_error(&pair, "unexpected field token")),
        }
    }

    Ok(ModelField { name, ty: FieldType { name: ty_name, optional, list }, attributes })
}

fn parse_attribute(pair: Pair<'_, Rule>) -> CompileResult<Attribute> {
    let mut inner = pair.into_inner();
    let name = expect_rule(&mut inner, Rule::ident, "expected attribute name")?.as_str().to_string();
    let arguments = inner
        .find(|pair| pair.as_rule() == Rule::attribute_arguments)
        .map(parse_attribute_arguments)
        .transpose()?
        .unwrap_or_default();

    Ok(Attribute { name, arguments })
}

fn parse_attribute_arguments(pair: Pair<'_, Rule>) -> CompileResult<Vec<AttributeArgument>> {
    pair.into_inner().filter(|pair| pair.as_rule() == Rule::attribute_argument).map(parse_attribute_argument).collect()
}

fn parse_attribute_argument(pair: Pair<'_, Rule>) -> CompileResult<AttributeArgument> {
    let error = pair_position_error(&pair, "expected attribute argument");
    let pair = pair.into_inner().next().ok_or(error)?;

    match pair.as_rule() {
        Rule::named_argument => {
            let mut inner = pair.into_inner();
            let key = expect_rule(&mut inner, Rule::ident, "expected argument name")?.as_str().to_string();
            let value =
                parse_attribute_value(expect_rule(&mut inner, Rule::attribute_value, "expected argument value")?)?;

            Ok(AttributeArgument::Named { key, value })
        }
        Rule::attribute_value => Ok(AttributeArgument::Value(parse_attribute_value(pair)?)),
        _ => Err(pair_error(&pair, "unexpected attribute argument")),
    }
}

fn parse_attribute_value(pair: Pair<'_, Rule>) -> CompileResult<AttributeValue> {
    let error = pair_position_error(&pair, "expected attribute value");
    let pair = pair.into_inner().next().ok_or(error)?;

    match pair.as_rule() {
        Rule::attribute_array => {
            let values = pair
                .into_inner()
                .filter(|pair| pair.as_rule() == Rule::attribute_value)
                .map(parse_attribute_value)
                .collect::<CompileResult<Vec<_>>>()?;

            Ok(AttributeValue::Array(values))
        }
        Rule::attribute_call => {
            let mut inner = pair.into_inner();
            let name = expect_rule(&mut inner, Rule::ident, "expected function name")?.as_str().to_string();
            let arguments = inner
                .find(|pair| pair.as_rule() == Rule::attribute_arguments)
                .map(parse_attribute_arguments)
                .transpose()?
                .unwrap_or_default();

            Ok(AttributeValue::Call { name, arguments })
        }
        Rule::string_literal => Ok(AttributeValue::String(unquote(pair.as_str()))),
        Rule::number_literal | Rule::boolean_literal | Rule::ident => {
            Ok(AttributeValue::Ident(pair.as_str().to_string()))
        }
        _ => Err(pair_error(&pair, "unexpected attribute value")),
    }
}

fn expect_rule<'a>(
    inner: &mut impl Iterator<Item = Pair<'a, Rule>>,
    rule: Rule,
    message: &'static str,
) -> CompileResult<Pair<'a, Rule>> {
    let pair = inner.next().ok_or_else(|| CompileError::new(message, 1, 1))?;

    if pair.as_rule() == rule { Ok(pair) } else { Err(pair_error(&pair, message)) }
}

fn pair_error(pair: &Pair<'_, Rule>, message: impl Into<String>) -> CompileError {
    let (line, column) = pair.as_span().start_pos().line_col();
    CompileError::new(message, line, column)
}

fn pair_position_error(pair: &Pair<'_, Rule>, message: impl Into<String>) -> CompileError {
    pair_error(pair, message)
}

fn unquote(value: &str) -> String {
    let value = value.strip_prefix('"').and_then(|value| value.strip_suffix('"')).unwrap_or(value);
    let mut output = String::new();
    let mut chars = value.chars();

    while let Some(ch) = chars.next() {
        if ch != '\\' {
            output.push(ch);
            continue;
        }

        output.push(match chars.next() {
            Some('"') => '"',
            Some('\\') => '\\',
            Some('n') => '\n',
            Some('r') => '\r',
            Some('t') => '\t',
            Some(other) => other,
            None => '\\',
        });
    }

    output
}

#[cfg(test)]
mod tests {
    use super::parse_schema;

    #[test]
    fn compiles_schema_model() {
        let schema = parse_schema(
            r#"
            config {
                database = "postgresql"
                database_url = env("DATABASE_URL")
                read_replicas = [env("DATABASE_URL"), env("READ_DATABASE_URL")]
            }

            enum Status {
                Active
                Canceled
            }

            model User {
                id      String  @id @default(uuid())
                email   String
                status  Status
                tokens  Token[]
            }

            model Token {
                id       String  @id @default(uuid())
                user     User?   @relation(fields: [user_id], references: [id], onDelete: Cascade)
                user_id  String?
            }
            "#,
        )
        .expect("schema should compile");

        assert_eq!(schema.config().expect("config").entries.len(), 3);
        assert_eq!(schema.enums().count(), 1);
        assert_eq!(schema.models().count(), 2);

        let token = schema.models().find(|model| model.name == "Token").expect("token");
        let user = token.fields.iter().find(|field| field.name == "user").expect("user");
        assert!(user.ty.optional);
        assert!(user.attributes.iter().any(|attribute| attribute.name == "relation"));
    }
}