-
Notifications
You must be signed in to change notification settings - Fork 216
/
failure_modes.rs
221 lines (196 loc) Β· 9.26 KB
/
failure_modes.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
use migration_engine_tests::test_api::*;
#[test_connector(tags(CockroachDb))]
fn failing_migration_after_migration_dropping_data(api: TestApi) {
let migrations = &[
r#"
CREATE TABLE "Dog" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
is_good_dog BOOLEAN NOT NULL DEFAULT TRUE
);
INSERT INTO "Dog" (name, is_good_dog) VALUES
('snoopy', true),
('marmaduke', true),
('pluto', true),
('dingo', true),
('itzi', true),
('mugi', true)
;
"#,
r#"
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
"#,
r#"
ALTER TABLE "Dog" ALTER COLUMN is_good_dog TYPE INTEGER;
"#,
];
let dir = write_migrations(migrations);
let err = api.apply_migrations(&dir).send_unwrap_err().to_string();
let expectation = expect![[r#"
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 2
Database error code: 42703
Database error:
ERROR: column "is_good_dog" does not exist
DbError { severity: "ERROR", parsed_severity: None, code: SqlState(E42703), message: "column \"is_good_dog\" does not exist", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("column_resolver.go"), line: Some(196), routine: Some("NewUndefinedColumnError") }
0: sql_migration_connector::apply_migration::apply_script
with [3mmigration_name[0m[2m=[0m" 2"
at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:104
1: migration_core::commands::apply_migrations::Applying migration
with [3mmigration_name[0m[2m=[0m" 2"
at migration-engine/core/src/commands/apply_migrations.rs:49"#]];
expectation.assert_eq(&err);
}
#[test_connector(tags(CockroachDb))]
fn failing_step_in_migration_dropping_data(api: TestApi) {
let migrations = &[
r#"
CREATE TABLE "Dog" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
is_good_dog BOOLEAN NOT NULL DEFAULT TRUE
);
INSERT INTO "Dog" (name, is_good_dog) VALUES
('snoopy', true),
('marmaduke', true),
('pluto', true),
('dingo', true),
('itzi', true),
('mugi', true)
;
"#,
r#"
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
ALTER TABLE "Dog" ALTER COLUMN is_good_dog TYPE INTEGER;
"#,
];
let dir = write_migrations(migrations);
let err = api.apply_migrations(&dir).send_unwrap_err().to_string();
let expectation = expect![[r#"
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 1
Database error code: 42703
Database error:
ERROR: column "is_good_dog" does not exist
DbError { severity: "ERROR", parsed_severity: None, code: SqlState(E42703), message: "column \"is_good_dog\" does not exist", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("column_resolver.go"), line: Some(196), routine: Some("NewUndefinedColumnError") }
0: sql_migration_connector::apply_migration::apply_script
with [3mmigration_name[0m[2m=[0m" 1"
at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:104
1: migration_core::commands::apply_migrations::Applying migration
with [3mmigration_name[0m[2m=[0m" 1"
at migration-engine/core/src/commands/apply_migrations.rs:49"#]];
expectation.assert_eq(&err);
}
#[test_connector(tags(CockroachDb))]
fn multiple_alter_tables_in_one_migration_works(api: TestApi) {
let migrations = &[
r#"
CREATE TABLE "Dog" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
is_good_dog BOOLEAN NOT NULL DEFAULT TRUE
);
INSERT INTO "Dog" (name, is_good_dog) VALUES
('snoopy', true),
('marmaduke', true),
('pluto', true),
('dingo', true),
('itzi', true),
('mugi', true)
;
"#,
r#"
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
ALTER TABLE "Dog" ADD COLUMN is_good_dog INTEGER NOT NULL DEFAULT 100;
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
ALTER TABLE "Dog" ADD COLUMN is_good_dog INTEGER NOT NULL DEFAULT 100;
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
ALTER TABLE "Dog" ADD COLUMN is_good_dog INTEGER NOT NULL DEFAULT 100;
"#,
];
let dir = write_migrations(migrations);
api.apply_migrations(&dir).send_sync();
}
#[test_connector(tags(CockroachDb))]
fn multiple_alter_tables_in_multiple_migration_works(api: TestApi) {
let migrations = &[
r#"
CREATE TABLE "Dog" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
is_good_dog BOOLEAN NOT NULL DEFAULT TRUE
);
INSERT INTO "Dog" (name, is_good_dog) VALUES
('snoopy', true),
('marmaduke', true),
('pluto', true),
('dingo', true),
('itzi', true),
('mugi', true)
;
"#,
r#"
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
"#,
r#"
ALTER TABLE "Dog" ADD COLUMN is_good_dog INTEGER NOT NULL DEFAULT 100;
"#,
r#"
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
"#,
r#"
ALTER TABLE "Dog" ADD COLUMN is_good_dog INTEGER NOT NULL DEFAULT 100;
"#,
r#"
ALTER TABLE "Dog" DROP COLUMN is_good_dog;
"#,
r#"
ALTER TABLE "Dog" ADD COLUMN is_good_dog INTEGER NOT NULL DEFAULT 100;
"#,
];
let dir = write_migrations(migrations);
api.apply_migrations(&dir).send_sync();
}
#[test_connector(tags(CockroachDb))]
fn syntax_errors_return_error_position(api: TestApi) {
let migrations = &[r#"
CREATE TABLE "Dog" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
is_good_dog BOOLEAN NOT NULL DEFAULT TRUE
);
"#];
let dir = write_migrations(migrations);
let err = api.apply_migrations(&dir).send_unwrap_err().to_string();
let expectation = expect![[r#"
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 0
Database error code: 42601
Database error:
ERROR: at or near "is_good_dog": syntax error
DETAIL: source SQL:
CREATE TABLE "Dog" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
is_good_dog BOOLEAN NOT NULL DEFAULT TRUE
^
HINT: try \h CREATE TABLE
DbError { severity: "ERROR", parsed_severity: None, code: SqlState(E42601), message: "at or near \"is_good_dog\": syntax error", detail: Some("source SQL:\nCREATE TABLE \"Dog\" (\n id SERIAL PRIMARY KEY,\n name TEXT NOT NULL\n is_good_dog BOOLEAN NOT NULL DEFAULT TRUE\n ^"), hint: Some("try \\h CREATE TABLE"), position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("lexer.go"), line: Some(251), routine: Some("Error") }
0: sql_migration_connector::apply_migration::apply_script
with [3mmigration_name[0m[2m=[0m" 0"
at migration-engine/connectors/sql-migration-connector/src/apply_migration.rs:104
1: migration_core::commands::apply_migrations::Applying migration
with [3mmigration_name[0m[2m=[0m" 0"
at migration-engine/core/src/commands/apply_migrations.rs:49"#]];
expectation.assert_eq(&err);
}
fn write_migrations(migrations: &[&str]) -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
for (idx, migration) in migrations.iter().enumerate() {
let migration_dir = dir.path().join(format!("{:3}", idx));
std::fs::create_dir(&migration_dir).unwrap();
let migration_path = migration_dir.join("migration.sql");
std::fs::write(&migration_path, migration).unwrap();
}
dir
}