@@ -4,76 +4,59 @@ mod common;
44
55#[test]
66fn add_column() {
7− let (mut reshape, mut old_db, mut new_db) = common::setup();
8−
9− let create_users_table = Migration::new("create_user_table", None).with_action(
10− CreateTableBuilder::default()
11− .name("users")
12− .primary_key(vec!["id".to_string()])
13− .columns(vec![
14− ColumnBuilder::default()
15− .name("id")
16− .data_type("INTEGER")
17− .build()
18− .unwrap(),
19− ColumnBuilder::default()
20− .name("name")
21− .data_type("TEXT")
22− .build()
23− .unwrap(),
24− ])
25− .build()
26− .unwrap(),
7+ let mut test = common::Test::new("Add column");
8+
9+ test.first_migration(
10+ Migration::new("create_user_table", None).with_action(
11+ CreateTableBuilder::default()
12+ .name("users")
13+ .primary_key(vec!["id".to_string()])
14+ .columns(vec![
15+ ColumnBuilder::default()
16+ .name("id")
17+ .data_type("INTEGER")
18+ .build()
19+ .unwrap(),
20+ ColumnBuilder::default()
21+ .name("name")
22+ .data_type("TEXT")
23+ .build()
24+ .unwrap(),
25+ ])
26+ .build()
27+ .unwrap(),
28+ ),
2729 );
2830
29− let add_first_last_name_columns = Migration::new("add_first_and_last_name_columns", None)
30− .with_action(AddColumn {
31− table: "users".to_string(),
32− column: Column {
33− name: "first".to_string(),
34− data_type: "TEXT".to_string(),
35− nullable: false,
36− default: None,
37− generated: None,
38− },
39− up: Some("(STRING_TO_ARRAY(name, ' '))[1]".to_string()),
40− })
41− .with_action(AddColumn {
42− table: "users".to_string(),
43− column: Column {
44− name: "last".to_string(),
45− data_type: "TEXT".to_string(),
46− nullable: false,
47− default: None,
48− generated: None,
49− },
50− up: Some("(STRING_TO_ARRAY(name, ' '))[2]".to_string()),
51− });
52−
53− let first_migrations = vec![create_users_table.clone()];
54− let second_migrations = vec![
55− create_users_table.clone(),
56− add_first_last_name_columns.clone(),
57− ];
58−
59− // Run first migration, should automatically finish
60− reshape.migrate(first_migrations.clone()).unwrap();
61−
62− // Update search paths
63− old_db
64− .simple_query(&reshape::schema_query_for_migration(
65− &first_migrations.last().unwrap().name,
66− ))
67− .unwrap();
68− new_db
69− .simple_query(&reshape::schema_query_for_migration(
70− &first_migrations.last().unwrap().name,
71− ))
72− .unwrap();
31+ test.second_migration(
32+ Migration::new("add_first_and_last_name_columns", None)
33+ .with_action(AddColumn {
34+ table: "users".to_string(),
35+ column: Column {
36+ name: "first".to_string(),
37+ data_type: "TEXT".to_string(),
38+ nullable: false,
39+ default: None,
40+ generated: None,
41+ },
42+ up: Some("(STRING_TO_ARRAY(name, ' '))[1]".to_string()),
43+ })
44+ .with_action(AddColumn {
45+ table: "users".to_string(),
46+ column: Column {
47+ name: "last".to_string(),
48+ data_type: "TEXT".to_string(),
49+ nullable: false,
50+ default: None,
51+ generated: None,
52+ },
53+ up: Some("(STRING_TO_ARRAY(name, ' '))[2]".to_string()),
54+ }),
55+ );
7356
74− // Insert some test users
75− new_db
76− .simple_query(
57+ test.after_first(|db| {
58+ // Insert some test users
59+ db.simple_query(
7760 "
7861 INSERT INTO users (id, name) VALUES
7962 (1, 'John Doe'),
@@ -81,39 +64,53 @@ fn add_column() {
8164 ",
8265 )
8366 .unwrap();
67+ });
8468
85− // Run second migration
86− reshape.migrate(second_migrations.clone()).unwrap();
87− new_db
88− .simple_query(&reshape::schema_query_for_migration(
89− &second_migrations.last().unwrap().name,
90− ))
91− .unwrap();
69+ test.intermediate(|old_db, new_db| {
70+ // Check that the existing users have the new columns populated
71+ let expected = vec![("John", "Doe"), ("Jane", "Doe")];
72+ assert!(new_db
73+ .query("SELECT first, last FROM users ORDER BY id", &[],)
74+ .unwrap()
75+ .iter()
76+ .map(|row| (row.get("first"), row.get("last")))
77+ .eq(expected));
78+
79+ // Insert data using old schema and make sure the new columns are populated
80+ old_db
81+ .simple_query("INSERT INTO users (id, name) VALUES (3, 'Test Testsson')")
82+ .unwrap();
83+ let (first_name, last_name): (String, String) = new_db
84+ .query_one("SELECT first, last from users WHERE id = 3", &[])
85+ .map(|row| (row.get("first"), row.get("last")))
86+ .unwrap();
87+ assert_eq!(
88+ ("Test", "Testsson"),
89+ (first_name.as_ref(), last_name.as_ref())
90+ );
91+ });
9292
93− // Check that the existing users have the new columns populated
94− let expected = vec![("John", "Doe"), ("Jane", "Doe")];
95− assert!(new_db
96− .query("SELECT first, last FROM users ORDER BY id", &[],)
97− .unwrap()
98− .iter()
99− .map(|row| (row.get("first"), row.get("last")))
100− .eq(expected));
93+ test.after_completion(|db| {
94+ let expected = vec![("John", "Doe"), ("Jane", "Doe"), ("Test", "Testsson")];
95+ assert!(db
96+ .query("SELECT first, last FROM users ORDER BY id", &[],)
97+ .unwrap()
98+ .iter()
99+ .map(|row| (row.get("first"), row.get("last")))
100+ .eq(expected));
101+ });
101102
102− // Insert data using old schema and make sure the new columns are populated
103− old_db
104− .simple_query("INSERT INTO users (id, name) VALUES (3, 'Test Testsson')")
105− .unwrap();
106− let (first_name, last_name): (String, String) = new_db
107− .query_one("SELECT first, last from users WHERE id = 3", &[])
108− .map(|row| (row.get("first"), row.get("last")))
109− .unwrap();
110− assert_eq!(
111− ("Test", "Testsson"),
112− (first_name.as_ref(), last_name.as_ref())
113− );
103+ test.after_abort(|db| {
104+ let expected = vec![("John Doe"), ("Jane Doe"), ("Test Testsson")];
105+ assert!(db
106+ .query("SELECT name FROM users ORDER BY id", &[],)
107+ .unwrap()
108+ .iter()
109+ .map(|row| row.get::<'_, _, String>("name"))
110+ .eq(expected));
111+ });
114112
115− reshape.complete().unwrap();
116− common::assert_cleaned_up(&mut new_db);
113+ test.run()
117114}
118115
119116#[test]