Skip to content

Commit

Permalink
trying a few things before arriving at a fix which is a patch to sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronbraid committed Aug 9, 2023
1 parent 099cc01 commit 25fe95b
Show file tree
Hide file tree
Showing 18 changed files with 860 additions and 60 deletions.
603 changes: 557 additions & 46 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ edition = "2021"
authors = ["Cameron <[email protected]>"]

[workspace.dependencies]

[patch.crates-io]
# sqlx = { path = "../sqlx"}
sqlx = { git = "https://github.com/cameronbraid/sqlx", branch = "decimal_compatibility_lenient" }

8 changes: 6 additions & 2 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sea-orm = { version = "0", features = [ "sqlx-mysql", "runtime-tokio-rustls", "macros", "with-chrono", "with-rust_decimal", "debug-print" ] }
sea-orm = { version = "*", features = [ "sqlx-mysql", "runtime-tokio", "macros", "with-chrono", "with-rust_decimal", "debug-print", "with-bigdecimal" ] }
tokio = { version = "1.0", features = ["rt-multi-thread", "macros", "sync", "time","full"] }
migration = { path = "../migration" }
entity = { path = "../entity" }
Expand All @@ -15,4 +15,8 @@ rust_decimal = { version = "1.28.0", features = ["serde-float"] }
rust_decimal_macros = "1.28.0"
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = ["json", "env-filter", "valuable", "fmt", "std"] }
log = "*"
log = "*"
sqlx = { version = "*", features = [ "runtime-tokio", "rust_decimal", "bigdecimal" ] }

mysql = { version = "*", default-features = false, features = ["minimal", "derive"] }
mysql_common = { version = "*", default-features = false, features = ["rust_decimal"]}
29 changes: 29 additions & 0 deletions app/src/bin/has_decimal_mysql_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use app::docker;
use color_eyre::Result;
use entity::has_decimal;
use mysql::Pool;
use mysql::prelude::FromRow;
use rust_decimal::Decimal;

use mysql::prelude::Queryable;
#[tokio::main]
async fn main() -> Result<()> {

#[derive(FromRow, Debug)]
pub struct Row {
pub id : i64,
pub price: Decimal,
}

let url = "mysql://user:password@localhost:3307/db?enable_cleartext_plugin=true";
let pool = Pool::new(url)?;

let mut conn = pool.get_conn()?;
let row: Option<Row> = conn
.exec_first(
"SELECT * from has_decimal",())?;

println!("row: {:?}", row);

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ async fn main() -> Result<()> {
.await?;

tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let item = has_decimal::Entity::find_by_id(1).one(&db.mysql).await?;
println!("item: {:?}", item);
} else {
println!("item: {:?}", item);
}

let _item = has_decimal::Entity::find_by_id(1)
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;

Ok(())
}
22 changes: 22 additions & 0 deletions app/src/bin/has_decimal_seaorm_mysql_insert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use app::docker;
use color_eyre::Result;
use entity::has_decimal;
use rust_decimal_macros::dec;
use sea_orm::ActiveModelTrait;
use sea_orm::EntityTrait;
use sea_orm::Set;

#[tokio::main]
async fn main() -> Result<()> {
let db = docker::db_connect().await;

let item = has_decimal::ActiveModel {
id: Set(1),
price: Set(dec!(10.0)),
}
.insert(&db.mysql)
.await?;

println!("item: {:?}", item);
Ok(())
}
17 changes: 17 additions & 0 deletions app/src/bin/has_decimal_seaorm_readyset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use app::docker;
use color_eyre::Result;
use entity::has_decimal;
use sea_orm::EntityTrait;


#[tokio::main]
async fn main() -> Result<()> {
let db = docker::db_connect().await;

let item = has_decimal::Entity::find_by_id(1)
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;
println!("item: {:?}", item);
Ok(())
}
23 changes: 23 additions & 0 deletions app/src/bin/has_decimal_sqlx_mysql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use color_eyre::Result;
use rust_decimal::Decimal;
use sqlx::{MySqlPool, types::BigDecimal};

#[tokio::main]
async fn main() -> Result<()> {

let pool = MySqlPool::connect("mysql://user:password@localhost:3307/db?ssl-mode=disabled").await?;

#[derive(sqlx::FromRow, Debug)]
pub struct Row {
pub id : i64,
pub price: Decimal,
}

// Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL)
let row : Row = sqlx::query_as("SELECT id, price from has_decimal")
.fetch_one(&pool).await?;

println!("row: {:?}", row);

Ok(())
}
24 changes: 24 additions & 0 deletions app/src/bin/has_decimal_sqlx_readyset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use color_eyre::Result;
use rust_decimal::Decimal;
use sqlx::MySqlPool;

