-
Notifications
You must be signed in to change notification settings - Fork 216
/
remapping_database_names_postgres.rs
324 lines (282 loc) Β· 10.5 KB
/
remapping_database_names_postgres.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
use crate::*;
use barrel::types;
use test_harness::*;
#[test_each_connector(tags("postgres"))]
async fn remapping_fields_with_invalid_characters_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("_a", types::text());
t.add_column("*b", types::text());
t.add_column("?c", types::text());
t.add_column("(d", types::text());
t.add_column(")e", types::text());
t.add_column("/f", types::text());
t.add_column("g a", types::text());
t.add_column("h-a", types::text());
t.add_column("h1", types::text());
});
})
.await;
let dm = r#"
model User {
a String @map("_a")
b String @map("*b")
c String @map("?c")
d String @map("(d")
e String @map(")e")
f String @map("/f")
g_a String @map("g a")
h1 String
h_a String @map("h-a")
id Int @id @default(autoincrement())
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_tables_with_invalid_characters_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());
});
migration.create_table("User with Space", |t| {
t.add_column("id", types::primary());
});
})
.await;
let dm = r#"
model User {
id Int @id @default(autoincrement())
@@map("?User")
}
model User_with_Space {
id Int @id @default(autoincrement())
@@map("User with Space")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_models_in_relations_should_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("User with Space", |t| {
t.add_column("id", types::primary());
t.add_column("name", types::text());
});
migration.create_table("Post", |t| {
t.add_column("id", types::primary());
t.add_column("user_id", types::integer());
t.inject_custom("FOREIGN KEY (\"user_id\") REFERENCES \"User with Space\"(\"id\")");
t.inject_custom("CONSTRAINT post_user_unique UNIQUE(\"user_id\")");
});
})
.await;
let dm = r#"
model Post {
id Int @id @default(autoincrement())
user_id User_with_Space
}
model User_with_Space {
id Int @id @default(autoincrement())
name String
post Post?
@@map("User with Space")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
#[test]
async fn remapping_models_in_compound_relations_should_work(api: &TestApi) {
let barrel = api.barrel();
let _setup_schema = barrel
.execute(|migration| {
migration.create_table("User with Space", |t| {
t.add_column("id", types::primary());
t.add_column("name", types::text());
t.inject_custom("CONSTRAINT user_unique UNIQUE(\"id\", \"name\")");
});
migration.create_table("Post", |t| {
t.add_column("id", types::primary());
t.add_column("user_id", types::integer());
t.add_column("user_name", types::text());
t.inject_custom(
"FOREIGN KEY (\"user_id\",\"user_name\") REFERENCES \"User with Space\"(\"id\", \"name\")",
);
t.inject_custom("CONSTRAINT post_user_unique UNIQUE(\"user_id\", \"user_name\")");
});
})
.await;
let dm = r#"
model Post {
id Int @id @default(autoincrement())
user_with_Space User_with_Space @map(["user_id", "user_name"]) @relation(references:[id, name])
}
model User_with_Space {
id Int @id @default(autoincrement())
name String
post Post?
@@map("User with Space")
@@unique([id, name], name: "user_unique")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
#[test]
async fn remapping_fields_in_compound_relations_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-that-is-invalid", types::text());
t.inject_custom("CONSTRAINT user_unique UNIQUE(\"id\", \"name-that-is-invalid\")");
});
migration.create_table("Post", |t| {
t.add_column("id", types::primary());
t.add_column("user_id", types::integer());
t.add_column("user_name", types::text());
t.inject_custom(
"FOREIGN KEY (\"user_id\",\"user_name\") REFERENCES \"User\"(\"id\", \"name-that-is-invalid\")",
);
t.inject_custom("CONSTRAINT post_user_unique UNIQUE(\"user_id\", \"user_name\")");
});
})
.await;
let dm = r#"
model Post {
id Int @id @default(autoincrement())
user User @map(["user_id", "user_name"]) @relation(references:[id, name_that_is_invalid])
}
model User {
id Int @id @default(autoincrement())
name_that_is_invalid String @map("name-that-is-invalid")
post Post?
@@unique([id, name_that_is_invalid], name: "user_unique")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_enum_names_should_work(api: &TestApi) {
let sql1 = format!("CREATE Type _color as ENUM ('black')");
api.database().execute_raw(&sql1, &[]).await.unwrap();
api.barrel()
.execute(|migration| {
migration.create_table("Book", |t| {
t.add_column("id", types::primary());
t.inject_custom("color _color Not Null");
});
})
.await;
let dm = r#"
model Book {
color color
id Int @default(autoincrement()) @id
}
enum color {
black
@@map("_color")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_enum_values_should_work(api: &TestApi) {
let sql1 = format!("CREATE Type Color as ENUM ('b lack', 'w hite')");
api.database().execute_raw(&sql1, &[]).await.unwrap();
api.barrel()
.execute(|migration| {
migration.create_table("Book", |t| {
t.add_column("id", types::primary());
t.inject_custom("color Color Not Null");
});
})
.await;
let dm = r#"
model Book {
color color
id Int @default(autoincrement()) @id
}
enum color {
b_lack @map("b lack")
w_hite @map("w hite")
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_compound_primary_keys_should_work(api: &TestApi) {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("first_name", types::text());
t.add_column("last@name", types::text());
t.inject_custom("Primary Key (\"first_name\", \"last@name\")");
});
})
.await;
let dm = r#"
model User {
first_name String
last_name String @map("last@name")
@@id([first_name, last_name])
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_enum_default_values_should_work(api: &TestApi) {
let sql = format!("CREATE Type color as ENUM ( 'b lack', 'white')");
api.database().execute_raw(&sql, &[]).await.unwrap();
api.barrel()
.execute(|migration| {
migration.create_table("Book", |t| {
t.add_column("id", types::primary());
t.inject_custom("color color Not Null default 'b lack'");
});
})
.await;
let dm = r#"
model Book {
color color @default(b_lack)
id Int @default(autoincrement()) @id
}
enum color{
b_lack @map("b lack")
white
}
"#;
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}
#[test_each_connector(tags("postgres"))]
async fn remapping_field_names_to_empty_should_comment_them_out(api: &TestApi) {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("1", types::text());
t.add_column("last", types::primary());
});
})
.await;
let dm = "model User {\n /// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*\n // 1 String @map(\"1\")\n last Int @default(autoincrement()) @id\n}";
let result = dbg!(api.introspect().await);
assert_eq!(&result, dm);
}