2021-10-19 17:32:37 +03:00
|
|
|
use reshape::migrations::{Column, CreateTable, Migration, RemoveColumn};
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_column() {
|
|
|
|
let (mut reshape, mut old_db, mut new_db) = common::setup();
|
|
|
|
|
|
|
|
let create_table_migration =
|
|
|
|
Migration::new("create_users_table", None).with_action(CreateTable {
|
|
|
|
name: "users".to_string(),
|
2021-11-09 01:32:14 +03:00
|
|
|
primary_key: vec!["id".to_string()],
|
2021-10-27 01:10:02 +03:00
|
|
|
foreign_keys: vec![],
|
2021-10-19 17:32:37 +03:00
|
|
|
columns: vec![
|
|
|
|
Column {
|
|
|
|
name: "id".to_string(),
|
2021-12-28 18:01:11 +03:00
|
|
|
data_type: "INTEGER".to_string(),
|
|
|
|
nullable: true,
|
2021-10-19 17:32:37 +03:00
|
|
|
default: None,
|
2021-12-28 18:01:11 +03:00
|
|
|
generated: None,
|
2021-10-19 17:32:37 +03:00
|
|
|
},
|
|
|
|
Column {
|
|
|
|
name: "name".to_string(),
|
|
|
|
data_type: "TEXT".to_string(),
|
|
|
|
nullable: false,
|
|
|
|
default: None,
|
2021-12-28 18:01:11 +03:00
|
|
|
generated: None,
|
2021-10-19 17:32:37 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
let remove_column_migration =
|
|
|
|
Migration::new("remove_name_column", None).with_action(RemoveColumn {
|
|
|
|
table: "users".to_string(),
|
|
|
|
column: "name".to_string(),
|
|
|
|
down: Some("'TEST_DOWN_VALUE'".to_string()),
|
|
|
|
});
|
|
|
|
|
|
|
|
let first_migrations = vec![create_table_migration.clone()];
|
|
|
|
let second_migrations = vec![
|
|
|
|
create_table_migration.clone(),
|
|
|
|
remove_column_migration.clone(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Run migrations
|
|
|
|
reshape.migrate(first_migrations.clone()).unwrap();
|
|
|
|
reshape.migrate(second_migrations.clone()).unwrap();
|
|
|
|
|
|
|
|
// Update schemas of Postgres connections
|
2021-11-11 22:47:07 +03:00
|
|
|
let old_schema_query =
|
|
|
|
reshape::schema_query_for_migration(&first_migrations.last().unwrap().name);
|
|
|
|
let new_schema_query =
|
|
|
|
reshape::schema_query_for_migration(&second_migrations.last().unwrap().name);
|
2021-10-19 17:32:37 +03:00
|
|
|
old_db.simple_query(&old_schema_query).unwrap();
|
|
|
|
new_db.simple_query(&new_schema_query).unwrap();
|
|
|
|
|
|
|
|
// Insert using old schema and ensure it can be retrieved through new schema
|
|
|
|
old_db
|
2021-12-28 18:01:11 +03:00
|
|
|
.simple_query("INSERT INTO users(id, name) VALUES (1, 'John Doe')")
|
2021-10-19 17:32:37 +03:00
|
|
|
.unwrap();
|
|
|
|
let results = new_db
|
|
|
|
.query("SELECT id FROM users WHERE id = 1", &[])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(1, results.len());
|
|
|
|
assert_eq!(1, results[0].get::<_, i32>("id"));
|
|
|
|
|
|
|
|
// Ensure the name column is not accesible through the new schema
|
|
|
|
assert!(new_db.query("SELECT id, name FROM users", &[]).is_err());
|
|
|
|
|
|
|
|
// Insert using new schema and ensure the down function is correctly applied
|
|
|
|
new_db
|
2021-12-28 18:01:11 +03:00
|
|
|
.simple_query("INSERT INTO users(id) VALUES (2)")
|
2021-10-19 17:32:37 +03:00
|
|
|
.unwrap();
|
|
|
|
let result = old_db
|
|
|
|
.query_opt("SELECT name FROM users WHERE id = 2", &[])
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
Some("TEST_DOWN_VALUE"),
|
|
|
|
result.as_ref().map(|row| row.get("name"))
|
|
|
|
);
|
|
|
|
|
|
|
|
reshape.complete_migration().unwrap();
|
|
|
|
}
|