Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

splitting into multiple schema.rs #3796

Merged
merged 33 commits into from
Mar 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f304b19
Support multiple schemas in diesel.toml
forest1102 Sep 17, 2023
7cda082
fix errors
forest1102 Sep 18, 2023
1fcd745
Merge branch 'master' into forest1102/issue1728
forest1102 Sep 19, 2023
5a9edd1
Merge branch 'master' into forest1102/issue1728
forest1102 Sep 19, 2023
fb2df8c
Merge branch 'master' into forest1102/issue1728
forest1102 Oct 6, 2023
55e308b
Merge branch 'master' into forest1102/issue1728
forest1102 Nov 17, 2023
f9fa6a5
Revert " Support multiple schemas in diesel.toml"
forest1102 Nov 17, 2023
d06b8aa
Merge branch 'master' into forest1102/issue1728
forest1102 Dec 4, 2023
89b46a5
Merge branch 'master' into forest1102/issue1728
forest1102 Dec 22, 2023
7308f79
enable to use multiple print_schema
forest1102 Dec 26, 2023
3ed6934
Merge branch 'master' into forest1102/issue1728
forest1102 Dec 26, 2023
18d77f1
delete unused package
forest1102 Dec 26, 2023
523cbe9
fix cli parameter error
forest1102 Jan 9, 2024
ff316f2
Merge branch 'diesel-rs:master' into forest1102/issue1728
forest1102 Jan 9, 2024
728c27a
add test for mysql/sqlite
forest1102 Jan 9, 2024
139ee48
fix sqlite snap
forest1102 Jan 9, 2024
cc724fd
update snapshot for mysql
forest1102 Jan 9, 2024
f067e96
Merge remote-tracking branch 'origin/master' into forest1102/issue1728
forest1102 Jan 12, 2024
e748bfe
changes after commit
forest1102 Jan 12, 2024
3044c21
change after commit
forest1102 Jan 12, 2024
15c8a24
fix clippy
forest1102 Jan 12, 2024
96a2d4d
Merge branch 'master' into forest1102/issue1728
forest1102 Jan 13, 2024
206f702
Merge branch 'master' into forest1102/issue1728
forest1102 Jan 17, 2024
a23b136
Merge branch 'master' into forest1102/issue1728
forest1102 Jan 30, 2024
f2fe4a2
Merge branch 'master' into forest1102/issue1728
forest1102 Feb 8, 2024
266a682
Merge branch 'master' into forest1102/issue1728
forest1102 Feb 9, 2024
57edc74
Merge branch 'master' into forest1102/issue1728
forest1102 Feb 25, 2024
2860749
reflect suggestion
forest1102 Feb 25, 2024
2c21c43
Merge remote-tracking branch 'origin/master' into uforest1102/issue1728
forest1102 Feb 28, 2024
5c2ae6d
Merge remote-tracking branch 'origin/master' into forest1102/issue1728
forest1102 Mar 1, 2024
6458a46
changes after merge and refactor generate_sql_based_on_diff_schema
forest1102 Mar 1, 2024
455e863
add changelog
forest1102 Mar 2, 2024
2343a5e
Merge branch 'master' into forest1102/issue1728
forest1102 Mar 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
changes after merge and refactor generate_sql_based_on_diff_schema
Signed-off-by: forest1102 <[email protected]>
forest1102 committed Mar 1, 2024
commit 6458a46f51c3357a8a8408c86b33b1c3edfb9a3b
4 changes: 2 additions & 2 deletions diesel_cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -322,9 +322,9 @@ pub fn build_cli() -> Command {
.default_values(["default"])
.help("select schema key from diesel.toml, use 'default' for print_schema without key."),
weiznich marked this conversation as resolved.
Show resolved Hide resolved
).arg(
Arg::new("sqlite-integer-primary-key-is-bigint")
position_sensitive_flag(Arg::new("sqlite-integer-primary-key-is-bigint"))
.long("sqlite-integer-primary-key-is-bigint")
.action(ArgAction::SetTrue)
.action(ArgAction::Append)
.help(
"For SQLite 3.37 and above, detect `INTEGER PRIMARY KEY` columns as `BigInt`, \
when the table isn't declared with `WITHOUT ROWID`.\n\
78 changes: 53 additions & 25 deletions diesel_cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ impl Config {
.print_schema
.all_configs
.get_mut(&key)
.ok_or(crate::errors::Error::NoPrintSchemaKeyFound(key))?;
.ok_or(crate::errors::Error::NoSchemaKeyFound(key))?;
if let Some(table_names_with_indices) = table_names_with_indices.clone() {
let table_names = table_names_with_indices
.range(boundary)
@@ -142,29 +142,16 @@ impl Config {
}
}
} else {
let print_schema = match self.print_schema.all_configs.entry("default".to_string()) {
Entry::Vacant(entry) => entry.insert(PrintSchema::default()),
Entry::Occupied(entry) => entry.into_mut(),
};
let table_names = matches
.get_many::<String>("table-name")
.unwrap_or_default()
.map(|table_name_regex| regex::Regex::new(table_name_regex).map(Into::into))
.collect::<Result<Vec<Regex>, _>>()?;

if matches
.try_get_one::<bool>("only-tables")?
.cloned()
.unwrap_or(false)
{
print_schema.filter = Filtering::OnlyTables(table_names)
} else if matches
.try_get_one::<bool>("except-tables")?
.cloned()
.unwrap_or(false)
{
print_schema.filter = Filtering::ExceptTables(table_names)
}
let print_schema = self
.print_schema
.all_configs
.entry("default".to_string())
.or_insert(PrintSchema::default().set_filter(matches)?);
let print_schema = print_schema.clone().set_filter(matches)?;
self.print_schema
.all_configs
.entry("default".to_string())
.and_modify(|v| *v = print_schema);
}
Ok(self)
}
@@ -188,6 +175,11 @@ impl Config {
get_values_with_indices::<bool>(matches, "generate-custom-type-definitions")?;
let custom_type_derives_with_indices =
get_values_with_indices::<String>(matches, "custom-type-derives")?;
let sqlite_integer_primary_key_is_bigint_with_indices =
get_values_with_indices::<bool>(
matches,
"sqlite-integer-primary-key-is-bigint",
)?;
for (key, boundary) in selected_schema_keys.values().cloned().zip(
selected_schema_keys
.keys()
@@ -206,7 +198,7 @@ impl Config {
.print_schema
.all_configs
.get_mut(&key)
.ok_or(crate::errors::Error::NoPrintSchemaKeyFound(key))?;
.ok_or(crate::errors::Error::NoSchemaKeyFound(key))?;
if let Some(schema) = schema_with_indices
.clone()
.and_then(|v| v.range(boundary).nth(0).map(|v| v.1.clone()))
@@ -287,6 +279,16 @@ impl Config {
if !custom_type_derives.is_empty() {
print_schema.custom_type_derives = Some(custom_type_derives);
}
if let Some(sqlite_integer_primary_key_is_bigint) =
sqlite_integer_primary_key_is_bigint_with_indices
.clone()
.and_then(|with_docs_with_indices| {
with_docs_with_indices.range(boundary).nth(0).map(|v| *v.1)
})
{
print_schema.sqlite_integer_primary_key_is_bigint =
Some(sqlite_integer_primary_key_is_bigint);
}
}
}
} else {
@@ -337,6 +339,9 @@ impl Config {
let derives = derives.cloned().collect();
config.custom_type_derives = Some(derives);
}
if matches.get_flag("sqlite-integer-primary-key-is-bigint") {
config.sqlite_integer_primary_key_is_bigint = Some(true);
}
}
Ok(self)
}
@@ -458,6 +463,29 @@ impl PrintSchema {
derives
}
}

