forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_reset.rs
More file actions
152 lines (124 loc) · 4.69 KB
/
Copy pathdatabase_reset.rs
File metadata and controls
152 lines (124 loc) · 4.69 KB
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
#[cfg(feature = "postgres")]
extern crate url;
use support::{database, project};
#[test]
fn reset_drops_the_database() {
let p = project("reset_drops_the_database")
.folder("migrations")
.build();
let db = database(&p.database_url()).create();
db.execute("CREATE TABLE posts ( id INTEGER )");
assert!(db.table_exists("posts"));
let result = p.command("database")
.arg("reset")
.run();
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!db.table_exists("posts"));
}
#[test]
fn reset_runs_database_setup() {
let p = project("reset_runs_database_setup")
.folder("migrations")
.build();
let db = database(&p.database_url()).create();
db.execute("CREATE TABLE posts ( id INTEGER )");
db.execute("CREATE TABLE users ( id INTEGER )");
p.create_migration("12345_create_users_table",
"CREATE TABLE users ( id INTEGER )",
"DROP TABLE users");
assert!(db.table_exists("posts"));
assert!(db.table_exists("users"));
let result = p.command("database")
.arg("reset")
.run();
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!db.table_exists("posts"));
assert!(db.table_exists("users"));
assert!(db.table_exists("__diesel_schema_migrations"));
}
#[test]
#[cfg(feature = "postgres")]
fn reset_handles_postgres_urls_with_username_and_password() {
let p = project("handles_postgres_urls")
.folder("migrations")
.build();
let db = database(&p.database_url()).create();
db.execute("DROP ROLE IF EXISTS foo");
db.execute("CREATE ROLE foo WITH LOGIN SUPERUSER PASSWORD 'password'");
let database_url = {
let mut new_url = url::Url::parse(&p.database_url()).expect("invalid url");
new_url.set_username("foo").expect("could not set username");
new_url.set_password(Some("password")).expect("could not set password");
new_url.to_string()
};
let result = p.command("database")
.arg("reset")
.env("DATABASE_URL", &database_url)
.run();
assert!(result.is_success(), "Result was unsuccessful {:?}", result.stdout());
assert!(result.stdout().contains("Dropping database:"),
"Unexpected stdout {}", result.stdout());
assert!(result.stdout().contains("Creating database:"),
"Unexpected stdout {}", result.stdout());
}
#[test]
fn reset_works_with_migration_dir_by_arg() {
let p = project("reset_works_with_migration_dir_by_arg")
.folder("foo")
.build();
let db = database(&p.database_url()).create();
db.execute("CREATE TABLE posts ( id INTEGER )");
db.execute("CREATE TABLE users ( id INTEGER )");
p.create_migration_in_directory("foo",
"12345_create_users_table",
"CREATE TABLE users ( id INTEGER )",
"DROP TABLE users");
assert!(db.table_exists("posts"));
assert!(db.table_exists("users"));
let result = p.command("database")
.arg("reset")
.arg("--migration-dir=foo")
.run();
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!db.table_exists("posts"));
assert!(db.table_exists("users"));
assert!(db.table_exists("__diesel_schema_migrations"));
}
#[test]
fn reset_works_with_migration_dir_by_env() {
let p = project("reset_works_with_migration_dir_by_env")
.folder("bar")
.build();
let db = database(&p.database_url()).create();
db.execute("CREATE TABLE posts ( id INTEGER )");
db.execute("CREATE TABLE users ( id INTEGER )");
p.create_migration_in_directory("bar",
"12345_create_users_table",
"CREATE TABLE users ( id INTEGER )",
"DROP TABLE users");
assert!(db.table_exists("posts"));
assert!(db.table_exists("users"));
let result = p.command("database")
.arg("reset")
.env("MIGRATION_DIRECTORY", "bar")
.run();
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!db.table_exists("posts"));
assert!(db.table_exists("users"));
assert!(db.table_exists("__diesel_schema_migrations"));
}
#[test]
fn reset_sanitize_database_name() {
let p = project("name-with-dashes")
.folder("migrations")
.build();
let _db = database(&p.database_url()).create();
let result = p.command("database")
.arg("reset")
.run();
assert!(result.is_success(), "Result was unsuccessful {:?}", result.stdout());
assert!(result.stdout().contains("Dropping database:"),
"Unexpected stdout {}", result.stdout());
assert!(result.stdout().contains("Creating database:"),
"Unexpected stdout {}", result.stdout());
}