-
Notifications
You must be signed in to change notification settings - Fork 212
/
sqlite_introspection_tests.rs
259 lines (245 loc) Β· 9.27 KB
/
sqlite_introspection_tests.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
use barrel::{types, Migration};
use pretty_assertions::assert_eq;
use sql_schema_describer::*;
mod common;
mod sqlite;
use common::*;
use sqlite::*;
#[tokio::test]
async fn sqlite_column_types_must_work() {
let mut migration = Migration::new().schema(SCHEMA);
migration.create_table("User", move |t| {
t.inject_custom("int_col int not null");
t.add_column("int4_col", types::integer());
t.add_column("text_col", types::text());
t.add_column("real_col", types::float());
t.add_column("primary_col", types::primary());
});
let full_sql = migration.make::<barrel::backend::Sqlite>();
let inspector = get_sqlite_describer(&full_sql, "sqlite_column_types_must_work").await;
let result = inspector.describe(SCHEMA).await.expect("describing");
let table = result.get_table("User").expect("couldn't get User table");
let mut expected_columns = vec![
Column {
name: "int_col".to_string(),
tpe: ColumnType {
raw: "int".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Required,
},
default: None,
auto_increment: false,
},
Column {
name: "int4_col".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Required,
},
default: None,
auto_increment: false,
},
Column {
name: "text_col".to_string(),
tpe: ColumnType {
raw: "TEXT".to_string(),
family: ColumnTypeFamily::String,
arity: ColumnArity::Required,
},
default: None,
auto_increment: false,
},
Column {
name: "real_col".to_string(),
tpe: ColumnType {
raw: "REAL".to_string(),
family: ColumnTypeFamily::Float,
arity: ColumnArity::Required,
},
default: None,
auto_increment: false,
},
Column {
name: "primary_col".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Required,
},
default: None,
auto_increment: true,
},
];
expected_columns.sort_unstable_by_key(|c| c.name.to_owned());
assert_eq!(
table,
&Table {
name: "User".to_string(),
columns: expected_columns,
indices: vec![],
primary_key: Some(PrimaryKey {
columns: vec!["primary_col".to_string()],
sequence: None,
}),
foreign_keys: vec![],
}
);
}
#[tokio::test]
async fn sqlite_foreign_key_on_delete_must_be_handled() {
let sql = format!(
"CREATE TABLE \"{0}\".City (id INTEGER NOT NULL PRIMARY KEY);
CREATE TABLE \"{0}\".User (
id INTEGER NOT NULL PRIMARY KEY,
city INTEGER REFERENCES City(id) ON DELETE NO ACTION,
city_cascade INTEGER REFERENCES City(id) ON DELETE CASCADE,
city_restrict INTEGER REFERENCES City (id) ON DELETE RESTRICT,
city_set_default INTEGER REFERENCES City(id) ON DELETE SET DEFAULT,
city_set_null INTEGER REFERENCES City(id) ON DELETE SET NULL
)",
SCHEMA
);
let inspector = get_sqlite_describer(&sql, "sqlite_foreign_key_on_delete_must_be_handled").await;
let schema = inspector.describe(SCHEMA).await.expect("describing");
let mut table = schema.get_table("User").expect("get User table").to_owned();
table.foreign_keys.sort_unstable_by_key(|fk| fk.columns.clone());
assert_eq!(
table,
Table {
name: "User".to_string(),
columns: vec![
Column {
name: "city".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Nullable,
},
default: None,
auto_increment: false,
},
Column {
name: "city_cascade".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Nullable,
},
default: None,
auto_increment: false,
},
Column {
name: "city_restrict".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Nullable,
},
default: None,
auto_increment: false,
},
Column {
name: "city_set_default".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Nullable,
},
default: None,
auto_increment: false,
},
Column {
name: "city_set_null".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Nullable,
},
default: None,
auto_increment: false,
},
Column {
name: "id".to_string(),
tpe: ColumnType {
raw: "INTEGER".to_string(),
family: ColumnTypeFamily::Int,
arity: ColumnArity::Required,
},
default: None,
auto_increment: true,
},
],
indices: vec![],
primary_key: Some(PrimaryKey {
columns: vec!["id".to_string()],
sequence: None,
}),
foreign_keys: vec![
ForeignKey {
constraint_name: None,
columns: vec!["city".to_string()],
referenced_columns: vec!["id".to_string()],
referenced_table: "City".to_string(),
on_delete_action: ForeignKeyAction::NoAction,
},
ForeignKey {
constraint_name: None,
columns: vec!["city_cascade".to_string()],
referenced_columns: vec!["id".to_string()],
referenced_table: "City".to_string(),
on_delete_action: ForeignKeyAction::Cascade,
},
ForeignKey {
constraint_name: None,
columns: vec!["city_restrict".to_string()],
referenced_columns: vec!["id".to_string()],
referenced_table: "City".to_string(),
on_delete_action: ForeignKeyAction::Restrict,
},
ForeignKey {
constraint_name: None,
columns: vec!["city_set_default".to_string()],
referenced_columns: vec!["id".to_string()],
referenced_table: "City".to_string(),
on_delete_action: ForeignKeyAction::SetDefault,
},
ForeignKey {
constraint_name: None,
columns: vec!["city_set_null".to_string()],
referenced_columns: vec!["id".to_string()],
referenced_table: "City".to_string(),
on_delete_action: ForeignKeyAction::SetNull,
},
],
}
);
}
#[tokio::test]
async fn sqlite_text_primary_keys_must_be_inferred_on_table_and_not_as_separate_indexes() {
let mut migration = Migration::new().schema(SCHEMA);
migration.create_table("User", move |t| {
t.add_column("int4_col", types::integer());
t.add_column("text_col", types::text());
t.add_column("real_col", types::float());
t.add_column("primary_col", types::text());
// Simulate how we create primary keys in the migrations engine.
t.inject_custom("PRIMARY KEY (\"primary_col\")");
});
let full_sql = migration.make::<barrel::backend::Sqlite>();
let inspector = get_sqlite_describer(
&full_sql,
"sqlite_text_primary_keys_must_be_inferred_on_table_and_not_as_separate_indexes",
)
.await;
let result = inspector.describe(SCHEMA).await.expect("describing");
let table = result.get_table("User").expect("couldn't get User table");
assert!(table.indices.is_empty());
assert_eq!(
table.primary_key.as_ref().unwrap(),
&PrimaryKey {
columns: vec!["primary_col".to_owned()],
sequence: None
}
);
}