-
Notifications
You must be signed in to change notification settings - Fork 216
/
tables_sqlite.rs
280 lines (258 loc) Β· 9.1 KB
/
tables_sqlite.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
use crate::*;
use barrel::types;
use test_harness::*;
#[test_each_connector(tags("sqlite"))]
#[test]
async fn introspecting_a_simple_table_with_gql_types_must_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("bool", types::boolean());
t.add_column("float", types::float());
t.add_column("date", types::date());
t.add_column("id", types::primary());
t.add_column("integer", types::integer());
t.inject_custom("int int not null");
t.add_column("string", types::text());
});
})
.await;
let dm = r#"
model Blog {
bool Boolean
date DateTime
float Float
id Int @id @default(autoincrement())
int Int
integer Int
string String
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_compound_primary_keys_must_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("id", types::integer());
t.add_column("authorId", types::text());
t.inject_custom("PRIMARY KEY (\"id\", \"authorId\")");
});
})
.await;
let dm = r#"
model Blog {
authorId String
id Int
@@id([id, authorId])
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_unique_index_must_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("id", types::primary());
t.add_column("authorId", types::text());
t.add_index("test", types::index(vec!["authorId"]).unique(true));
});
})
.await;
let dm = r#"
model Blog {
authorId String @unique
id Int @id @default(autoincrement())
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_multi_column_unique_index_must_work(api: &TestApi) {
let barrel = api.barrel();
barrel
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
t.add_column("firstname", types::text());
t.add_column("lastname", types::text());
t.add_index("test", types::index(vec!["firstname", "lastname"]).unique(true));
});
})
.await;
let dm = r#"
model User {
firstname String
id Int @id @default(autoincrement())
lastname String
@@unique([firstname, lastname], name: "test")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_required_and_optional_columns_must_work(api: &TestApi) {
let barrel = api.barrel();
barrel
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
t.add_column("requiredname", types::text().nullable(false));
t.add_column("optionalname", types::text().nullable(true));
});
})
.await;
let dm = r#"
model User {
id Int @id @default(autoincrement())
optionalname String?
requiredname String
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
//#[test_each_connector(tags("sqlite"))]
//#[ignore]
//fn introspecting_a_table_with_datetime_default_values_should_work(api: &TestApi) {
// let barrel = api.barrel();
// let _setup_schema = barrel.execute(|migration| {
// migration.create_table("User", |t| {
// t.add_column("id", types::primary());
// t.add_column("name", types::text());
// t.inject_custom("\"joined\" date DEFAULT CURRENT_DATE")
// });
// });
//
// let dm = r#"
// model User {
// id Int @id
// joined DateTime? @default(now())
// name String
// }
// "#;
// let result = dbg!(api.introspect().await);
// custom_assert(&result, dm);
//}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_default_values_should_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("a", types::text());
t.add_column("id", types::primary());
t.inject_custom("\"bool\" Boolean NOT NULL DEFAULT false");
t.inject_custom("\"bool2\" Boolean NOT NULL DEFAULT 0");
t.inject_custom("\"float\" Float NOT NULL DEFAULT 5.3");
t.inject_custom("\"int\" INTEGER NOT NULL DEFAULT 5");
t.inject_custom("\"string\" TEXT NOT NULL DEFAULT \"Test\"");
});
})
.await;
let dm = r#"
model User {
a String
bool Boolean @default(false)
bool2 Boolean @default(false)
float Float @default(5.3)
id Int @id @default(autoincrement())
int Int @default(5)
string String @default("Test")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_a_non_unique_index_should_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("a", types::text());
t.add_column("id", types::primary());
t.add_index("test", types::index(vec!["a"]));
});
})
.await;
let dm = r#"
model User {
a String
id Int @id @default(autoincrement())
@@index([a], name: "test")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_a_multi_column_non_unique_index_should_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("a", types::text());
t.add_column("b", types::text());
t.add_column("id", types::primary());
t.add_index("test", types::index(vec!["a", "b"]));
});
})
.await;
let dm = r#"
model User {
a String
b String
id Int @id @default(autoincrement())
@@index([a,b], name: "test")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_with_optional_autoincrement_should_work(api: &TestApi) {
api.barrel()
.execute(|migration| {
migration.create_table("Book", |t| {
t.inject_custom("book_id Integer Primary Key Autoincrement");
});
})
.await;
let dm = r#"
model Book {
book_id Int @default(autoincrement()) @id
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("sqlite"))]
async fn introspecting_a_table_without_uniques_should_comment_it_out(api: &TestApi) {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("id", types::primary());
});
migration.create_table("Post", |t| {
t.add_column("id", types::integer());
t.inject_custom(
"user_id INTEGER NOT NULL UNIQUE,
FOREIGN KEY (`user_id`) REFERENCES `User`(`id`)",
)
});
})
.await;
let dm = "model User {\n id Int @default(autoincrement()) @id\n}\n\n/// The underlying table does not contain a unique identifier and can therefore currently not be handled.\n// model Post {\n // id Int\n // user_id User\n// }";
let result = dbg!(api.introspect().await);
assert_eq!(&result, dm);
}