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

issue: 2376 order_by_desc not working with cursor_by #2377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions issues/2376/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[workspace]
# A separate workspace

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

[dependencies]
sea-orm = { version = "^0.12.0", features = [
"sqlx-sqlite",
"runtime-async-std-native-tls",
"macros",
] }
serde = { version = "1.0.210", features = ["derive"] }
futures = "0.3.30"
tokio = { version = "1.40.0", features = ["full"] }
sea-orm-macros = "1.0.1"
chrono = "0.4.38"
1 change: 1 addition & 0 deletions issues/2376/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod thread;
59 changes: 59 additions & 0 deletions issues/2376/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
mod thread;
mod time_builder;

use sea_orm::{
sea_query::TableCreateStatement, ActiveValue, ConnectionTrait, Database, DatabaseConnection,
EntityTrait, QueryOrder, QuerySelect, Schema,
};
use time_builder::TimeBuilder;

#[tokio::main]
async fn main() {
let conn: DatabaseConnection = Database::connect("sqlite::memory:").await.unwrap();
create_thread_table(&conn).await;

let thread_1 = create_thread(&conn).await;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let thread_2 = create_thread(&conn).await;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let thread_3 = create_thread(&conn).await;

// This is not working
let result = thread::Entity::find()
.cursor_by(thread::Column::Id)
.order_by_desc(thread::Column::Id)
.limit(10)
.all(&conn)
.await
.unwrap();

// // This is working
// let result = thread::Entity::find()
// .order_by_desc(thread::Column::Id)
// .all(&conn)
// .await
// .unwrap();

assert_eq!(result[0].id, thread_3.id);
assert_eq!(result[1].id, thread_2.id);
assert_eq!(result[2].id, thread_1.id);
}

async fn create_thread(conn: &DatabaseConnection) -> thread::Model {
thread::Entity::insert(thread::ActiveModel {
created_at: ActiveValue::Set(TimeBuilder::now().into()),
..Default::default()
})
.exec_with_returning(conn)
.await
.unwrap()
}

async fn create_thread_table(conn: &DatabaseConnection) {
let schema = Schema::new(sea_orm::DbBackend::Sqlite);
let stmt: TableCreateStatement = schema.create_table_from_entity(thread::Entity);

conn.execute(sea_orm::DbBackend::Sqlite.build(&stmt))
.await
.unwrap();
}
19 changes: 19 additions & 0 deletions issues/2376/src/thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1

use sea_orm::entity::prelude::*;
use sea_orm::DeriveEntityModel;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "thread")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_type = "Text")]
pub created_at: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
59 changes: 59 additions & 0 deletions issues/2376/src/time_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

extern crate chrono;
use chrono::prelude::*;

pub struct TimeBuilder {
time: DateTime<FixedOffset>,
}

impl TimeBuilder {
pub fn now() -> Self {
Self {
time: Utc::now().fixed_offset(),
}
}

pub fn from_string(str: &str) -> Self {
Self {
time: DateTime::parse_from_rfc3339(str).unwrap(),
}
}

pub fn from_i64(timestamp: i64) -> Self {
let time = DateTime::from_timestamp(timestamp, 0)
.unwrap()
.fixed_offset();

Self { time }
}

pub fn to_string(&self) -> String {
self.time.to_rfc3339()
}

pub fn to_i64(&self) -> i64 {
self.time.timestamp()
}

pub fn is_near_now(&self) -> bool {
self.is_near_offset_sec(1)
}

pub fn is_near_offset_sec(&self, offset_sec: i64) -> bool {
let now = Utc::now();
let diff = now.signed_duration_since(self.time).num_seconds().abs();
diff <= offset_sec
}
}

impl From<TimeBuilder> for String {
fn from(time: TimeBuilder) -> Self {
time.to_string()
}
}

impl From<TimeBuilder> for i64 {
fn from(time: TimeBuilder) -> Self {
time.to_i64()
}
}