immigrant_generator_postgres/
validate.rs

1use schema::{
2	column::ColumnAnnotation,
3	root::Schema,
4	scalar::ScalarAnnotation,
5	table::TableAnnotation,
6	uid::{RenameExt, RenameMap},
7};
8
9/// Can be updated in database source code, and some are already doing that,
10/// but for simplicity assuming default here.
11pub const MAX_IDENTIFIER_LEN: usize = 63;
12
13fn validate_db<T: RenameExt>(v: &T, rn: &RenameMap) {
14	let str = v.db(rn);
15	let str = str.raw();
16	assert!(
17		str.len() <= MAX_IDENTIFIER_LEN,
18		"{str} is larger than max allowed identifier! consider renaming"
19	);
20}
21
22pub fn validate(_code: &str, schema: &Schema, rn: &RenameMap) {
23	for ele in schema.items() {
24		validate_db(&ele, rn);
25		match ele {
26			schema::SchemaItem::Table(t) => {
27				for ele in t.columns() {
28					validate_db(&ele, rn);
29					for ele in &ele.annotations {
30						match ele {
31							ColumnAnnotation::Check(_)
32							| ColumnAnnotation::Unique(_)
33							| ColumnAnnotation::PrimaryKey(_)
34							| ColumnAnnotation::Index(_) => panic!("should be propagated"),
35							ColumnAnnotation::Default(_) | ColumnAnnotation::InitializeAs(_) => {}
36						}
37					}
38				}
39				for ele in &t.annotations {
40					match ele {
41						TableAnnotation::Check(c) => validate_db(c, rn),
42						TableAnnotation::Unique(u) => validate_db(u, rn),
43						TableAnnotation::PrimaryKey(p) => validate_db(p, rn),
44						TableAnnotation::Index(i) => validate_db(i, rn),
45						TableAnnotation::External => {}
46					}
47				}
48			}
49			schema::SchemaItem::Enum(e) => {
50				for ele in &e.items {
51					validate_db(ele, rn);
52				}
53			}
54			schema::SchemaItem::Scalar(s) => {
55				for ele in &s.annotations {
56					match ele {
57						ScalarAnnotation::Check(c) => validate_db(c, rn),
58						ScalarAnnotation::PrimaryKey(_)
59						| ScalarAnnotation::Unique(_)
60						| ScalarAnnotation::Index(_) => panic!("should be propagated"),
61						ScalarAnnotation::Default(_)
62						| ScalarAnnotation::Inline
63						| ScalarAnnotation::External => {}
64					}
65				}
66			}
67			schema::SchemaItem::Composite(c) => {
68				for ele in &c.fields {
69					validate_db(ele, rn);
70				}
71			}
72			schema::SchemaItem::View(_) => {}
73		}
74	}
75}