on main help sign in
Break out Index struct from add_index

This makes the format of `add_index` a bit more consistent with `add_column` which has a nested definition for the column. This also sets up for us to allow adding indices in `create_table`.

commitdd90cac03a5b78430940332083fb72d9ba629c8b authorfabianlindfors <fabian@flapplabs.se> dateFri 14 Jan 2022, 11:53 (5 years ago) parent2a3fda7
7 files changed+50−30
README.md+6−4 src/migrations/add_index.rs+10−10 src/migrations/common.rs+8 src/migrations/mod.rs+1−1 tests/add_index.rs+11−7 tests/alter_column.rs+6−4 tests/remove_index.rs+8−4
README.md+6−4
@@ -399,11 +399,13 @@ primary_key = "id"
399399[[actions]]
400400type = "add_index"
401401table = "users"
402−name = "name_idx"
403−columns = ["name"]
404402
405−# Defaults to false
406−unique = true
403+ [actions.index]
404+ name = "name_idx"
405+ columns = ["name"]
406+
407+ # Defaults to false
408+ unique = true
407409```
408410
409411#### Remove index
src/migrations/add_index.rs+10−10
@@ -1,4 +1,4 @@
1−use super::{Action, MigrationContext};
1+use super::{common::Index, Action, MigrationContext};
22use crate::{
33 db::{Conn, Transaction},
44 schema::Schema,
@@ -9,16 +9,16 @@ use serde::{Deserialize, Serialize};
99#[derive(Serialize, Deserialize, Debug)]
1010pub struct AddIndex {
1111 pub table: String,
12− pub name: String,
13− pub columns: Vec<String>,
14− #[serde(default)]
15− pub unique: bool,
12+ pub index: Index,
1613}
1714
1815#[typetag::serde(name = "add_index")]
1916impl Action for AddIndex {
2017 fn describe(&self) -> String {
21− format!("Adding index \"{}\" to table \"{}\"", self.name, self.table)
18+ format!(
19+ "Adding index \"{}\" to table \"{}\"",
20+ self.index.name, self.table
21+ )
2222 }
2323
2424 fn run(
@@ -32,17 +32,17 @@ impl Action for AddIndex {
3232 let column_real_names: Vec<String> = table
3333 .columns
3434 .iter()
35− .filter(|column| self.columns.contains(&column.name))
35+ .filter(|column| self.index.columns.contains(&column.name))
3636 .map(|column| format!("\"{}\"", column.real_name))
3737 .collect();
3838
39− let unique = if self.unique { "UNIQUE" } else { "" };
39+ let unique = if self.index.unique { "UNIQUE" } else { "" };
4040
4141 db.run(&format!(
4242 r#"
4343 CREATE {unique} INDEX CONCURRENTLY "{name}" ON "{table}" ({columns})
4444 "#,
45− name = self.name,
45+ name = self.index.name,
4646 table = self.table,
4747 columns = column_real_names.join(", "),
4848 ))
@@ -65,7 +65,7 @@ impl Action for AddIndex {
6565 r#"
6666 DROP INDEX CONCURRENTLY IF EXISTS "{name}"
6767 "#,
68− name = self.name,
68+ name = self.index.name,
6969 ))
7070 .context("failed to drop index")?;
7171 Ok(())
src/migrations/common.rs+8
@@ -28,6 +28,14 @@ fn nullable_default() -> bool {
2828 true
2929}
3030
31+#[derive(Serialize, Deserialize, Clone, Debug)]
32+pub struct Index {
33+ pub name: String,
34+ pub columns: Vec<String>,
35+ #[serde(default)]
36+ pub unique: bool,
37+}
38+
3139#[derive(Debug)]
3240struct PostgresRawValue {
3341 bytes: Vec<u8>,
src/migrations/mod.rs+1−1
@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
77
88// Re-export migration types
99mod common;
10−pub use common::{Column, ColumnBuilder};
10+pub use common::{Column, ColumnBuilder, Index};
1111
1212mod create_table;
1313pub use create_table::{CreateTable, CreateTableBuilder, ForeignKey};
tests/add_index.rs+11−7
@@ -1,4 +1,4 @@
1−use reshape::migrations::{AddIndex, ColumnBuilder, CreateTableBuilder, Migration};
1+use reshape::migrations::{AddIndex, ColumnBuilder, CreateTableBuilder, Index, Migration};
22
33mod common;
44
@@ -27,9 +27,11 @@ fn add_index() {
2727 );
2828 let add_index_migration = Migration::new("add_name_index", None).with_action(AddIndex {
2929 table: "users".to_string(),
30− name: "name_idx".to_string(),
31− columns: vec!["name".to_string()],
32− unique: false,
30+ index: Index {
31+ name: "name_idx".to_string(),
32+ columns: vec!["name".to_string()],
33+ unique: true,
34+ },
3335 });
3436
3537 let first_migrations = vec![create_table_migration.clone()];
@@ -95,9 +97,11 @@ fn add_index_unique() {
9597 );
9698 let add_index_migration = Migration::new("add_name_index", None).with_action(AddIndex {
9799 table: "users".to_string(),
98− name: "name_idx".to_string(),
99− columns: vec!["name".to_string()],
100− unique: true,
100+ index: Index {
101+ name: "name_idx".to_string(),
102+ columns: vec!["name".to_string()],
103+ unique: true,
104+ },
101105 });
102106
103107 let first_migrations = vec![create_table_migration.clone()];
tests/alter_column.rs+6−4
@@ -1,5 +1,5 @@
11use reshape::migrations::{
2− AddIndex, AlterColumn, ColumnBuilder, ColumnChanges, CreateTableBuilder, Migration,
2+ AddIndex, AlterColumn, ColumnBuilder, ColumnChanges, CreateTableBuilder, Index, Migration,
33};
44
55mod common;
@@ -537,9 +537,11 @@ fn alter_column_with_index() {
537537 )
538538 .with_action(AddIndex {
539539 table: "users".to_string(),
540− name: "users_name_idx".to_string(),
541− columns: vec!["first_name".to_string(), "last_name".to_string()],
542− unique: false,
540+ index: Index {
541+ name: "users_name_idx".to_string(),
542+ columns: vec!["first_name".to_string(), "last_name".to_string()],
543+ unique: false,
544+ },
543545 });
544546 let uppercase_name = Migration::new("uppercase_name", None).with_action(AlterColumn {
545547 table: "users".to_string(),
tests/remove_index.rs+8−4
@@ -1,4 +1,6 @@
1−use reshape::migrations::{AddIndex, ColumnBuilder, CreateTableBuilder, Migration, RemoveIndex};
1+use reshape::migrations::{
2+ AddIndex, ColumnBuilder, CreateTableBuilder, Index, Migration, RemoveIndex,
3+};
24
35mod common;
46
@@ -28,9 +30,11 @@ fn remove_index() {
2830 )
2931 .with_action(AddIndex {
3032 table: "users".to_string(),
31− name: "name_idx".to_string(),
32− columns: vec!["name".to_string()],
33− unique: false,
33+ index: Index {
34+ name: "name_idx".to_string(),
35+ columns: vec!["name".to_string()],
36+ unique: false,
37+ },
3438 });
3539
3640 let remove_index_migration =