on main help sign in
Break out Index struct from add_index
dd90cacfabianlindfors
README.md+6−4
@@ -399,11 +399,13 @@ primary_key = "id"
399[[actions]]
399[[actions]]
400type = "add_index"
400type = "add_index"
401table = "users"
401table = "users"
402−name = "name_idx"
403−columns = ["name"]
404
402
403+ [actions.index]
404+ name = "name_idx"
405+ columns = ["name"]
406+
405−# Defaults to false
407+ # Defaults to false
406−unique = true
408+ unique = true
407```
409```
408
410
409#### Remove index
411#### 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};
2use crate::{
2use crate::{
3 db::{Conn, Transaction},
3 db::{Conn, Transaction},
4 schema::Schema,
4 schema::Schema,
@@ -9,16 +9,16 @@ use serde::{Deserialize, Serialize};
9#[derive(Serialize, Deserialize, Debug)]
9#[derive(Serialize, Deserialize, Debug)]
10pub struct AddIndex {
10pub struct AddIndex {
11 pub table: String,
11 pub table: String,
12− pub name: String,
12+ pub index: Index,
13− pub columns: Vec<String>,
14− #[serde(default)]
15− pub unique: bool,
16}
13}
17
14
18#[typetag::serde(name = "add_index")]
15#[typetag::serde(name = "add_index")]
19impl Action for AddIndex {
16impl Action for AddIndex {
20 fn describe(&self) -> String {
17 fn describe(&self) -> String {
18+ format!(
21− format!("Adding index \"{}\" to table \"{}\"", self.name, self.table)
19+ "Adding index \"{}\" to table \"{}\"",
20+ self.index.name, self.table
21+ )
22 }
22 }
23
23
24 fn run(
24 fn run(
@@ -32,17 +32,17 @@ impl Action for AddIndex {
32 let column_real_names: Vec<String> = table
32 let column_real_names: Vec<String> = table
33 .columns
33 .columns
34 .iter()
34 .iter()
35− .filter(|column| self.columns.contains(&column.name))
35+ .filter(|column| self.index.columns.contains(&column.name))
36 .map(|column| format!("\"{}\"", column.real_name))
36 .map(|column| format!("\"{}\"", column.real_name))
37 .collect();
37 .collect();
38
38
39− let unique = if self.unique { "UNIQUE" } else { "" };
39+ let unique = if self.index.unique { "UNIQUE" } else { "" };
40
40
41 db.run(&format!(
41 db.run(&format!(
42 r#"
42 r#"
43 CREATE {unique} INDEX CONCURRENTLY "{name}" ON "{table}" ({columns})
43 CREATE {unique} INDEX CONCURRENTLY "{name}" ON "{table}" ({columns})
44 "#,
44 "#,
45− name = self.name,
45+ name = self.index.name,
46 table = self.table,
46 table = self.table,
47 columns = column_real_names.join(", "),
47 columns = column_real_names.join(", "),
48 ))
48 ))
@@ -65,7 +65,7 @@ impl Action for AddIndex {
65 r#"
65 r#"
66 DROP INDEX CONCURRENTLY IF EXISTS "{name}"
66 DROP INDEX CONCURRENTLY IF EXISTS "{name}"
67 "#,
67 "#,
68− name = self.name,
68+ name = self.index.name,
69 ))
69 ))
70 .context("failed to drop index")?;
70 .context("failed to drop index")?;
71 Ok(())
71 Ok(())
src/migrations/common.rs+8
@@ -28,6 +28,14 @@ fn nullable_default() -> bool {
28 true
28 true
29}
29}
30
30
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+
31#[derive(Debug)]
39#[derive(Debug)]
32struct PostgresRawValue {
40struct PostgresRawValue {
33 bytes: Vec<u8>,
41 bytes: Vec<u8>,
src/migrations/mod.rs+1−1
@@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
7
7
8// Re-export migration types
8// Re-export migration types
9mod common;
9mod common;
10−pub use common::{Column, ColumnBuilder};
10+pub use common::{Column, ColumnBuilder, Index};
11
11
12mod create_table;
12mod create_table;
13pub use create_table::{CreateTable, CreateTableBuilder, ForeignKey};
13pub 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};
2
2
3mod common;
3mod common;
4
4
@@ -27,9 +27,11 @@ fn add_index() {
27 );
27 );
28 let add_index_migration = Migration::new("add_name_index", None).with_action(AddIndex {
28 let add_index_migration = Migration::new("add_name_index", None).with_action(AddIndex {
29 table: "users".to_string(),
29 table: "users".to_string(),
30+ index: Index {
30− name: "name_idx".to_string(),
31+ name: "name_idx".to_string(),
31− columns: vec!["name".to_string()],
32+ columns: vec!["name".to_string()],
32− unique: false,
33+ unique: true,
34+ },
33 });
35 });
34
36
35 let first_migrations = vec![create_table_migration.clone()];
37 let first_migrations = vec![create_table_migration.clone()];
@@ -95,9 +97,11 @@ fn add_index_unique() {
95 );
97 );
96 let add_index_migration = Migration::new("add_name_index", None).with_action(AddIndex {
98 let add_index_migration = Migration::new("add_name_index", None).with_action(AddIndex {
97 table: "users".to_string(),
99 table: "users".to_string(),
100+ index: Index {
98− name: "name_idx".to_string(),
101+ name: "name_idx".to_string(),
99− columns: vec!["name".to_string()],
102+ columns: vec!["name".to_string()],
100− unique: true,
103+ unique: true,
104+ },
101 });
105 });
102
106
103 let first_migrations = vec![create_table_migration.clone()];
107 let first_migrations = vec![create_table_migration.clone()];
tests/alter_column.rs+6−4
@@ -1,5 +1,5 @@
1use reshape::migrations::{
1use reshape::migrations::{
2− AddIndex, AlterColumn, ColumnBuilder, ColumnChanges, CreateTableBuilder, Migration,
2+ AddIndex, AlterColumn, ColumnBuilder, ColumnChanges, CreateTableBuilder, Index, Migration,
3};
3};
4
4
5mod common;
5mod common;
@@ -537,9 +537,11 @@ fn alter_column_with_index() {
537 )
537 )
538 .with_action(AddIndex {
538 .with_action(AddIndex {
539 table: "users".to_string(),
539 table: "users".to_string(),
540+ index: Index {
540− name: "users_name_idx".to_string(),
541+ name: "users_name_idx".to_string(),
541− columns: vec!["first_name".to_string(), "last_name".to_string()],
542+ columns: vec!["first_name".to_string(), "last_name".to_string()],
542− unique: false,
543+ unique: false,
544+ },
543 });
545 });
544 let uppercase_name = Migration::new("uppercase_name", None).with_action(AlterColumn {
546 let uppercase_name = Migration::new("uppercase_name", None).with_action(AlterColumn {
545 table: "users".to_string(),
547 table: "users".to_string(),
tests/remove_index.rs+8−4
@@ -1,4 +1,6 @@
1+use reshape::migrations::{
1−use reshape::migrations::{AddIndex, ColumnBuilder, CreateTableBuilder, Migration, RemoveIndex};
2+ AddIndex, ColumnBuilder, CreateTableBuilder, Index, Migration, RemoveIndex,
3+};
2
4
3mod common;
5mod common;
4
6
@@ -28,9 +30,11 @@ fn remove_index() {
28 )
30 )
29 .with_action(AddIndex {
31 .with_action(AddIndex {
30 table: "users".to_string(),
32 table: "users".to_string(),
33+ index: Index {
31− name: "name_idx".to_string(),
34+ name: "name_idx".to_string(),
32− columns: vec!["name".to_string()],
35+ columns: vec!["name".to_string()],
33− unique: false,
36+ unique: false,
37+ },
34 });
38 });
35
39
36 let remove_index_migration =
40 let remove_index_migration =