5fn move_column_between_tables() {
6 let mut test = Test::new("Move column between tables");
10 name = "create_tables"
41 test.second_migration(
43 name = "move_email_column"
54 # When `users` is updated in the old schema, we write the email value to `profiles`
55 # When `profiles` is updated in the old schema, the equivalent `users.email` will also be updated
59 where = "profiles.user_id = users.id"
62 type = "remove_column"
66 # When `profiles` is changed in the new schema, we write the email address back to the removed column
69 value = "profiles.email"
70 where = "users.id = profiles.user_id"
74 test.after_first(|db| {
75 db.simple_query("INSERT INTO users (id, email) VALUES (1, 'test1@test.com')")
77 db.simple_query("INSERT INTO users (id, email) VALUES (2, 'test2@test.com')")
80 db.simple_query("INSERT INTO profiles (id, user_id) VALUES (1, 1)")
82 db.simple_query("INSERT INTO profiles (id, user_id) VALUES (2, 2)")
86 test.intermediate(|old_db, new_db| {
87 // Ensure emails were backfilled into profiles
88 let profiles_emails: Vec<String> = new_db
99 .map(|row| row.get("email"))
101 assert_eq!(vec!("test1@test.com", "test2@test.com"), profiles_emails);
103 // Ensure insert in old schema updates new
105 .simple_query("INSERT INTO users (id, email) VALUES (3, 'test3@test.com')")
108 .simple_query("INSERT INTO profiles (id, user_id) VALUES (3, 3)")
110 let new_email: String = new_db
111 .query("SELECT email FROM profiles WHERE id = 3", &[])
114 .map(|row| row.get("email"))
116 assert_eq!("test3@test.com", new_email);
118 // Ensure updates in old schema updates new
120 .simple_query("UPDATE users SET email = 'test3+updated@test.com' WHERE id = 3")
122 let new_email: String = new_db
123 .query("SELECT email FROM profiles WHERE id = 3", &[])
126 .map(|row| row.get("email"))
128 assert_eq!("test3+updated@test.com", new_email);
130 // Ensure insert in new schema updates old
133 "INSERT INTO profiles (id, user_id, email) VALUES (4, 4, 'test4@test.com')",
137 .simple_query("INSERT INTO users (id) VALUES (4)")
139 let old_email: String = old_db
140 .query("SELECT email FROM users WHERE id = 4", &[])
143 .map(|row| row.get("email"))
145 assert_eq!("test4@test.com", old_email);
147 // Ensure update in new schema updates old
149 .simple_query("UPDATE profiles SET email = 'test4+updated@test.com' WHERE id = 4")
151 let old_email: String = old_db
152 .query("SELECT email FROM users WHERE id = 4", &[])
155 .map(|row| row.get("email"))
157 assert_eq!("test4+updated@test.com", old_email);
164fn extract_relation_into_new_table() {
165 let mut test = Test::new("Extract relation into new table");
167 test.first_migration(
169 name = "create_tables"
172 type = "create_table"
181 type = "create_table"
195 name = "account_role"
201 test.second_migration(
203 name = "add_account_user_connection"
206 type = "create_table"
207 name = "user_account_connections"
208 primary_key = ["account_id", "user_id"]
225 values = { user_id = "id", account_id = "account_id", role = "UPPER(account_role)" }
226 where = "user_account_connections.user_id = users.id"
229 type = "remove_column"
231 column = "account_id"
234 table = "user_account_connections"
235 value = "user_account_connections.account_id"
236 where = "users.id = user_account_connections.user_id"
239 type = "remove_column"
241 column = "account_role"
244 table = "user_account_connections"
245 value = "LOWER(user_account_connections.role)"
246 where = "users.id = user_account_connections.user_id"
250 test.after_first(|db| {
251 db.simple_query("INSERT INTO accounts (id) VALUES (1)")
253 db.simple_query("INSERT INTO users (id, account_id, account_role) VALUES (1, 1, 'admin')")
257 test.intermediate(|old_db, new_db| {
258 // Ensure connections was backfilled
259 let rows: Vec<(i32, i32, String)> = new_db
262 SELECT account_id, user_id, role
263 FROM user_account_connections
269 .map(|row| (row.get("account_id"), row.get("user_id"), row.get("role")))
271 assert_eq!(1, rows.len());
273 let row = rows.first().unwrap();
274 assert_eq!(1, row.0);
275 assert_eq!(1, row.1);
276 assert_eq!("ADMIN", row.2);
278 // Ensure inserted user in old schema creates a new connection
281 "INSERT INTO users (id, account_id, account_role) VALUES (2, 1, 'developer')",
288 SELECT account_id, user_id, role
289 FROM user_account_connections
290 WHERE account_id = 1 AND user_id = 2 AND role = 'DEVELOPER'
299 // Ensure NOT NULL constraint still applies to old schema
302 "INSERT INTO users (id, account_id, account_role) VALUES (2, NULL, 'developer')",
304 assert!(result.is_err());
306 // Ensure updated user role in old schema updates connection in new schema
308 .simple_query("UPDATE users SET account_role = 'admin' WHERE id = 2")
314 SELECT account_id, user_id, role
315 FROM user_account_connections
316 WHERE account_id = 1 AND user_id = 2 AND role = 'ADMIN'
325 // Ensure updated connection in new schema updates old schema user
328 "UPDATE user_account_connections SET role = 'DEVELOPER' WHERE account_id = 1 AND user_id = 2",
337 WHERE id = 2 AND account_id = 1 AND account_role = 'developer'
346 // Ensure insert of user with connection through new schema updates user in old schema
351 INSERT INTO users (id) VALUES (3);
352 INSERT INTO user_account_connections (user_id, account_id, role) VALUES (3, 1, 'DEVELOPER');
368 WHERE id = 3 AND account_id = 1 AND account_role = 'developer'
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list