#[tokio::main]
async fn main() -> Result<()> {
let pool =
MySqlPool::connect("mysql://user:password@localhost:3307/db?ssl-mode=disabled").await?;

#[derive(sqlx::FromRow, Debug)]
pub struct Row {
pub id: i64,
pub price: Decimal,
}

// Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL)
let row: Row = sqlx::query_as("SELECT id, price from has_decimal")
.fetch_one(&pool)
.await?;

println!("row: {:?}", row);

Ok(())
}
76 changes: 76 additions & 0 deletions app/src/bin/product.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use app::docker;
use color_eyre::Result;
use entity::product;
use sea_orm::ActiveModelTrait;
use sea_orm::ColumnTrait;
use sea_orm::DbBackend;
use sea_orm::EntityTrait;
use sea_orm::FromQueryResult;
use sea_orm::QueryFilter;
use sea_orm::Set;
use sea_orm::Statement;

#[tokio::main]
async fn main() -> Result<()> {
let db = docker::db_connect().await;

let item = product::Entity::find_by_id(1).one(&db.mysql).await?;

if item.is_none() {
product::ActiveModel {
id: Set(1),
name: Set("test".to_string()),
}
.insert(&db.mysql)
.await?;

tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}

let _item = product::Entity::find_by_id(1)
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;

let _item = product::Entity::find()
.filter(product::Column::Id.is_in(vec![1, 2, 3]))
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;

let _item = product::Entity::find()
.from_raw_sql(Statement::from_sql_and_values(
DbBackend::MySql,
r#"SELECT * FROM product WHERE id = ?"#,
[1.into()],
))
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;

let _item = product::Entity::find()
.from_raw_sql(Statement::from_sql_and_values(
DbBackend::MySql,
r#"SELECT * FROM product WHERE id in (1)"#,
[],
))
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;

#[derive(Debug, FromQueryResult)]
pub struct ProductName {
pub name: String,
}

let _foo = ProductName::find_by_statement(Statement::from_sql_and_values(
DbBackend::MySql,
r#"SELECT product.name as name from product left join has_decimal on product.id = has_decimal.id where product.id = 1"#,
[],
))
.one(&db.readyset)
.await?
.ok_or_else(|| color_eyre::eyre::eyre!("not found in readyset"))?;

Ok(())
}
2 changes: 1 addition & 1 deletion app/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn db_connect() -> Connections {

Connections {
mysql: Database::connect({
let mut opt = ConnectOptions::new("mysql://user:password@localhost:3306/db".to_owned());
let mut opt = ConnectOptions::new("mysql://user:password@localhost:3306/db?ssl-mode=disabled".to_owned());
opt.sqlx_logging_level(log::LevelFilter::Debug);
opt
})
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
# command:
# - --ssl=off
ports:
- '3306:3306'
volumes:
Expand All @@ -31,4 +33,5 @@ services:
--db-dir=/data
--username=user
--password=password
--log-level=DEBUG
--log-level=TRACE
--statement-logging
2 changes: 1 addition & 1 deletion entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sea-orm = { version = "0", features = [ "sqlx-mysql", "runtime-tokio-rustls", "macros", "with-chrono", "with-rust_decimal", "debug-print" ] }
sea-orm = { version = "0", features = [ "sqlx-mysql", "runtime-tokio", "macros", "with-chrono", "with-rust_decimal", "debug-print" ] }

[lib]
name = "entity"
Expand Down
3 changes: 3 additions & 0 deletions entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod has_decimal;
pub mod product;

pub mod prelude;
pub mod order;
13 changes: 13 additions & 0 deletions entity/src/order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "order")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
}

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

impl ActiveModelBehavior for ActiveModel {}
10 changes: 10 additions & 0 deletions entity/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
pub use super::has_decimal::Entity as HasDecimal;
pub use super::has_decimal::Model as HasDecimalModel;

pub use super::product::Entity as Product;
pub use super::product::Model as ProductModel;

pub use super::order::Entity as Order;
pub use super::order::Model as OrderModel;

pub use sea_orm::QueryOrder;
pub use sea_orm::QuerySelect;
pub use sea_orm::QueryTrait;
14 changes: 14 additions & 0 deletions entity/src/product.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 = "product")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
55 changes: 51 additions & 4 deletions migration/src/m20220101_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,47 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(HasDecimal::Price).decimal().null())
.to_owned(),
)
.await
.await?;

manager
.create_table(
Table::create()
.table(Product::Table)
.if_not_exists()
.col(
ColumnDef::new(Product::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Product::Name).string().null())
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Order::Table)
.if_not_exists()
.col(
ColumnDef::new(Order::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
// .col(ColumnDef::new(Product::Name).string().null())
.to_owned(),
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(HasDecimal::Table).to_owned())
.await

Ok(())
}
}

Expand All @@ -37,3 +71,16 @@ enum HasDecimal {
Id,
Price,
}

#[derive(DeriveIden)]
enum Product {
Table,
Id,
Name,
}

#[derive(DeriveIden)]
enum Order {
Table,
Id,
}

0 comments on commit 25fe95b

Please sign in to comment.