Skip to content

Commit

Permalink
Upstream Changes - 4 (#2168)
Browse files Browse the repository at this point in the history
* cli: generate entity ignore default database schema

* fixup

* select result is `i32`

* fix

* clippy

* revert

* revert

* revert

* revert
  • Loading branch information
billy1624 authored Mar 27, 2024
1 parent 6b52288 commit 0ff000b
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 225 deletions.
7 changes: 2 additions & 5 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ pub async fn run_generate_command(
use sqlx::Postgres;

println!("Connecting to Postgres ...");
let schema = database_schema
.as_ref()
.map(|s| s.as_str())
.unwrap_or("public");
let schema = database_schema.as_deref().unwrap_or("public");
let connection =
sqlx_connect::<Postgres>(max_connections, url.as_str(), Some(schema))
.await?;
Expand All @@ -171,7 +168,7 @@ pub async fn run_generate_command(
.filter(|schema| filter_skip_tables(&schema.info.name))
.map(|schema| schema.write())
.collect();
(Some(schema.schema), table_stmts)
(database_schema, table_stmts)
}
_ => unimplemented!("{} is not supported", url.scheme()),
};
Expand Down
76 changes: 3 additions & 73 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,16 +818,9 @@ impl EntityWriter {
}

pub fn gen_schema_name(schema_name: &Option<String>) -> Option<TokenStream> {
match schema_name {
Some(schema_name) => {
if schema_name != "public" {
Some(quote! { #schema_name })
} else {
None
}
}
None => None,
}
schema_name
.as_ref()
.map(|schema_name| quote! { #schema_name })
}
}

Expand Down Expand Up @@ -1569,27 +1562,6 @@ mod tests {
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_expanded_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
false,
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES_WITH_SCHEMA_NAME[i].as_bytes())?.to_string(),
EntityWriter::gen_expanded_code_blocks(
Expand Down Expand Up @@ -1674,27 +1646,6 @@ mod tests {
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
false,
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES_WITH_SCHEMA_NAME[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
Expand Down Expand Up @@ -2344,27 +2295,6 @@ mod tests {
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
false,
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES_EXPANDED[i].as_bytes())?.to_string(),
EntityWriter::gen_expanded_code_blocks(
Expand Down
22 changes: 2 additions & 20 deletions sea-orm-migration/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::S
/// Helper struct for writing migration scripts in migration file
pub struct SchemaManager<'c> {
conn: SchemaManagerConnection<'c>,
schema: Option<String>,
}

impl<'c> SchemaManager<'c> {
Expand All @@ -21,7 +20,6 @@ impl<'c> SchemaManager<'c> {
{
Self {
conn: conn.into_schema_manager_connection(),
schema: None,
}
}

Expand All @@ -40,18 +38,6 @@ impl<'c> SchemaManager<'c> {
pub fn get_connection(&self) -> &SchemaManagerConnection<'c> {
&self.conn
}

pub fn set_schema<T>(&mut self, schema: T) -> &mut Self
where
T: Into<String>,
{
self.schema = Some(schema.into());
self
}

pub fn get_schema(&self) -> &Option<String> {
&self.schema
}
}

/// Schema Creation
Expand Down Expand Up @@ -114,7 +100,7 @@ impl<'c> SchemaManager<'c> {
where
T: AsRef<str>,
{
has_table(&self.conn, self.schema.as_deref(), table).await
has_table(&self.conn, table).await
}

pub async fn has_column<T, C>(&self, table: T, column: C) -> Result<bool, DbErr>
Expand Down Expand Up @@ -160,11 +146,7 @@ impl<'c> SchemaManager<'c> {
}
}

pub(crate) async fn has_table<C, T>(
conn: &C,
_schema: Option<&str>,
table: T,
) -> Result<bool, DbErr>
pub(crate) async fn has_table<C, T>(conn: &C, table: T) -> Result<bool, DbErr>
where
C: ConnectionTrait,
T: AsRef<str>,
Expand Down
9 changes: 2 additions & 7 deletions tests/bits_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod common;

use common::features::*;
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection, NotSet};
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};

#[sea_orm_macros::test]
#[cfg(feature = "sqlx-postgres")]
Expand All @@ -26,12 +26,7 @@ pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> {
bit64: 64,
};

let res = bits::ActiveModel {
id: NotSet,
..bits.clone().into_active_model()
}
.insert(db)
.await?;
let res = bits.clone().into_active_model().insert(db).await?;

let model = Bits::find().one(db).await?;
assert_eq!(model, Some(res));
Expand Down
20 changes: 7 additions & 13 deletions tests/delete_by_id_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod common;
pub use common::{features::*, setup::*, TestContext};
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel, NotSet};
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel};

#[sea_orm_macros::test]
async fn main() -> Result<(), DbErr> {
Expand All @@ -21,12 +21,9 @@ pub async fn create_and_delete_applog(db: &DatabaseConnection) -> Result<(), DbE
created_at: "2021-09-17T17:50:20+08:00".parse().unwrap(),
};

Applog::insert(applog::ActiveModel {
id: NotSet,
..log1.clone().into_active_model()
})
.exec(db)
.await?;
Applog::insert(log1.clone().into_active_model())
.exec(db)
.await?;

let log2 = applog::Model {
id: 2,
Expand All @@ -35,12 +32,9 @@ pub async fn create_and_delete_applog(db: &DatabaseConnection) -> Result<(), DbE
created_at: "2022-09-17T17:50:20+08:00".parse().unwrap(),
};

Applog::insert(applog::ActiveModel {
id: NotSet,
..log2.clone().into_active_model()
})
.exec(db)
.await?;
Applog::insert(log2.clone().into_active_model())
.exec(db)
.await?;

let delete_res = Applog::delete_by_id(2).exec(db).await?;
assert_eq!(delete_res.rows_affected, 1);
Expand Down
9 changes: 3 additions & 6 deletions tests/dyn_table_name_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod common;
pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{
entity::prelude::*, DatabaseConnection, Delete, IntoActiveModel, Iterable, NotSet, QueryTrait,
Set, Update,
entity::prelude::*, DatabaseConnection, Delete, IntoActiveModel, Iterable, QueryTrait, Set,
Update,
};
use sea_query::{Expr, Query};

Expand All @@ -31,10 +31,7 @@ pub async fn dyn_table_name_lazy_static(db: &DatabaseConnection) -> Result<(), D
name: "1st Row".into(),
};
// Prepare insert statement
let mut insert = Entity::insert(ActiveModel {
id: NotSet,
..model.clone().into_active_model()
});
let mut insert = Entity::insert(model.clone().into_active_model());
// Reset the table name of insert statement
insert.query().into_table(entity.table_ref());
// Execute the insert statement
Expand Down
9 changes: 2 additions & 7 deletions tests/event_trigger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use common::{
TestContext,
};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection, NotSet};
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};

#[sea_orm_macros::test]
#[cfg(all(feature = "sqlx-postgres", feature = "postgres-array"))]
Expand All @@ -33,12 +33,7 @@ pub async fn insert_event_trigger(db: &DatabaseConnection) -> Result<(), DbErr>
),
};

