-
Notifications
You must be signed in to change notification settings - Fork 215
/
db_execute.rs
177 lines (155 loc) Β· 6.17 KB
/
db_execute.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
use migration_engine_tests::test_api::*;
use quaint::{prelude::Queryable, single::Quaint};
#[test]
fn db_execute_input_source_takes_expected_json_shape() {
let value = DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer {
url: "uiuiui".to_owned(),
}),
script: "SQL goes here".to_owned(),
};
let expected = expect![[r#"
{
"datasourceType": {
"tag": "url",
"url": "uiuiui"
},
"script": "SQL goes here"
}"#]];
expected.assert_eq(&serde_json::to_string_pretty(&value).unwrap());
}
#[test]
fn db_execute_happy_path_with_literal_url() {
let tmpdir = tempfile::TempDir::new().unwrap();
let url = format!("file:{}/db1.sqlite", tmpdir.path().to_string_lossy());
let script = r#"
CREATE TABLE "dogs" ( id INTEGER PRIMARY KEY, name TEXT );
INSERT INTO "dogs" ("name") VALUES ('snoopy'), ('marmaduke');
"#;
// Execute the command.
let generic_api = migration_core::migration_api(None, None).unwrap();
tok(generic_api.db_execute(DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer { url: url.clone() }),
script: script.to_owned(),
}))
.unwrap();
// Check that the command was executed
let q = tok(quaint::single::Quaint::new(&url)).unwrap();
let result = tok(q.query_raw("SELECT name FROM dogs;", &[])).unwrap();
let mut rows = result.into_iter();
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "snoopy");
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "marmaduke");
}
#[test]
fn db_execute_happy_path_with_prisma_schema() {
let tmpdir = tempfile::TempDir::new().unwrap();
let url = format!("file:{}/dbfromschema.sqlite", tmpdir.path().to_string_lossy());
let prisma_schema = format!(
r#"
datasource dbtest {{
url = "{}"
provider = "sqlite"
}}
"#,
url.replace('\\', "\\\\")
);
let schema_path = tmpdir.path().join("schema.prisma");
std::fs::write(&schema_path, &prisma_schema).unwrap();
let script = r#"
CREATE TABLE "dogs" ( id INTEGER PRIMARY KEY, name TEXT );
INSERT INTO "dogs" ("name") VALUES ('snoopy'), ('marmaduke');
"#;
// Execute the command.
let generic_api = migration_core::migration_api(None, None).unwrap();
tok(generic_api.db_execute(DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Schema(SchemaContainer {
schema: schema_path.to_string_lossy().into_owned(),
}),
script: script.to_owned(),
}))
.unwrap();
// Check that the command was executed
let q = tok(quaint::single::Quaint::new(&url)).unwrap();
let result = tok(q.query_raw("SELECT name FROM dogs;", &[])).unwrap();
let mut rows = result.into_iter();
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "snoopy");
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "marmaduke");
}
#[test_connector(tags(Mysql))]
fn mysql_incomplete_script_works(api: TestApi) {
let script = r#"
CREATE TABLE `dogs` ( id INTEGER AUTO_INCREMENT PRIMARY KEY, name TEXT );
INSERT INTO `dogs` (`name`) VALUES ('snoopy'), ('marmaduke') -- missing final semicolon
"#;
let url = api.connection_string().to_owned();
let generic_api = migration_core::migration_api(None, None).unwrap();
tok(generic_api.db_execute(DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer { url: url.clone() }),
script: script.to_owned(),
}))
.unwrap();
// Check that the command was executed
let q = tok(Quaint::new(&url)).unwrap();
let result = tok(q.query_raw("SELECT name FROM dogs;", &[])).unwrap();
let mut rows = result.into_iter();
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "snoopy");
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "marmaduke");
}
#[test_connector(tags(Mysql))]
fn db_execute_error_path(api: TestApi) {
let script = r#"
-- wrong quotes
CREATE TABLE "dogs" ( id INTEGER AUTO_INCREMENT PRIMARY KEY, name TEXT );
"#;
let generic_api = migration_core::migration_api(None, None).unwrap();
let result = tok(generic_api.db_execute(DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer {
url: api.connection_string().to_owned(),
}),
script: script.to_owned(),
}));
assert!(result.is_err());
}
#[test_connector(tags(Postgres))]
fn db_execute_drop_database_that_doesnt_exist_error(api: TestApi) {
let script = r#"
DROP DATABASE "thisisadatabaseweassumedoesntexist";
"#;
let generic_api = migration_core::migration_api(None, None).unwrap();
let result = tok(generic_api.db_execute(DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer {
url: api.connection_string().to_owned(),
}),
script: script.to_owned(),
}));
let error = result.unwrap_err().to_string();
let expectation = expect![[r#"
Database `thisisadatabaseweassumedoesntexist.prisma-tests` does not exist on the database server at `localhost:5433`.
"#]];
expectation.assert_eq(&error);
}
#[test]
fn sqlite_db_execute_with_schema_datasource_resolves_relative_paths_correctly() {
let tmpdir = tempfile::tempdir().unwrap();
let prisma_dir = tmpdir.path().join("prisma");
std::fs::create_dir_all(&prisma_dir).unwrap();
let schema_path = prisma_dir.join("schema.prisma");
let schema = r#"
datasource sqlitedb {
provider = "sqlite"
url = "file:./dev.db"
}
"#;
std::fs::write(&schema_path, schema).unwrap();
let expected_sqlite_path = prisma_dir.join("dev.db");
assert!(!expected_sqlite_path.exists());
let api = migration_core::migration_api(None, None).unwrap();
tok(api.db_execute(DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Schema(SchemaContainer {
schema: schema_path.to_str().unwrap().to_owned(),
}),
script: "CREATE TABLE dog ( id INTEGER PRIMARY KEY )".to_owned(),
}))
.unwrap();
assert!(expected_sqlite_path.exists());
}