Skip to content

Commit e269805

Browse files
committed
Fix sea-orm-sync
1 parent 212cce9 commit e269805

File tree

7 files changed

+71
-30
lines changed

7 files changed

+71
-30
lines changed

sea-orm-sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ rust_decimal = { version = "1", default-features = false, features = [
5050
sea-orm-macros = { version = "~2.0.0-rc.20", path = "../sea-orm-macros", default-features = false, features = [
5151
"strum",
5252
] }
53-
sea-query = { version = "=1.0.0-rc.23", default-features = false, features = [
53+
sea-query = { version = "=1.0.0-rc.26", default-features = false, features = [
5454
"thread-safe",
5555
"hashable-value",
5656
"backend-mysql",

sea-orm-sync/src/entity/column.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{
22
ColumnDef, ColumnType, DbBackend, EntityName, Iden, IdenStatic, IntoSimpleExpr, Iterable,
33
};
4+
#[cfg(feature = "postgres-array")]
5+
use sea_query::ArrayElement;
46
use sea_query::{
57
BinOper, DynIden, Expr, ExprTrait, IntoIden, IntoLikeExpr, SeaRc, SelectStatement, Value,
68
};
@@ -392,13 +394,13 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr {
392394
/// );
393395
/// ```
394396
#[cfg(feature = "postgres-array")]
395-
fn eq_any<A>(&self, arr: A) -> Expr
397+
fn eq_any<T>(&self, arr: impl IntoIterator<Item = T>) -> Expr
396398
where
397-
A: Into<sea_query::value::Array>,
399+
T: ArrayElement,
398400
{
399401
use sea_query::extension::postgres::PgFunc;
400402

401-
let value = Value::Array(arr.into());
403+
let value = Value::Array(arr.into_iter().collect());
402404
Expr::col(self.as_column_ref()).eq(PgFunc::any(Expr::val(value)))
403405
}
404406

sea-orm-sync/src/entity/column/types.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,9 @@ macro_rules! impl_numeric_column {
255255
#[cfg(feature = "postgres-array")]
256256
pub fn eq_any<V>(&self, v: impl IntoIterator<Item = V>) -> Expr
257257
where
258-
V: $trait + sea_query::ValueType + sea_query::with_array::NotU8,
259-
Vec<V>: Into<sea_query::value::Array>,
258+
V: $trait + sea_query::ArrayElement,
260259
{
261-
let vec: Vec<V> = v.into_iter().collect();
262-
self.0.eq_any(vec)
260+
self.0.eq_any(v)
263261
}
264262

265263
bind_subquery_func!(pub in_subquery);
@@ -305,11 +303,9 @@ macro_rules! impl_string_column {
305303
#[cfg(feature = "postgres-array")]
306304
pub fn eq_any<V>(&self, v: impl IntoIterator<Item = V>) -> Expr
307305
where
308-
V: Into<String> + sea_query::ValueType + sea_query::with_array::NotU8,
309-
Vec<V>: Into<sea_query::value::Array>,
306+
V: Into<String> + sea_query::ArrayElement
310307
{
311-
let vec: Vec<V> = v.into_iter().collect();
312-
self.0.eq_any(vec)
308+
self.0.eq_any(v)
313309
}
314310

315311
bind_subquery_func!(pub in_subquery);

sea-orm-sync/src/entity/column/types/with_datetime.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use super::*;
2+
#[cfg(feature = "postgres-array")]
3+
use sea_query::ArrayElement;
24
use sea_query::{DateLikeValue, DateTimeLikeValue, TimeLikeValue};
35

46
impl<E: EntityTrait> DateLikeColumn<E> {
@@ -28,14 +30,11 @@ impl<E: EntityTrait> DateLikeColumn<E> {
2830

2931
/// `= ANY(..)` operator. Postgres only.
3032
#[cfg(feature = "postgres-array")]
31-
pub fn eq_any<V>(&self, v: impl IntoIterator<Item = V>) -> Expr
33+
pub fn eq_any<T>(&self, v: impl IntoIterator<Item = T>) -> Expr
3234
where
33-
V: DateLikeValue + sea_query::ValueType + sea_query::with_array::NotU8,
34-
Vec<V>: Into<sea_query::value::Array>,
35+
T: DateLikeValue + ArrayElement,
3536
{
36-
use itertools::Itertools;
37-
38-
self.0.eq_any(v.into_iter().collect_vec())
37+
self.0.eq_any(v)
3938
}
4039

4140
bind_subquery_func!(pub in_subquery);
@@ -69,13 +68,11 @@ impl<E: EntityTrait> TimeLikeColumn<E> {
6968

7069
/// `= ANY(..)` operator. Postgres only.
7170
#[cfg(feature = "postgres-array")]
72-
pub fn eq_any<V>(&self, v: impl IntoIterator<Item = V>) -> Expr
71+
pub fn eq_any<T>(&self, v: impl IntoIterator<Item = T>) -> Expr
7372
where
74-
V: TimeLikeValue + sea_query::ValueType + sea_query::with_array::NotU8,
75-
Vec<V>: Into<sea_query::value::Array>,
73+
T: TimeLikeValue + ArrayElement,
7674
{
77-
let vec: Vec<V> = v.into_iter().collect();
78-
self.0.eq_any(vec)
75+
self.0.eq_any(v)
7976
}
8077

8178
bind_subquery_func!(pub in_subquery);
@@ -111,11 +108,9 @@ impl<E: EntityTrait> DateTimeLikeColumn<E> {
111108
#[cfg(feature = "postgres-array")]
112109
pub fn eq_any<V>(&self, v: impl IntoIterator<Item = V>) -> Expr
113110
where
114-
V: DateTimeLikeValue + sea_query::ValueType + sea_query::with_array::NotU8,
115-
Vec<V>: Into<sea_query::value::Array>,
111+
V: DateTimeLikeValue + sea_query::ArrayElement,
116112
{
117-
let vec: Vec<V> = v.into_iter().collect();
118-
self.0.eq_any(vec)
113+
self.0.eq_any(v)
119114
}
120115

121116
bind_subquery_func!(pub in_subquery);

sea-orm-sync/src/value/json.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::sea_query::{ArrayType, ColumnType, Nullable, ValueType, ValueTypeErr};
2-
use crate::{ColIdx, QueryResult, TryGetError, TryGetable, Value, error::json_err};
2+
use crate::{
3+
ActiveValue, ColIdx, IntoActiveValue, QueryResult, TryGetError, TryGetable, Value,
4+
error::json_err,
5+
};
36

47
use serde::{Deserialize, Deserializer, Serialize, Serializer};
58

@@ -83,6 +86,15 @@ impl<T> Nullable for JsonField<T> {
8386
}
8487
}
8588

89+
impl<T> IntoActiveValue<JsonField<T>> for JsonField<T>
90+
where
91+
T: Serialize,
92+
{
93+
fn into_active_value(self) -> ActiveValue<JsonField<T>> {
94+
ActiveValue::Set(self)
95+
}
96+
}
97+
8698
impl<T> TryGetable for JsonField<T>
8799
where
88100
for<'de> T: Deserialize<'de>,

sea-orm-sync/src/value/timestamp.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ macro_rules! impl_timestamp {
5151
}
5252
}
5353

54+
impl sea_orm::IntoActiveValue<$ty> for $ty {
55+
fn into_active_value(self) -> sea_orm::ActiveValue<$ty> {
56+
sea_orm::ActiveValue::Set(self)
57+
}
58+
}
59+
5460
impl Deref for $ty {
5561
type Target = $inner;
5662

sea-orm-sync/tests/string_primary_key_tests.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod common;
44

55
pub use common::{TestContext, features::*, setup::*};
66
use pretty_assertions::assert_eq;
7-
use sea_orm::{DatabaseConnection, entity::prelude::*, entity::*};
7+
use sea_orm::{DatabaseConnection, TryInsertResult, entity::prelude::*, entity::*};
88
use serde_json::json;
99

1010
#[sea_orm_macros::test]
@@ -43,7 +43,7 @@ pub fn insert_and_delete_repository(db: &DatabaseConnection) -> Result<(), DbErr
4343
{
4444
use sea_orm::sea_query::OnConflict;
4545

46-
let err = Repository::insert(repository)
46+
let err = Repository::insert(repository.clone())
4747
// MySQL does not support DO NOTHING, we might workaround that later
4848
.on_conflict(OnConflict::new().do_nothing().to_owned())
4949
.exec(db);
@@ -99,6 +99,36 @@ pub fn insert_and_delete_repository(db: &DatabaseConnection) -> Result<(), DbErr
9999
]
100100
);
101101

102+
#[cfg(any(feature = "sqlx-sqlite", feature = "sqlx-postgres"))]
103+
{
104+
let result = Repository::insert_many([
105+
repository::Model {
106+
id: "unique-id-002".to_owned(), // conflict
107+
owner: "GC".to_owned(),
108+
name: "G.C.".to_owned(),
109+
description: None,
110+
}
111+
.into_active_model(),
112+
repository::Model {
113+
id: "unique-id-003".to_owned(), // insert succeed
114+
owner: "GC".to_owned(),
115+
name: "G.C.".to_owned(),
116+
description: None,
117+
}
118+
.into_active_model(),
119+
])
120+
.on_conflict_do_nothing()
121+
.exec_with_returning_many(db)?;
122+
123+
match result {
124+
TryInsertResult::Inserted(inserted) => {
125+
assert_eq!(inserted.len(), 1);
126+
assert_eq!(inserted[0].id, "unique-id-003");
127+
}
128+
_ => panic!("{result:?}"),
129+
}
130+
}
131+
102132
Ok(())
103133
}
104134

0 commit comments

Comments
 (0)