Skip to content

Commit

Permalink
Merge branch 'SeaQL:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Diwakar-Gupta committed Jan 19, 2023
2 parents 671eb8e + fce1c76 commit 2408bf9
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 31 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
* Added `DatabaseConnection::close` https://github.com/SeaQL/sea-orm/pull/1236
* Refactor schema module to expose functions for database alteration https://github.com/SeaQL/sea-orm/pull/1256
* Added `is_null` getter for `ColumnDef` struct https://github.com/SeaQL/sea-orm/pull/1381
* Postgres insert many will throw `RecordNotInserted` error if non of them are being inserted https://github.com/SeaQL/sea-orm/pull/1021
* `MockDatabase::append_exec_results()`, `MockDatabase::append_query_results()`, `MockDatabase::append_exec_errors()` and `MockDatabase::append_query_errors()` take any types implemented `IntoIterator` trait https://github.com/SeaQL/sea-orm/pull/1367
* `find_by_id` and `delete_by_id` take any Into primary key value https://github.com/SeaQL/sea-orm/pull/1362
* Added `ActiveValue::reset` to convert `Unchanged` into `Set` https://github.com/SeaQL/sea-orm/pull/1177
* Generate compact entity with `#[sea_orm(column_type = "JsonBinary")]` macro attribute https://github.com/SeaQL/sea-orm/pull/1346

### Upgrades

Expand All @@ -47,11 +47,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
* Removed dependency when not needed https://github.com/SeaQL/sea-orm/pull/1213
* Changed all version = "^x.y.z" into version = "x.y.z" and disabled default features and enable only the needed ones https://github.com/SeaQL/sea-orm/pull/1300
* Cleanup the use of `vec!` macros https://github.com/SeaQL/sea-orm/pull/1367
* Fixed a small typo https://github.com/SeaQL/sea-orm/pull/1391

### Bug Fixes

* Fixes `DeriveColumn` (by qualifying `IdenStatic::as_str`) https://github.com/SeaQL/sea-orm/pull/1280
* Prevent returning connections to pool with a positive transaction depth https://github.com/SeaQL/sea-orm/pull/1283
* Postgres insert many will throw `RecordNotInserted` error if non of them are being inserted https://github.com/SeaQL/sea-orm/pull/1021
* Fixes inserting active models by `insert_many` with `on_conflict` and `do_nothing` panics if no rows are inserted on Postgres https://github.com/SeaQL/sea-orm/issues/899
* Don't call `last_insert_id` if not needed https://github.com/SeaQL/sea-orm/pull/1403
* Fixes hitting 'negative last_insert_rowid' panic with Sqlite https://github.com/SeaQL/sea-orm/issues/1357

### Breaking changes

