forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration_generate.rs
More file actions
190 lines (162 loc) · 5.55 KB
/
Copy pathmigration_generate.rs
File metadata and controls
190 lines (162 loc) · 5.55 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
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
use chrono::prelude::*;
use regex::Regex;
use crate::support::project;
pub static TIMESTAMP_FORMAT: &str = "%Y-%m-%d-%H%M%S";
#[test]
fn migration_generate_creates_a_migration_with_the_proper_name() {
let p = project("migration_name").folder("migrations").build();
let result = p.command("migration").arg("generate").arg("hello").run();
// check overall output
let expected_stdout = Regex::new(
"\
Creating migrations.\\d{4}-\\d{2}-\\d{2}-\\d{6}_hello.up.sql
Creating migrations.\\d{4}-\\d{2}-\\d{2}-\\d{6}_hello.down.sql\
",
)
.unwrap();
assert!(result.is_success(), "Command failed: {:?}", result);
assert!(expected_stdout.is_match(result.stdout()));
// check timestamps are properly formatted
let captured_timestamps = Regex::new(r"(?P<stamp>[\d-]*)_hello").unwrap();
let mut stamps_found = 0;
for caps in captured_timestamps.captures_iter(result.stdout()) {
let timestamp = Utc.datetime_from_str(&caps["stamp"], TIMESTAMP_FORMAT);
assert!(
timestamp.is_ok(),
"Found invalid timestamp format: {:?}",
&caps["stamp"]
);
stamps_found += 1;
}
assert_eq!(stamps_found, 2);
let migrations = p.migrations();
assert_eq!(1, migrations.len());
let migration = &migrations[0];
assert_eq!("hello", migration.name());
assert!(migration.path().join("up.sql").exists());
assert!(migration.path().join("down.sql").exists());
}
#[test]
fn migration_generate_creates_a_migration_with_initial_contents() {
let p = project("migration_name").folder("migrations").build();
let result = p.command("migration").arg("generate").arg("hello").run();
assert!(result.is_success(), "Command failed: {:?}", result);
let migrations = p.migrations();
let migration = &migrations[0];
let up = file_content(&migration.path().join("up.sql"));
let down = file_content(&migration.path().join("down.sql"));
assert_eq!(up.trim(), "-- Your SQL goes here");
assert_eq!(down.trim(), "-- This file should undo anything in `up.sql`");
fn file_content<P: AsRef<::std::path::Path>>(path: P) -> String {
use std::io::Read;
let mut file = ::std::fs::File::open(path).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
contents
}
}
#[test]
fn migration_generate_doesnt_require_database_url_to_be_set() {
let p = project("migration_name").folder("migrations").build();
let result = p
.command_without_database_url("migration")
.arg("generate")
.arg("hello")
.run();
assert!(result.is_success(), "Command failed: {:?}", result);
}
#[test]
fn migration_version_can_be_specified_on_creation() {
let p = project("migration_name").folder("migrations").build();
let result = p
.command("migration")
.arg("generate")
.arg("hello")
.arg("--version=1234")
.run();
let expected_stdout = Regex::new(
"\
Creating migrations.1234_hello.up.sql
Creating migrations.1234_hello.down.sql
",
)
.unwrap();
assert!(result.is_success(), "Command failed: {:?}", result);
assert!(expected_stdout.is_match(result.stdout()));
assert!(p.has_file("migrations/1234_hello/up.sql"));
assert!(p.has_file("migrations/1234_hello/down.sql"));
}
#[test]
fn migration_directory_can_be_specified_for_generate_by_command_line_arg() {
let p = project("migration_name").folder("foo").build();
let result = p
.command("migration")
.arg("generate")
.arg("stuff")
.arg("--version=12345")
.arg("--migration-dir=foo")
.run();
let expected_stdout = Regex::new(
"\
Creating foo.12345_stuff.up.sql
Creating foo.12345_stuff.down.sql
",
)
.unwrap();
assert!(result.is_success(), "Command failed: {:?}", result);
assert!(expected_stdout.is_match(result.stdout()));
assert!(p.has_file("foo/12345_stuff/up.sql"));
assert!(p.has_file("foo/12345_stuff/down.sql"));
}
#[test]
fn migration_directory_can_be_specified_for_generate_by_env_var() {
let p = project("migration_name").folder("bar").build();
let result = p
.command("migration")
.arg("generate")
.arg("stuff")
.arg("--version=12345")
.env("MIGRATION_DIRECTORY", "bar")
.run();
let expected_stdout = Regex::new(
"\
Creating bar.12345_stuff.up.sql
Creating bar.12345_stuff.down.sql
",
)
.unwrap();
assert!(result.is_success(), "Command failed: {:?}", result);
assert!(expected_stdout.is_match(result.stdout()));
assert!(p.has_file("bar/12345_stuff/up.sql"));
assert!(p.has_file("bar/12345_stuff/down.sql"));
}
#[test]
fn migration_generate_respects_migrations_dir_from_diesel_toml() {
let p = project("migration_name")
.folder("custom_migrations")
.file(
"diesel.toml",
r#"
[migrations_directory]
dir = "custom_migrations"
"#,
)
.build();
let result = p
.command("migration")
.arg("generate")
.arg("stuff")
.arg("--version=12345")
.run();
let expected_stdout = Regex::new(
"\
Creating custom_migrations.12345_stuff.up.sql
Creating custom_migrations.12345_stuff.down.sql
",
)
.unwrap();
assert!(result.is_success(), "Command failed: {:?}", result);
assert!(expected_stdout.is_match(result.stdout()));
assert!(p.has_file("custom_migrations/12345_stuff/up.sql"));
assert!(p.has_file("custom_migrations/12345_stuff/down.sql"));
}