let result = event_trigger::ActiveModel {
id: NotSet,
..event_trigger.clone().into_active_model()
}
.insert(db)
.await?;
let result = event_trigger.clone().into_active_model().insert(db).await?;

assert_eq!(result, event_trigger);

Expand Down
16 changes: 3 additions & 13 deletions tests/json_struct_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod common;

pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection, NotSet};
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use serde_json::json;

#[sea_orm_macros::test]
Expand Down Expand Up @@ -41,12 +41,7 @@ pub async fn insert_json_struct_1(db: &DatabaseConnection) -> Result<(), DbErr>
}),
};

let result = ActiveModel {
id: NotSet,
..model.clone().into_active_model()
}
.insert(db)
.await?;
let result = model.clone().into_active_model().insert(db).await?;

assert_eq!(result, model);

Expand Down Expand Up @@ -81,12 +76,7 @@ pub async fn insert_json_struct_2(db: &DatabaseConnection) -> Result<(), DbErr>
json_value_opt: None,
};

let result = ActiveModel {
id: NotSet,
..model.clone().into_active_model()
}
.insert(db)
.await?;
let result = model.clone().into_active_model().insert(db).await?;

assert_eq!(result, model);

Expand Down
17 changes: 3 additions & 14 deletions tests/json_vec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ pub async fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> {
])),
};

let result = json_vec::ActiveModel {
id: NotSet,
..json_vec.clone().into_active_model()
}
.insert(db)
.await?;
let result = json_vec.clone().into_active_model().insert(db).await?;

assert_eq!(result, json_vec);

Expand Down Expand Up @@ -77,7 +72,7 @@ pub async fn insert_json_string_vec_derive(db: &DatabaseConnection) -> Result<()

pub async fn insert_json_struct_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
let json_vec = json_vec_derive::json_struct_vec::Model {
id: 1,
id: 2,
struct_vec: vec![
json_vec_derive::json_struct_vec::JsonColumn {
value: "4".to_string(),
Expand All @@ -91,13 +86,7 @@ pub async fn insert_json_struct_vec_derive(db: &DatabaseConnection) -> Result<()
],
};

let result = json_vec_derive::json_struct_vec::ActiveModel {
id: NotSet,
..json_vec.clone().into_active_model()
}
.insert(db)
.await?;
assert_eq!(result, json_vec);
let result = json_vec.clone().into_active_model().insert(db).await?;

let model = json_vec_derive::json_struct_vec::Entity::find()
.filter(json_vec_derive::json_struct_vec::Column::Id.eq(json_vec.id))
Expand Down
9 changes: 2 additions & 7 deletions tests/pi_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod common;

use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection, NotSet};
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use std::str::FromStr;

#[sea_orm_macros::test]
Expand All @@ -24,12 +24,7 @@ pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr>
big_decimal_opt: None,
};

let res = pi::ActiveModel {
id: NotSet,
..pi.clone().into_active_model()
}
.insert(db)
.await?;
let res = pi.clone().into_active_model().insert(db).await?;

let model = Pi::find().one(db).await?;
assert_eq!(model, Some(res));
Expand Down
Loading

0 comments on commit 0ff000b

Please sign in to comment.