Expand Down Expand Up @@ -83,6 +88,13 @@ impl ActiveModelBehavior for ActiveModel {
}
```

## 0.10.7 - 2023-01-19

### Bug Fixes

* Inserting active models by `insert_many` with `on_conflict` and `do_nothing` panics if no rows are inserted on Postgres https://github.com/SeaQL/sea-orm/issues/899
* Hitting 'negative last_insert_rowid' panic with Sqlite https://github.com/SeaQL/sea-orm/issues/1357

## 0.10.6 - 2022-12-23

### Enhancements
Expand Down
2 changes: 1 addition & 1 deletion examples/jsonrpsee_example/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ migration = { path = "../migration" }
anyhow = "1.0.52"
async-trait = "0.1.52"
log = { version = "0.4", features = ["std"] }
simplelog = "*"
simplelog = "0.12"
18 changes: 18 additions & 0 deletions issues/1357/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[workspace]
# A separate workspace

[package]
name = "sea-orm-issues-1357"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
anyhow = "1"
serde = "1"
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }

[dependencies.sea-orm]
path = "../../"
default-features = false
features = ["macros", "runtime-tokio-native-tls", "sqlx-sqlite"]
14 changes: 14 additions & 0 deletions issues/1357/src/entity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "pool")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i64,
pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
42 changes: 42 additions & 0 deletions issues/1357/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use anyhow::Result;
use sea_orm::{ConnectionTrait, Database, EntityTrait, IntoActiveModel, Schema};

mod entity;

use entity::*;

#[tokio::main]
async fn main() -> Result<()> {
let db = Database::connect("sqlite::memory:").await.unwrap();

let builder = db.get_database_backend();
let schema = Schema::new(builder);
let stmt = schema.create_table_from_entity(Entity);
db.execute(builder.build(&stmt)).await?;

let model = Model {
id: 100,
name: "Hello".to_owned(),
};

let res = Entity::insert(model.clone().into_active_model())
.exec(&db)
.await?;

assert_eq!(Entity::find().one(&db).await?, Some(model.clone()));
assert_eq!(res.last_insert_id, model.id);

let model = Model {
id: -10,
name: "World".to_owned(),
};

let res = Entity::insert(model.clone().into_active_model())
.exec(&db)
.await?;

assert_eq!(Entity::find().one(&db).await?, Some(model.clone()));
assert_eq!(res.last_insert_id, model.id);

Ok(())
}
1 change: 1 addition & 0 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Column {
ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)),
ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)),
ColumnType::Text => Some("Text".to_owned()),
ColumnType::JsonBinary => Some("JsonBinary".to_owned()),
ColumnType::Custom(iden) => {
Some(format!("Custom(\"{}\".to_owned())", iden.to_string()))
}
Expand Down
113 changes: 113 additions & 0 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1957,4 +1957,117 @@ mod tests {

Ok(expected.to_string())
}

#[test]
fn test_gen_postgres() -> io::Result<()> {
let entities = vec![
// This tests that the JsonBinary column type is annotated
// correctly in compact entity form. More information can be found
// in this issue:
//
// https://github.com/SeaQL/sea-orm/issues/1344
Entity {
table_name: "task".to_owned(),
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::Integer,
auto_increment: true,
not_null: true,
unique: false,
},
Column {
name: "payload".to_owned(),
col_type: ColumnType::Json,
auto_increment: false,
not_null: true,
unique: false,
},
Column {
name: "payload_binary".to_owned(),
col_type: ColumnType::JsonBinary,
auto_increment: false,
not_null: true,
unique: false,
},
],
relations: vec![],
conjunct_relations: vec![],
primary_keys: vec![PrimaryKey {
name: "id".to_owned(),
}],
},
];
const ENTITY_FILES: [&str; 1] = [include_str!("../../tests/postgres/binary_json.rs")];

const ENTITY_FILES_EXPANDED: [&str; 1] =
[include_str!("../../tests/postgres/binary_json_expanded.rs")];

assert_eq!(entities.len(), ENTITY_FILES.len());

for (i, entity) in entities.iter().enumerate() {
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&None,
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.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(),
)
.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(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("schema_name".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
}

Ok(())
}
}
21 changes: 21 additions & 0 deletions sea-orm-codegen/tests/postgres/binary_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
//!
//! This file tests that the JsonBinary column type is annotated correctly is
//! compact entity form. More information can be found in this issue:
//!
//! https://github.com/SeaQL/sea-orm/issues/1344

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "task")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub payload: Json,
#[sea_orm(column_type = "JsonBinary")]
pub payload_binary: Json,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
65 changes: 65 additions & 0 deletions sea-orm-codegen/tests/postgres/binary_json_expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
//!
//! This file tests that the JsonBinary column type is annotated correctly is
//! expanded entity form. More information can be found in this issue:
//!
//! https://github.com/SeaQL/sea-orm/issues/1344

use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;
impl EntityName for Entity {
fn schema_name(&self) -> Option< &str > {
Some("schema_name")
}
fn table_name(&self) -> &str {
"task"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
pub struct Model {
pub id: i32,
pub payload: Json,
pub payload_binary: Json,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Payload,
PayloadBinary,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}

impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
// This is the part that is being tested.
Self::Payload => ColumnType::Json.def(),
Self::PayloadBinary => ColumnType::JsonBinary.def(),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}
impl ActiveModelBehavior for ActiveModel {}
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub enum DbErr {
/// None of the records are being inserted into the database,
/// if you insert with upsert expression that means
/// all of them conflict with existing records in the database
#[error("RecordNotInserted Error: {0}")]
RecordNotInserted(String),
#[error("None of the records are being inserted")]
RecordNotInserted,
}

/// Runtime error
Expand Down
2 changes: 1 addition & 1 deletion src/executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ExecResult {
}
}

/// Get the number of rows affedted by the operation
/// Get the number of rows affected by the operation
pub fn rows_affected(&self) -> u64 {
match &self.result {
#[cfg(feature = "sqlx-mysql")]
Expand Down

0 comments on commit 2408bf9

Please sign in to comment.