pub fn set_filter(mut self, matches: &ArgMatches) -> Result<Self, crate::errors::Error> {
let table_names = matches
.get_many::<String>("table-name")
.unwrap_or_default()
.map(|table_name_regex| regex::Regex::new(table_name_regex).map(Into::into))
.collect::<Result<Vec<Regex>, _>>()?;

if matches
.try_get_one::<bool>("only-tables")?
.cloned()
.unwrap_or(false)
{
self.filter = Filtering::OnlyTables(table_names)
} else if matches
.try_get_one::<bool>("except-tables")?
.cloned()
.unwrap_or(false)
{
self.filter = Filtering::ExceptTables(table_names)
}
Ok(self)
}
}

#[derive(Default, Deserialize, Debug)]
4 changes: 2 additions & 2 deletions diesel_cli/src/errors.rs
Original file line number Diff line number Diff line change
@@ -60,6 +60,6 @@ pub enum Error {
UrlParsingError(#[from] url::ParseError),
#[error("Failed to parse CLI parameter: {0}")]
ClapMatchesError(#[from] clap::parser::MatchesError),
#[error(" no `[print_schema.{0}]` entries in your diesel.toml")]
NoPrintSchemaKeyFound(String),
#[error("No `[print_schema.{0}]` entries in your diesel.toml")]
NoSchemaKeyFound(String),
}
47 changes: 15 additions & 32 deletions diesel_cli/src/migrations/diff_schema.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ use std::io::Read;
use std::path::Path;
use syn::visit::Visit;

use crate::config::{Config, PrintSchema};
use crate::config::PrintSchema;
use crate::database::InferConnection;
use crate::infer_schema_internals::{
filter_table_names, load_table_names, ColumnDefinition, ColumnType, ForeignKeyConstraint,
@@ -28,7 +28,7 @@ fn compatible_type_list() -> HashMap<&'static str, Vec<&'static str>> {

#[tracing::instrument]
pub fn generate_sql_based_on_diff_schema(
config: Config,
config: PrintSchema,
matches: &ArgMatches,
schema_file_path: &Path,
) -> Result<(String, String), crate::errors::Error> {
@@ -83,45 +83,28 @@ pub fn generate_sql_based_on_diff_schema(
table_pk_key_list.insert(t.table_name.to_string(), keys);
expected_schema_map.insert(t.table_name.to_string(), t);
}

config.print_schema.with_docs = DocConfig::NoDocComments;
config.print_schema.column_sorting = ColumnSorting::OrdinalPosition;
config.with_docs = DocConfig::NoDocComments;
config.column_sorting = ColumnSorting::OrdinalPosition;

// Parameter `sqlite_integer_primary_key_is_bigint` is only used for a SQLite connection
match conn {
#[cfg(feature = "postgres")]
InferConnection::Pg(_) => config.print_schema.sqlite_integer_primary_key_is_bigint = None,
InferConnection::Pg(_) => config.sqlite_integer_primary_key_is_bigint = None,
#[cfg(feature = "sqlite")]
InferConnection::Sqlite(_) => (),
#[cfg(feature = "mysql")]
InferConnection::Mysql(_) => {
config.print_schema.sqlite_integer_primary_key_is_bigint = None;
config.sqlite_integer_primary_key_is_bigint = None;
}
}

let mut schema_diff = Vec::new();
let table_names = load_table_names(&mut conn, None)?;
let tables_from_database = filter_table_names(
table_names.clone(),
&(if config.print_schema.all_configs.len() > 1 {
config.print_schema.all_configs.get("default")
} else {
config
.print_schema
.all_configs
.first_key_value()
.map(|v| v.1)
})
.map(|v| v.filter.clone())
.unwrap_or_default(),
);
let tables_from_database = filter_table_names(table_names.clone(), &config.filter);
for table in tables_from_database {
tracing::info!(?table, "Diff for existing table");
let columns = crate::infer_schema_internals::load_table_data(
&mut conn,
table.clone(),
&config.print_schema,
)?;
let columns =
crate::infer_schema_internals::load_table_data(&mut conn, table.clone(), &config)?;
if let Some(t) = expected_schema_map.remove(&table.sql_name.to_lowercase()) {
tracing::info!(table = ?t.sql_name, "Table exists in schema.rs");
let mut primary_keys_in_db =
@@ -232,19 +215,19 @@ pub fn generate_sql_based_on_diff_schema(
#[cfg(feature = "postgres")]
InferConnection::Pg(_) => {
let mut qb = diesel::pg::PgQueryBuilder::default();
diff.generate_up_sql(&mut qb, &config.print_schema)?;
diff.generate_up_sql(&mut qb, &config)?;
qb.finish()
}
#[cfg(feature = "sqlite")]
InferConnection::Sqlite(_) => {
let mut qb = diesel::sqlite::SqliteQueryBuilder::default();
diff.generate_up_sql(&mut qb, &config.print_schema)?;
diff.generate_up_sql(&mut qb, &config)?;
qb.finish()
}
#[cfg(feature = "mysql")]
InferConnection::Mysql(_) => {
let mut qb = diesel::mysql::MysqlQueryBuilder::default();
diff.generate_up_sql(&mut qb, &config.print_schema)?;
diff.generate_up_sql(&mut qb, &config)?;
qb.finish()
}
};
@@ -253,19 +236,19 @@ pub fn generate_sql_based_on_diff_schema(
#[cfg(feature = "postgres")]
InferConnection::Pg(_) => {
let mut qb = diesel::pg::PgQueryBuilder::default();
diff.generate_down_sql(&mut qb, &config.print_schema)?;
diff.generate_down_sql(&mut qb, &config)?;
qb.finish()
}
#[cfg(feature = "sqlite")]
InferConnection::Sqlite(_) => {
let mut qb = diesel::sqlite::SqliteQueryBuilder::default();
diff.generate_down_sql(&mut qb, &config.print_schema)?;
diff.generate_down_sql(&mut qb, &config)?;
qb.finish()
}
#[cfg(feature = "mysql")]
InferConnection::Mysql(_) => {
let mut qb = diesel::mysql::MysqlQueryBuilder::default();
diff.generate_down_sql(&mut qb, &config.print_schema)?;
diff.generate_down_sql(&mut qb, &config)?;
qb.finish()
}
};
21 changes: 12 additions & 9 deletions diesel_cli/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -91,30 +91,33 @@ pub(super) fn run_migration_command(matches: &ArgMatches) -> Result<(), crate::e

let (up_sql, down_sql) = if let Some(diff_schema) = args.get_one::<String>("SCHEMA_RS")
{
let mut config = Config::read(matches)?;
let diff_schema = if diff_schema == "NOT_SET" {
if config.print_schema.all_configs.len() > 1 {
let config = Config::read(matches)?;
let mut print_schema =
if let Some(schema_key) = args.get_one::<String>("schema-key") {
config
.print_schema
.all_configs
.get("default")
.and_then(|v| v.file.clone())
.get(schema_key)
.ok_or(crate::errors::Error::NoSchemaKeyFound(schema_key.clone()))?
} else {
config
.print_schema
.all_configs
.first_key_value()
.and_then(|v| v.1.file.clone())
.get("default")
.ok_or(crate::errors::Error::NoSchemaKeyFound("default".into()))?
}
.clone();
let diff_schema = if diff_schema == "NOT_SET" {
print_schema.file.clone()
} else {
Some(PathBuf::from(diff_schema))
};
if args.get_flag("sqlite-integer-primary-key-is-bigint") {
config.print_schema.sqlite_integer_primary_key_is_bigint = Some(true);
print_schema.sqlite_integer_primary_key_is_bigint = Some(true);
}
if let Some(diff_schema) = diff_schema {
self::diff_schema::generate_sql_based_on_diff_schema(
config,
print_schema,
args,
&diff_schema,
)?