From 6fb7adf815eb0da1b900ba327d8269f5aff7ca69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Sun, 11 Nov 2018 20:51:40 +0900 Subject: [PATCH 01/20] implement regex filtering --- diesel_cli/Cargo.toml | 2 + diesel_cli/src/print_schema.rs | 63 +++++++++++++++---- diesel_cli/tests/print_schema.rs | 16 +++++ .../diesel.toml | 4 ++ .../mysql/expected.rs | 15 +++++ .../mysql/schema.sql | 2 + .../postgres/expected.rs | 15 +++++ .../postgres/schema.sql | 2 + .../sqlite/expected.rs | 15 +++++ .../sqlite/schema.sql | 2 + .../diesel.toml | 4 ++ .../mysql/expected.rs | 15 +++++ .../mysql/schema.sql | 2 + .../postgres/expected.rs | 15 +++++ .../postgres/schema.sql | 2 + .../sqlite/expected.rs | 15 +++++ .../sqlite/schema.sql | 2 + 17 files changed, 179 insertions(+), 12 deletions(-) create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/schema.sql create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/schema.sql create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/schema.sql create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/schema.sql create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/schema.sql create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected.rs create mode 100644 diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/schema.sql diff --git a/diesel_cli/Cargo.toml b/diesel_cli/Cargo.toml index 34215990a8c7..bbcacafe6f39 100644 --- a/diesel_cli/Cargo.toml +++ b/diesel_cli/Cargo.toml @@ -29,6 +29,8 @@ url = { version = "2.1.0", optional = true } barrel = { version = ">= 0.5.0", optional = true, features = ["diesel"] } libsqlite3-sys = { version = ">=0.8.0, <0.21.0", optional = true, features = ["min_sqlite_version_3_7_16"] } diffy = "0.2.0" +regex = "1.0.6" +serde_regex = "0.3.1" [dependencies.diesel] version = "~2.0.0" diff --git a/diesel_cli/src/print_schema.rs b/diesel_cli/src/print_schema.rs index b8cdf8c89035..7353589168e2 100644 --- a/diesel_cli/src/print_schema.rs +++ b/diesel_cli/src/print_schema.rs @@ -3,15 +3,20 @@ use crate::config; use crate::infer_schema_internals::*; use serde::de::{self, MapAccess, Visitor}; use serde::{Deserialize, Deserializer}; +use serde_regex::Serde as RegexWrapper; use std::error::Error; use std::fmt::{self, Display, Formatter, Write}; use std::io::Write as IoWrite; const SCHEMA_HEADER: &str = "// @generated automatically by Diesel CLI.\n"; +type Regex = RegexWrapper; + pub enum Filtering { OnlyTables(Vec), + OnlyTableRegexes(Vec), ExceptTables(Vec), + ExceptTableRegexes(Vec), None, } @@ -27,7 +32,13 @@ impl Filtering { match *self { OnlyTables(ref names) => !names.contains(name), + OnlyTableRegexes(ref regexes) => { + !regexes.iter().any(|regex| regex.is_match(&name.sql_name)) + } ExceptTables(ref names) => names.contains(name), + ExceptTableRegexes(ref regexes) => { + regexes.iter().any(|regex| regex.is_match(&name.sql_name)) + } None => false, } } @@ -311,42 +322,70 @@ impl<'de> Deserialize<'de> for Filtering { type Value = Filtering; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str("either only_tables or except_tables") + f.write_str("either only_tables, only_table_regexes, except_tables, or except_table_regexes") } fn visit_map(self, mut map: V) -> Result where V: MapAccess<'de>, { - let mut only_tables = None; - let mut except_tables = None; - while let Some((key, value)) = map.next_entry()? { + let mut only_tables = None::>; + let mut only_table_regexes = None::>; + let mut except_tables = None::>; + let mut except_table_regexes = None::>; + while let Some(key) = map.next_key()? { match key { "only_tables" => { if only_tables.is_some() { return Err(de::Error::duplicate_field("only_tables")); } - only_tables = Some(value); + only_tables = Some(map.next_value()?); + } + "only_table_regexes" => { + if only_table_regexes.is_some() { + return Err(de::Error::duplicate_field("only_table_regexes")); + } + only_table_regexes = Some(map.next_value()?); } "except_tables" => { if except_tables.is_some() { return Err(de::Error::duplicate_field("except_tables")); } - except_tables = Some(value); + except_tables = Some(map.next_value()?); + } + "except_table_regexes" => { + if except_table_regexes.is_some() { + return Err(de::Error::duplicate_field("except_table_regexes")); + } + except_table_regexes = Some(map.next_value()?); } _ => { return Err(de::Error::unknown_field( key, - &["only_tables", "except_tables"], + &[ + "only_tables", + "only_table_regexes", + "except_tables", + "except_table_regexes", + ], )) } } } - match (only_tables, except_tables) { - (Some(_), Some(_)) => Err(de::Error::duplicate_field("except_tables")), - (Some(w), None) => Ok(Filtering::OnlyTables(w)), - (None, Some(b)) => Ok(Filtering::ExceptTables(b)), - (None, None) => Ok(Filtering::None), + match ( + only_tables, + only_table_regexes, + except_tables, + except_table_regexes, + ) { + (Some(t), None, None, None) => Ok(Filtering::OnlyTables(t)), + (None, Some(t), None, None) => Ok(Filtering::OnlyTableRegexes(t)), + (None, None, Some(t), None) => Ok(Filtering::ExceptTables(t)), + (None, None, None, Some(t)) => Ok(Filtering::ExceptTableRegexes(t)), + (None, None, None, None) => Ok(Filtering::None), + _ => Err(de::Error::duplicate_field( + "only_tables, only_table_regexes, except_tables, except_table_regexes", + )), } } } diff --git a/diesel_cli/tests/print_schema.rs b/diesel_cli/tests/print_schema.rs index f2a28834bcf0..441e89f21ce7 100644 --- a/diesel_cli/tests/print_schema.rs +++ b/diesel_cli/tests/print_schema.rs @@ -22,6 +22,14 @@ fn run_infer_schema_include() { ); } +#[test] +fn run_infer_schema_include_regex() { + test_print_schema( + "print_schema_only_table_regexes", + vec!["--with-docs", "-w", "users1"], + ); +} + #[test] fn run_infer_schema_exclude() { test_print_schema( @@ -30,6 +38,14 @@ fn run_infer_schema_exclude() { ); } +#[test] +fn run_infer_schema_exclude_regex() { + test_print_schema( + "print_schema_except_table_regexes", + vec!["--with-docs", "-b", "users1"], + ); +} + #[test] fn run_infer_schema_order() { test_print_schema("print_schema_order", vec!["--with-docs"]); diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml new file mode 100644 index 000000000000..b4a2711a8cb3 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml @@ -0,0 +1,4 @@ +[print_schema] +file = "src/schema.rs" +with_docs = true +filter = { except_table_regexes = [".*1"] } diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/expected.rs b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/expected.rs new file mode 100644 index 000000000000..4b152b02fa4f --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/expected.rs @@ -0,0 +1,15 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users2` table. + /// + /// (Automatically generated by Diesel.) + users2 (id) { + /// The `id` column of the `users2` table. + /// + /// Its SQL type is `Integer`. + /// + /// (Automatically generated by Diesel.) + id -> Integer, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/schema.sql b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/schema.sql new file mode 100644 index 000000000000..556e9f7b54e8 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/mysql/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE users1 (id INTEGER PRIMARY KEY); +CREATE TABLE users2 (id INTEGER PRIMARY KEY); diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/expected.rs b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/expected.rs new file mode 100644 index 000000000000..1a6c144d9a53 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/expected.rs @@ -0,0 +1,15 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users2` table. + /// + /// (Automatically generated by Diesel.) + users2 (id) { + /// The `id` column of the `users2` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + id -> Int4, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/schema.sql b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/schema.sql new file mode 100644 index 000000000000..37d66071ac40 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/postgres/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE users1 (id SERIAL PRIMARY KEY); +CREATE TABLE users2 (id SERIAL PRIMARY KEY); diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected.rs b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected.rs new file mode 100644 index 000000000000..ddc6550c77a7 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/expected.rs @@ -0,0 +1,15 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users2` table. + /// + /// (Automatically generated by Diesel.) + users2 (id) { + /// The `id` column of the `users2` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/schema.sql b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/schema.sql new file mode 100644 index 000000000000..556e9f7b54e8 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/sqlite/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE users1 (id INTEGER PRIMARY KEY); +CREATE TABLE users2 (id INTEGER PRIMARY KEY); diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml new file mode 100644 index 000000000000..01cd76fb5091 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml @@ -0,0 +1,4 @@ +[print_schema] +file = "src/schema.rs" +with_docs = true +filter = { only_table_regexes = [".*1"] } diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/expected.rs b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/expected.rs new file mode 100644 index 000000000000..6d2c006f9504 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/expected.rs @@ -0,0 +1,15 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Integer`. + /// + /// (Automatically generated by Diesel.) + id -> Integer, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/schema.sql b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/schema.sql new file mode 100644 index 000000000000..556e9f7b54e8 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/mysql/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE users1 (id INTEGER PRIMARY KEY); +CREATE TABLE users2 (id INTEGER PRIMARY KEY); diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/expected.rs b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/expected.rs new file mode 100644 index 000000000000..802f38c7c4db --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/expected.rs @@ -0,0 +1,15 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Int4`. + /// + /// (Automatically generated by Diesel.) + id -> Int4, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/schema.sql b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/schema.sql new file mode 100644 index 000000000000..37d66071ac40 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/postgres/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE users1 (id SERIAL PRIMARY KEY); +CREATE TABLE users2 (id SERIAL PRIMARY KEY); diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected.rs b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected.rs new file mode 100644 index 000000000000..27b9cb8818c4 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/expected.rs @@ -0,0 +1,15 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + /// Representation of the `users1` table. + /// + /// (Automatically generated by Diesel.) + users1 (id) { + /// The `id` column of the `users1` table. + /// + /// Its SQL type is `Nullable`. + /// + /// (Automatically generated by Diesel.) + id -> Nullable, + } +} diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/schema.sql b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/schema.sql new file mode 100644 index 000000000000..556e9f7b54e8 --- /dev/null +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/sqlite/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE users1 (id INTEGER PRIMARY KEY); +CREATE TABLE users2 (id INTEGER PRIMARY KEY); From 0b6fee4fb0f1449700c75880000263182b59ee01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Sun, 11 Nov 2018 21:43:06 +0900 Subject: [PATCH 02/20] use absolute path for backward compatibility --- diesel_cli/src/print_schema.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diesel_cli/src/print_schema.rs b/diesel_cli/src/print_schema.rs index 7353589168e2..27990e632649 100644 --- a/diesel_cli/src/print_schema.rs +++ b/diesel_cli/src/print_schema.rs @@ -10,7 +10,7 @@ use std::io::Write as IoWrite; const SCHEMA_HEADER: &str = "// @generated automatically by Diesel CLI.\n"; -type Regex = RegexWrapper; +type Regex = RegexWrapper<::regex::Regex>; pub enum Filtering { OnlyTables(Vec), From 929cd3e5fc1e41d588496040fd19879961d7d60e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Tue, 18 Dec 2018 21:18:24 +0900 Subject: [PATCH 03/20] implement for CLI --- diesel_cli/src/cli.rs | 34 +++++++++++++++++++++++++++++++--- diesel_cli/src/main.rs | 8 +++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index ea11e3826217..2534cf12fd2d 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -124,7 +124,19 @@ pub fn build_cli() -> App<'static, 'static> { .short("o") .long("only-tables") .help("Only include tables from table-name") + .conflicts_with("only-table-regexes") .conflicts_with("except-tables") + .conflicts_with("except-table-regexes") + .conflicts_with("blacklist"), + ) + .arg( + Arg::with_name("only-table-regexes") + .long("only-table-regexes") + .help("Only include tables from table-name that matches regexp") + .conflicts_with("only-tables") + .conflicts_with("whitelist") + .conflicts_with("except-tables") + .conflicts_with("except-table-regexes") .conflicts_with("blacklist"), ) .arg( @@ -132,8 +144,10 @@ pub fn build_cli() -> App<'static, 'static> { .short("w") .long("whitelist") .hidden(true) + .conflicts_with("only-table-regexes") .conflicts_with("blacklist") - .conflicts_with("except-tables"), + .conflicts_with("except-tables") + .conflicts_with("except-table-regexes"), ) .arg( Arg::with_name("except-tables") @@ -141,7 +155,19 @@ pub fn build_cli() -> App<'static, 'static> { .long("except-tables") .help("Exclude tables from table-name") .conflicts_with("only-tables") - .conflicts_with("whitelist"), + .conflicts_with("only-table-regexes") + .conflicts_with("whitelist") + .conflicts_with("except-table-regexes"), + ) + .arg( + Arg::with_name("except-tables-regexes") + .long("except-tables-regexes") + .help("Exclude tables from table-name that matches regex") + .conflicts_with("only-tables") + .conflicts_with("whitelist") + .conflicts_with("only-table-regexes") + .conflicts_with("except-tables") + .conflicts_with("blacklist"), ) .arg( Arg::with_name("blacklist") @@ -149,7 +175,9 @@ pub fn build_cli() -> App<'static, 'static> { .long("blacklist") .hidden(true) .conflicts_with("whitelist") - .conflicts_with("only-tables"), + .conflicts_with("only-tables") + .conflicts_with("only-table-regexes") + .conflicts_with("except-table-regexes"), ) .arg( Arg::with_name("with-docs") diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 789244c25601..708a2bbbdf90 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -84,6 +84,7 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box> { let dir = migrations_dir(matches).unwrap_or_else(handle_error); call_with_conn!(database_url, redo_latest_migration(&dir)); regenerate_schema_if_file_specified(matches)?; + } ("list", Some(_)) => { let database_url = database::database_url(matches); @@ -415,8 +416,13 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { if matches.is_present("only-tables") || matches.is_present("whitelist") { config.filter = Filtering::OnlyTables(filter) - } else if matches.is_present("except-tables") || matches.is_present("blacklist") { + } else if matches.is_present("only-table-regexes") { + config.filter = Filtering::OnlyTableRegexes(filter) + } + else if matches.is_present("except-tables") || matches.is_present("blacklist") { config.filter = Filtering::ExceptTables(filter) + } else if matches.is_present("except-table-regexes") { + config.filter = Filtering::ExceptTableRegexes(filter) } if matches.is_present("with-docs") { From 5f2fd9282769a50970c3509b9684cf20dfa68786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Tue, 29 Jan 2019 18:54:17 +0900 Subject: [PATCH 04/20] compile pass --- diesel_cli/src/main.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 708a2bbbdf90..c822131e4b67 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -28,6 +28,7 @@ mod query_helper; use chrono::*; use clap::{ArgMatches, Shell}; use migrations_internals::{self as migrations, MigrationConnection}; +use regex::Regex; use std::any::Any; use std::error::Error; use std::fmt::Display; @@ -406,6 +407,12 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { }) .collect(); + let filter_regex = matches + .values_of("table-name-regexpes") + .unwrap_or_default() + .map(|table_name_regex| Regex::new(table_name_regex).unwrap().into()) + .collect(); + if matches.is_present("whitelist") { eprintln!("The `whitelist` option has been deprecated and renamed to `only-tables`."); } @@ -416,13 +423,15 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { if matches.is_present("only-tables") || matches.is_present("whitelist") { config.filter = Filtering::OnlyTables(filter) - } else if matches.is_present("only-table-regexes") { - config.filter = Filtering::OnlyTableRegexes(filter) + } + else if matches.is_present("only-table-regexes") { + config.filter = Filtering::OnlyTableRegexes(filter_regex) } else if matches.is_present("except-tables") || matches.is_present("blacklist") { config.filter = Filtering::ExceptTables(filter) - } else if matches.is_present("except-table-regexes") { - config.filter = Filtering::ExceptTableRegexes(filter) + } + else if matches.is_present("except-table-regexes") { + config.filter = Filtering::ExceptTableRegexes(filter_regex) } if matches.is_present("with-docs") { From 93c2a4a4c988792e214b9cf318bd66057e6e8202 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 24 Sep 2020 17:01:07 +0900 Subject: [PATCH 05/20] cargo fmt --- diesel_cli/src/main.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index c822131e4b67..0f18455dc9c6 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -85,7 +85,6 @@ fn run_migration_command(matches: &ArgMatches) -> Result<(), Box> { let dir = migrations_dir(matches).unwrap_or_else(handle_error); call_with_conn!(database_url, redo_latest_migration(&dir)); regenerate_schema_if_file_specified(matches)?; - } ("list", Some(_)) => { let database_url = database::database_url(matches); @@ -423,14 +422,11 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { if matches.is_present("only-tables") || matches.is_present("whitelist") { config.filter = Filtering::OnlyTables(filter) - } - else if matches.is_present("only-table-regexes") { + } else if matches.is_present("only-table-regexes") { config.filter = Filtering::OnlyTableRegexes(filter_regex) - } - else if matches.is_present("except-tables") || matches.is_present("blacklist") { + } else if matches.is_present("except-tables") || matches.is_present("blacklist") { config.filter = Filtering::ExceptTables(filter) - } - else if matches.is_present("except-table-regexes") { + } else if matches.is_present("except-table-regexes") { config.filter = Filtering::ExceptTableRegexes(filter_regex) } From 20423a6401bd87b1ae5a997d0385efb6e0f0c602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Tue, 29 Jan 2019 20:53:54 +0900 Subject: [PATCH 06/20] fix typo --- diesel_cli/src/cli.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index 2534cf12fd2d..ff08d0a8dacf 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -160,8 +160,8 @@ pub fn build_cli() -> App<'static, 'static> { .conflicts_with("except-table-regexes"), ) .arg( - Arg::with_name("except-tables-regexes") - .long("except-tables-regexes") + Arg::with_name("except-table-regexes") + .long("except-table-regexes") .help("Exclude tables from table-name that matches regex") .conflicts_with("only-tables") .conflicts_with("whitelist") From b84dd043f754ca8c71c2254141bc4f91028327dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Tue, 12 Mar 2019 11:55:55 +0900 Subject: [PATCH 07/20] show user friendly message --- diesel_cli/src/main.rs | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 0f18455dc9c6..662cee1c9d42 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -394,23 +394,27 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { config.schema = Some(String::from(schema_name)) } - let filter = matches - .values_of("table-name") - .unwrap_or_default() - .map(|table_name| { - if let Some(schema) = config.schema_name() { - TableName::new(table_name, schema) - } else { - table_name.parse().unwrap() - } - }) - .collect(); - - let filter_regex = matches - .values_of("table-name-regexpes") - .unwrap_or_default() - .map(|table_name_regex| Regex::new(table_name_regex).unwrap().into()) - .collect(); + let filter = { + matches + .values_of("table-name") + .unwrap_or_default() + .map(|table_name| { + if let Some(schema) = config.schema_name() { + TableName::new(table_name, schema) + } else { + table_name.parse().unwrap() + } + }) + .collect() + }; + let filter_regex = || { + matches + .values_of("table-name") + .unwrap_or_default() + .map(|table_name_regex| Regex::new(table_name_regex).map(Into::into)) + .collect::>() + .map_err(|e| format!("invalid argument for table filtering regex: {}", e)) + }; if matches.is_present("whitelist") { eprintln!("The `whitelist` option has been deprecated and renamed to `only-tables`."); @@ -423,11 +427,11 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { if matches.is_present("only-tables") || matches.is_present("whitelist") { config.filter = Filtering::OnlyTables(filter) } else if matches.is_present("only-table-regexes") { - config.filter = Filtering::OnlyTableRegexes(filter_regex) + config.filter = Filtering::OnlyTableRegexes(filter_regex()?) } else if matches.is_present("except-tables") || matches.is_present("blacklist") { config.filter = Filtering::ExceptTables(filter) } else if matches.is_present("except-table-regexes") { - config.filter = Filtering::ExceptTableRegexes(filter_regex) + config.filter = Filtering::ExceptTableRegexes(filter_regex()?) } if matches.is_present("with-docs") { From 8c36e361329bd022b32784b4b5e973683a137bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Mon, 30 Sep 2019 20:12:00 +0900 Subject: [PATCH 08/20] remove old flags and rename -regexes flags --- diesel_cli/src/cli.rs | 24 ------------------------ diesel_cli/src/main.rs | 8 ++++---- diesel_cli/src/print_schema.rs | 34 +++++++--------------------------- 3 files changed, 11 insertions(+), 55 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index ff08d0a8dacf..0e1e4eff8823 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -123,20 +123,8 @@ pub fn build_cli() -> App<'static, 'static> { Arg::with_name("only-tables") .short("o") .long("only-tables") - .help("Only include tables from table-name") - .conflicts_with("only-table-regexes") - .conflicts_with("except-tables") - .conflicts_with("except-table-regexes") - .conflicts_with("blacklist"), - ) - .arg( - Arg::with_name("only-table-regexes") - .long("only-table-regexes") .help("Only include tables from table-name that matches regexp") - .conflicts_with("only-tables") - .conflicts_with("whitelist") .conflicts_with("except-tables") - .conflicts_with("except-table-regexes") .conflicts_with("blacklist"), ) .arg( @@ -153,21 +141,9 @@ pub fn build_cli() -> App<'static, 'static> { Arg::with_name("except-tables") .short("e") .long("except-tables") - .help("Exclude tables from table-name") - .conflicts_with("only-tables") - .conflicts_with("only-table-regexes") - .conflicts_with("whitelist") - .conflicts_with("except-table-regexes"), - ) - .arg( - Arg::with_name("except-table-regexes") - .long("except-table-regexes") .help("Exclude tables from table-name that matches regex") .conflicts_with("only-tables") .conflicts_with("whitelist") - .conflicts_with("only-table-regexes") - .conflicts_with("except-tables") - .conflicts_with("blacklist"), ) .arg( Arg::with_name("blacklist") diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 662cee1c9d42..02765be0b36e 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -424,13 +424,13 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { eprintln!("The `blacklist` option has been deprecated and renamed to `except-tables`."); } - if matches.is_present("only-tables") || matches.is_present("whitelist") { + if matches.is_present("whitelist") { config.filter = Filtering::OnlyTables(filter) - } else if matches.is_present("only-table-regexes") { + } else if matches.is_present("only-tables") { config.filter = Filtering::OnlyTableRegexes(filter_regex()?) - } else if matches.is_present("except-tables") || matches.is_present("blacklist") { + } else if matches.is_present("blacklist") { config.filter = Filtering::ExceptTables(filter) - } else if matches.is_present("except-table-regexes") { + } else if matches.is_present("except-tables") { config.filter = Filtering::ExceptTableRegexes(filter_regex()?) } diff --git a/diesel_cli/src/print_schema.rs b/diesel_cli/src/print_schema.rs index 27990e632649..88180aae3f16 100644 --- a/diesel_cli/src/print_schema.rs +++ b/diesel_cli/src/print_schema.rs @@ -322,17 +322,15 @@ impl<'de> Deserialize<'de> for Filtering { type Value = Filtering; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str("either only_tables, only_table_regexes, except_tables, or except_table_regexes") + f.write_str("either only_tables or except_tables") } fn visit_map(self, mut map: V) -> Result where V: MapAccess<'de>, { - let mut only_tables = None::>; - let mut only_table_regexes = None::>; - let mut except_tables = None::>; - let mut except_table_regexes = None::>; + let mut only_tables = None::>; + let mut except_tables = None::>; while let Some(key) = map.next_key()? { match key { "only_tables" => { @@ -341,32 +339,18 @@ impl<'de> Deserialize<'de> for Filtering { } only_tables = Some(map.next_value()?); } - "only_table_regexes" => { - if only_table_regexes.is_some() { - return Err(de::Error::duplicate_field("only_table_regexes")); - } - only_table_regexes = Some(map.next_value()?); - } "except_tables" => { if except_tables.is_some() { return Err(de::Error::duplicate_field("except_tables")); } except_tables = Some(map.next_value()?); } - "except_table_regexes" => { - if except_table_regexes.is_some() { - return Err(de::Error::duplicate_field("except_table_regexes")); - } - except_table_regexes = Some(map.next_value()?); - } _ => { return Err(de::Error::unknown_field( key, &[ "only_tables", - "only_table_regexes", "except_tables", - "except_table_regexes", ], )) } @@ -374,17 +358,13 @@ impl<'de> Deserialize<'de> for Filtering { } match ( only_tables, - only_table_regexes, except_tables, - except_table_regexes, ) { - (Some(t), None, None, None) => Ok(Filtering::OnlyTables(t)), - (None, Some(t), None, None) => Ok(Filtering::OnlyTableRegexes(t)), - (None, None, Some(t), None) => Ok(Filtering::ExceptTables(t)), - (None, None, None, Some(t)) => Ok(Filtering::ExceptTableRegexes(t)), - (None, None, None, None) => Ok(Filtering::None), + (Some(t), None) => Ok(Filtering::OnlyTableRegexes(t)), + (None, Some(t)) => Ok(Filtering::ExceptTableRegexes(t)), + (None, None => Ok(Filtering::None), _ => Err(de::Error::duplicate_field( - "only_tables, only_table_regexes, except_tables, except_table_regexes", + "only_tables except_tables", )), } } From 8b5e375ad6c5cf37cffe9aa7fc6688a1406190d5 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 24 Sep 2020 17:29:15 +0900 Subject: [PATCH 09/20] cargo fmt --- diesel_cli/src/print_schema.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/diesel_cli/src/print_schema.rs b/diesel_cli/src/print_schema.rs index 88180aae3f16..0d5d5b41bb8a 100644 --- a/diesel_cli/src/print_schema.rs +++ b/diesel_cli/src/print_schema.rs @@ -348,24 +348,16 @@ impl<'de> Deserialize<'de> for Filtering { _ => { return Err(de::Error::unknown_field( key, - &[ - "only_tables", - "except_tables", - ], + &["only_tables", "except_tables"], )) } } } - match ( - only_tables, - except_tables, - ) { + match (only_tables, except_tables) { (Some(t), None) => Ok(Filtering::OnlyTableRegexes(t)), (None, Some(t)) => Ok(Filtering::ExceptTableRegexes(t)), - (None, None => Ok(Filtering::None), - _ => Err(de::Error::duplicate_field( - "only_tables except_tables", - )), + (None, None) => Ok(Filtering::None), + _ => Err(de::Error::duplicate_field("only_tables except_tables")), } } } From 6f9f3eaf312bcdcfdc2180a4c9781944a2e58083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Mon, 30 Sep 2019 20:19:28 +0900 Subject: [PATCH 10/20] update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba53656b8637..20983556fb87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/ and a bound to `QueryableByName` with `FromSqlRow`. +* CLI flags of `only-tables` and `except-tables` are now interpreted as regular expressions. + Similary, `only_tabels` and `except_tables` in `diesel.toml` are treated as regular expressions. + ### Fixed * Many types were incorrectly considered non-aggregate when they should not From 1e7e3dce8042e086963bd4ee916272c69cc9baaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Mon, 30 Sep 2019 20:20:54 +0900 Subject: [PATCH 11/20] update test --- .../print_schema/print_schema_except_table_regexes/diesel.toml | 2 +- .../print_schema/print_schema_only_table_regexes/diesel.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml index b4a2711a8cb3..46e084694dec 100644 --- a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml @@ -1,4 +1,4 @@ [print_schema] file = "src/schema.rs" with_docs = true -filter = { except_table_regexes = [".*1"] } +filter = { except_table = [".*1"] } diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml index 01cd76fb5091..7f722f6befe7 100644 --- a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml @@ -1,4 +1,4 @@ [print_schema] file = "src/schema.rs" with_docs = true -filter = { only_table_regexes = [".*1"] } +filter = { only_table = [".*1"] } From af8c2c8f93c6248b26b9b16efae8128089324ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=BAeen?= <3han5chou7@gmail.com> Date: Mon, 30 Sep 2019 20:22:16 +0900 Subject: [PATCH 12/20] cleanup flags --- diesel_cli/src/cli.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index 0e1e4eff8823..3d99a155c0d8 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -132,10 +132,8 @@ pub fn build_cli() -> App<'static, 'static> { .short("w") .long("whitelist") .hidden(true) - .conflicts_with("only-table-regexes") .conflicts_with("blacklist") .conflicts_with("except-tables") - .conflicts_with("except-table-regexes"), ) .arg( Arg::with_name("except-tables") @@ -152,8 +150,6 @@ pub fn build_cli() -> App<'static, 'static> { .hidden(true) .conflicts_with("whitelist") .conflicts_with("only-tables") - .conflicts_with("only-table-regexes") - .conflicts_with("except-table-regexes"), ) .arg( Arg::with_name("with-docs") From 6a92fa493a31dd6f51a90cd09c91c8429f54fc57 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 24 Sep 2020 18:07:20 +0900 Subject: [PATCH 13/20] fix typo --- .../print_schema/print_schema_except_table_regexes/diesel.toml | 2 +- .../print_schema/print_schema_only_table_regexes/diesel.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml index 46e084694dec..f1ac714489d2 100644 --- a/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml +++ b/diesel_cli/tests/print_schema/print_schema_except_table_regexes/diesel.toml @@ -1,4 +1,4 @@ [print_schema] file = "src/schema.rs" with_docs = true -filter = { except_table = [".*1"] } +filter = { except_tables = [".*1"] } diff --git a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml index 7f722f6befe7..5f058f8cd628 100644 --- a/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml +++ b/diesel_cli/tests/print_schema/print_schema_only_table_regexes/diesel.toml @@ -1,4 +1,4 @@ [print_schema] file = "src/schema.rs" with_docs = true -filter = { only_table = [".*1"] } +filter = { only_tables = [".*1"] } From ecda5cccb8fccf52af37cfca3f82dc0bc80a0bc9 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 24 Sep 2020 18:15:58 +0900 Subject: [PATCH 14/20] cargo fmt --- diesel_cli/src/cli.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index 3d99a155c0d8..e9813f152137 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -133,7 +133,7 @@ pub fn build_cli() -> App<'static, 'static> { .long("whitelist") .hidden(true) .conflicts_with("blacklist") - .conflicts_with("except-tables") + .conflicts_with("except-tables"), ) .arg( Arg::with_name("except-tables") @@ -141,7 +141,7 @@ pub fn build_cli() -> App<'static, 'static> { .long("except-tables") .help("Exclude tables from table-name that matches regex") .conflicts_with("only-tables") - .conflicts_with("whitelist") + .conflicts_with("whitelist"), ) .arg( Arg::with_name("blacklist") @@ -149,7 +149,7 @@ pub fn build_cli() -> App<'static, 'static> { .long("blacklist") .hidden(true) .conflicts_with("whitelist") - .conflicts_with("only-tables") + .conflicts_with("only-tables"), ) .arg( Arg::with_name("with-docs") From 35f176d72367a62cd344105d98b532d9fbb1b125 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 24 Sep 2020 19:35:28 +0900 Subject: [PATCH 15/20] remove closure --- diesel_cli/src/main.rs | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 02765be0b36e..0c9c23e4a184 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -394,27 +394,23 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { config.schema = Some(String::from(schema_name)) } - let filter = { - matches - .values_of("table-name") - .unwrap_or_default() - .map(|table_name| { - if let Some(schema) = config.schema_name() { - TableName::new(table_name, schema) - } else { - table_name.parse().unwrap() - } - }) - .collect() - }; - let filter_regex = || { - matches - .values_of("table-name") - .unwrap_or_default() - .map(|table_name_regex| Regex::new(table_name_regex).map(Into::into)) - .collect::>() - .map_err(|e| format!("invalid argument for table filtering regex: {}", e)) - }; + let filter = matches + .values_of("table-name") + .unwrap_or_default() + .map(|table_name| { + if let Some(schema) = config.schema_name() { + TableName::new(table_name, schema) + } else { + table_name.parse().unwrap() + } + }) + .collect(); + let filter_regex = matches + .values_of("table-name") + .unwrap_or_default() + .map(|table_name_regex| Regex::new(table_name_regex).map(Into::into)) + .collect::>() + .map_err(|e| format!("invalid argument for table filtering regex: {}", e)); if matches.is_present("whitelist") { eprintln!("The `whitelist` option has been deprecated and renamed to `only-tables`."); @@ -427,11 +423,11 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { if matches.is_present("whitelist") { config.filter = Filtering::OnlyTables(filter) } else if matches.is_present("only-tables") { - config.filter = Filtering::OnlyTableRegexes(filter_regex()?) + config.filter = Filtering::OnlyTableRegexes(filter_regex?) } else if matches.is_present("blacklist") { config.filter = Filtering::ExceptTables(filter) } else if matches.is_present("except-tables") { - config.filter = Filtering::ExceptTableRegexes(filter_regex()?) + config.filter = Filtering::ExceptTableRegexes(filter_regex?) } if matches.is_present("with-docs") { From 49d52d6e8a1b9c8da36e413dd7f1f5763cd5e7b4 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 25 Sep 2020 22:41:30 +0900 Subject: [PATCH 16/20] remove `OnlyTables` and `ExceptTables` --- diesel_cli/src/main.rs | 20 ++------------------ diesel_cli/src/print_schema.rs | 4 ---- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 0c9c23e4a184..19706c2938ea 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -384,7 +384,6 @@ fn convert_absolute_path_to_relative(target_path: &Path, mut current_path: &Path } fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { - use crate::infer_schema_internals::TableName; use crate::print_schema::*; let database_url = database::database_url(matches); @@ -394,17 +393,6 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { config.schema = Some(String::from(schema_name)) } - let filter = matches - .values_of("table-name") - .unwrap_or_default() - .map(|table_name| { - if let Some(schema) = config.schema_name() { - TableName::new(table_name, schema) - } else { - table_name.parse().unwrap() - } - }) - .collect(); let filter_regex = matches .values_of("table-name") .unwrap_or_default() @@ -420,13 +408,9 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { eprintln!("The `blacklist` option has been deprecated and renamed to `except-tables`."); } - if matches.is_present("whitelist") { - config.filter = Filtering::OnlyTables(filter) - } else if matches.is_present("only-tables") { + if matches.is_present("whitelist") || matches.is_present("only-tables") { config.filter = Filtering::OnlyTableRegexes(filter_regex?) - } else if matches.is_present("blacklist") { - config.filter = Filtering::ExceptTables(filter) - } else if matches.is_present("except-tables") { + } else if matches.is_present("blacklist") || matches.is_present("except-tables") { config.filter = Filtering::ExceptTableRegexes(filter_regex?) } diff --git a/diesel_cli/src/print_schema.rs b/diesel_cli/src/print_schema.rs index 0d5d5b41bb8a..6efebeede68c 100644 --- a/diesel_cli/src/print_schema.rs +++ b/diesel_cli/src/print_schema.rs @@ -13,9 +13,7 @@ const SCHEMA_HEADER: &str = "// @generated automatically by Diesel CLI.\n"; type Regex = RegexWrapper<::regex::Regex>; pub enum Filtering { - OnlyTables(Vec), OnlyTableRegexes(Vec), - ExceptTables(Vec), ExceptTableRegexes(Vec), None, } @@ -31,11 +29,9 @@ impl Filtering { use self::Filtering::*; match *self { - OnlyTables(ref names) => !names.contains(name), OnlyTableRegexes(ref regexes) => { !regexes.iter().any(|regex| regex.is_match(&name.sql_name)) } - ExceptTables(ref names) => names.contains(name), ExceptTableRegexes(ref regexes) => { regexes.iter().any(|regex| regex.is_match(&name.sql_name)) } From 462582a2a0e566cd81a9fb967694af1b9647f53b Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 25 Sep 2020 22:47:46 +0900 Subject: [PATCH 17/20] fix enum variants name --- diesel_cli/src/main.rs | 4 ++-- diesel_cli/src/print_schema.rs | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 19706c2938ea..e0cb7fd4be04 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -409,9 +409,9 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { } if matches.is_present("whitelist") || matches.is_present("only-tables") { - config.filter = Filtering::OnlyTableRegexes(filter_regex?) + config.filter = Filtering::OnlyTables(filter_regex?) } else if matches.is_present("blacklist") || matches.is_present("except-tables") { - config.filter = Filtering::ExceptTableRegexes(filter_regex?) + config.filter = Filtering::ExceptTables(filter_regex?) } if matches.is_present("with-docs") { diff --git a/diesel_cli/src/print_schema.rs b/diesel_cli/src/print_schema.rs index 6efebeede68c..1a9604df21f9 100644 --- a/diesel_cli/src/print_schema.rs +++ b/diesel_cli/src/print_schema.rs @@ -13,8 +13,8 @@ const SCHEMA_HEADER: &str = "// @generated automatically by Diesel CLI.\n"; type Regex = RegexWrapper<::regex::Regex>; pub enum Filtering { - OnlyTableRegexes(Vec), - ExceptTableRegexes(Vec), + OnlyTables(Vec), + ExceptTables(Vec), None, } @@ -29,12 +29,8 @@ impl Filtering { use self::Filtering::*; match *self { - OnlyTableRegexes(ref regexes) => { - !regexes.iter().any(|regex| regex.is_match(&name.sql_name)) - } - ExceptTableRegexes(ref regexes) => { - regexes.iter().any(|regex| regex.is_match(&name.sql_name)) - } + OnlyTables(ref regexes) => !regexes.iter().any(|regex| regex.is_match(&name.sql_name)), + ExceptTables(ref regexes) => regexes.iter().any(|regex| regex.is_match(&name.sql_name)), None => false, } } @@ -350,8 +346,8 @@ impl<'de> Deserialize<'de> for Filtering { } } match (only_tables, except_tables) { - (Some(t), None) => Ok(Filtering::OnlyTableRegexes(t)), - (None, Some(t)) => Ok(Filtering::ExceptTableRegexes(t)), + (Some(t), None) => Ok(Filtering::OnlyTables(t)), + (None, Some(t)) => Ok(Filtering::ExceptTables(t)), (None, None) => Ok(Filtering::None), _ => Err(de::Error::duplicate_field("only_tables except_tables")), } From bcd22908f7bdda9a94b9c884d744eb0401d015a8 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Sat, 26 Sep 2020 00:37:01 +0900 Subject: [PATCH 18/20] change `filter_regex` to `filter` --- diesel_cli/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index e0cb7fd4be04..d228c82ae27c 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -393,7 +393,7 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { config.schema = Some(String::from(schema_name)) } - let filter_regex = matches + let filter = matches .values_of("table-name") .unwrap_or_default() .map(|table_name_regex| Regex::new(table_name_regex).map(Into::into)) @@ -409,9 +409,9 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { } if matches.is_present("whitelist") || matches.is_present("only-tables") { - config.filter = Filtering::OnlyTables(filter_regex?) + config.filter = Filtering::OnlyTables(filter?) } else if matches.is_present("blacklist") || matches.is_present("except-tables") { - config.filter = Filtering::ExceptTables(filter_regex?) + config.filter = Filtering::ExceptTables(filter?) } if matches.is_present("with-docs") { From 3065e774b30d0c266e6c9c1da6a02df5e49ecaa3 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 28 Sep 2020 23:19:23 +0900 Subject: [PATCH 19/20] remove `whitelist` --- diesel_cli/src/cli.rs | 12 +----------- diesel_cli/src/main.rs | 6 +----- diesel_cli/tests/print_schema.rs | 4 ++-- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index e9813f152137..1ec79e7bb2f5 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -127,28 +127,18 @@ pub fn build_cli() -> App<'static, 'static> { .conflicts_with("except-tables") .conflicts_with("blacklist"), ) - .arg( - Arg::with_name("whitelist") - .short("w") - .long("whitelist") - .hidden(true) - .conflicts_with("blacklist") - .conflicts_with("except-tables"), - ) .arg( Arg::with_name("except-tables") .short("e") .long("except-tables") .help("Exclude tables from table-name that matches regex") - .conflicts_with("only-tables") - .conflicts_with("whitelist"), + .conflicts_with("only-tables"), ) .arg( Arg::with_name("blacklist") .short("b") .long("blacklist") .hidden(true) - .conflicts_with("whitelist") .conflicts_with("only-tables"), ) .arg( diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index d228c82ae27c..72dfe268d6ba 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -400,15 +400,11 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { .collect::>() .map_err(|e| format!("invalid argument for table filtering regex: {}", e)); - if matches.is_present("whitelist") { - eprintln!("The `whitelist` option has been deprecated and renamed to `only-tables`."); - } - if matches.is_present("blacklist") { eprintln!("The `blacklist` option has been deprecated and renamed to `except-tables`."); } - if matches.is_present("whitelist") || matches.is_present("only-tables") { + if matches.is_present("only-tables") { config.filter = Filtering::OnlyTables(filter?) } else if matches.is_present("blacklist") || matches.is_present("except-tables") { config.filter = Filtering::ExceptTables(filter?) diff --git a/diesel_cli/tests/print_schema.rs b/diesel_cli/tests/print_schema.rs index 441e89f21ce7..2bd62b05f5af 100644 --- a/diesel_cli/tests/print_schema.rs +++ b/diesel_cli/tests/print_schema.rs @@ -18,7 +18,7 @@ fn run_infer_schema() { fn run_infer_schema_include() { test_print_schema( "print_schema_only_tables", - vec!["--with-docs", "-w", "users1"], + vec!["--with-docs", "-o", "users1"], ); } @@ -26,7 +26,7 @@ fn run_infer_schema_include() { fn run_infer_schema_include_regex() { test_print_schema( "print_schema_only_table_regexes", - vec!["--with-docs", "-w", "users1"], + vec!["--with-docs", "-o", "users1"], ); } From 6f3fe60e5d0bc3861a3565f7517a0287d69463da Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 28 Sep 2020 23:42:55 +0900 Subject: [PATCH 20/20] remove `blacklist` --- diesel_cli/src/cli.rs | 10 +--------- diesel_cli/src/main.rs | 6 +----- diesel_cli/tests/print_schema.rs | 4 ++-- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/diesel_cli/src/cli.rs b/diesel_cli/src/cli.rs index 1ec79e7bb2f5..6743e54554f0 100644 --- a/diesel_cli/src/cli.rs +++ b/diesel_cli/src/cli.rs @@ -124,8 +124,7 @@ pub fn build_cli() -> App<'static, 'static> { .short("o") .long("only-tables") .help("Only include tables from table-name that matches regexp") - .conflicts_with("except-tables") - .conflicts_with("blacklist"), + .conflicts_with("except-tables"), ) .arg( Arg::with_name("except-tables") @@ -134,13 +133,6 @@ pub fn build_cli() -> App<'static, 'static> { .help("Exclude tables from table-name that matches regex") .conflicts_with("only-tables"), ) - .arg( - Arg::with_name("blacklist") - .short("b") - .long("blacklist") - .hidden(true) - .conflicts_with("only-tables"), - ) .arg( Arg::with_name("with-docs") .long("with-docs") diff --git a/diesel_cli/src/main.rs b/diesel_cli/src/main.rs index 72dfe268d6ba..bffb93bd4e09 100644 --- a/diesel_cli/src/main.rs +++ b/diesel_cli/src/main.rs @@ -400,13 +400,9 @@ fn run_infer_schema(matches: &ArgMatches) -> Result<(), Box> { .collect::>() .map_err(|e| format!("invalid argument for table filtering regex: {}", e)); - if matches.is_present("blacklist") { - eprintln!("The `blacklist` option has been deprecated and renamed to `except-tables`."); - } - if matches.is_present("only-tables") { config.filter = Filtering::OnlyTables(filter?) - } else if matches.is_present("blacklist") || matches.is_present("except-tables") { + } else if matches.is_present("except-tables") { config.filter = Filtering::ExceptTables(filter?) } diff --git a/diesel_cli/tests/print_schema.rs b/diesel_cli/tests/print_schema.rs index 2bd62b05f5af..f4cd6d55f486 100644 --- a/diesel_cli/tests/print_schema.rs +++ b/diesel_cli/tests/print_schema.rs @@ -34,7 +34,7 @@ fn run_infer_schema_include_regex() { fn run_infer_schema_exclude() { test_print_schema( "print_schema_except_tables", - vec!["--with-docs", "-b", "users1"], + vec!["--with-docs", "-e", "users1"], ); } @@ -42,7 +42,7 @@ fn run_infer_schema_exclude() { fn run_infer_schema_exclude_regex() { test_print_schema( "print_schema_except_table_regexes", - vec!["--with-docs", "-b", "users1"], + vec!["--with-docs", "-e", "users1"], ); }