Skip to content

Commit

Permalink
SQLite type mappings (#2077)
Browse files Browse the repository at this point in the history
* sqlite: deps

* sqlite: update data type mappings

* sqlite: decimal test cases

* sqlite: try negative numbers

* fixup

* fixup

* fmt

* clippy

* fixup

* fixup

* fixup

* refactor

* fix

* Drop the use of `rust_decimal_macros` (#2078)

* sqlite: decimal -> real

* revert

* Bump dependencies

* Fixup

* Fixup

* Fixup

* Fixup

* Refactor

* Refactor

* Refactor
  • Loading branch information
billy1624 authored Feb 5, 2024
1 parent 7f25da3 commit cef380b
Show file tree
Hide file tree
Showing 43 changed files with 119 additions and 148 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ tracing = { version = "0.1", default-features = false, features = ["attributes",
rust_decimal = { version = "1", default-features = false, optional = true }
bigdecimal = { version = "0.3", default-features = false, optional = true }
sea-orm-macros = { version = "0.12.12", path = "sea-orm-macros", default-features = false, features = ["strum"] }
sea-query = { version = "0.30.4", default-features = false, features = ["thread-safe", "hashable-value", "backend-mysql", "backend-postgres", "backend-sqlite"] }
sea-query-binder = { version = "0.5.0", default-features = false, optional = true }
sea-query = { version = "0.31.0-rc.3", default-features = false, features = ["thread-safe", "hashable-value", "backend-mysql", "backend-postgres", "backend-sqlite"] }
sea-query-binder = { version = "0.6.0-rc.1", default-features = false, optional = true }
strum = { version = "0.25", default-features = false }
serde = { version = "1.0", default-features = false }
serde_json = { version = "1.0", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/example_filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Name => ColumnType::string(None).def(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/example_fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Name => ColumnType::string(None).def(),
Self::CakeId => ColumnType::Integer.def(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/seaography_example/graphql/src/entities/cake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
#[sea_orm(column_type = "Decimal(Some((19, 4)))")]
#[sea_orm(column_type = "Decimal(Some((16, 4)))")]
pub price: Decimal,
pub bakery_id: Option<i32>,
pub gluten_free: i8,
Expand Down
2 changes: 1 addition & 1 deletion examples/seaography_example/migration/src/entity/cake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
#[sea_orm(column_type = "Decimal(Some((19, 4)))")]
#[sea_orm(column_type = "Decimal(Some((16, 4)))")]
pub price: Decimal,
pub bakery_id: Option<i32>,
pub gluten_free: i8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl MigrationTrait for Migration {
.primary_key(),
)
.col(ColumnDef::new(Cake::Name).string().not_null())
.col(ColumnDef::new(Cake::Price).decimal_len(19, 4).not_null())
.col(ColumnDef::new(Cake::Price).decimal_len(16, 4).not_null())
.col(ColumnDef::new(Cake::BakeryId).integer())
.foreign_key(
ForeignKey::create()
Expand Down
2 changes: 1 addition & 1 deletion issues/1143/src/entity/sea_orm_active_enums.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sea_orm::entity::prelude::*;

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "String(Some(1))")]
#[sea_orm(rs_type = "String", db_type = "string(Some(1))")]
pub enum Category {
#[sea_orm(string_value = "B")]
Big,
Expand Down
2 changes: 1 addition & 1 deletion issues/1599/entity/src/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Name => ColumnType::string(None).def(),
Self::VendorId => ColumnType::Integer.def().nullable(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true }
dotenvy = { version = "0.15", default-features = false, optional = true }
async-std = { version = "1.9", default-features = false, features = ["attributes", "tokio1"], optional = true }
sea-orm-codegen = { version = "=0.12.12", path = "../sea-orm-codegen", default-features = false, optional = true }
sea-schema = { version = "0.14.2" }
sea-schema = { version = "0.15.0-rc.1" }
sqlx = { version = "0.7", default-features = false, features = ["mysql", "postgres"], optional = true }
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] }
tracing = { version = "0.1", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ name = "sea_orm_codegen"
path = "src/lib.rs"

[dependencies]
sea-query = { version = "0.30.0", default-features = false, features = ["thread-safe"] }
sea-query = { version = "0.31.0-rc.3", default-features = false, features = ["thread-safe"] }
syn = { version = "2", default-features = false, features = ["parsing", "proc-macro", "derive", "printing"] }
quote = { version = "1", default-features = false }
heck = { version = "0.4", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ mod tests {
},
Column {
name: "name".to_owned(),
col_type: ColumnType::String(None),
col_type: ColumnType::string(None),
auto_increment: false,
not_null: false,
unique: false,
Expand Down
90 changes: 35 additions & 55 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{util::escape_rust_keyword, DateTimeCrate};
use heck::{ToSnakeCase, ToUpperCamelCase};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use sea_query::{BlobSize, ColumnDef, ColumnSpec, ColumnType};
use sea_query::{ColumnDef, ColumnSpec, ColumnType, StringLen};
use std::fmt::Write as FmtWrite;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -96,14 +96,12 @@ impl Column {
ColumnType::Text => Some("Text".to_owned()),
ColumnType::JsonBinary => Some("JsonBinary".to_owned()),
ColumnType::Custom(iden) => Some(format!("custom(\"{}\")", iden.to_string())),
ColumnType::Binary(BlobSize::Blob(None)) => Some("Binary(BlobSize::Blob(None))".into()),
ColumnType::Binary(BlobSize::Blob(Some(s))) => {
Some(format!("Binary(BlobSize::Blob(Some({s})))"))
}
ColumnType::Binary(BlobSize::Tiny) => Some("Binary(BlobSize::Tiny)".into()),
ColumnType::Binary(BlobSize::Medium) => Some("Binary(BlobSize::Medium)".into()),
ColumnType::Binary(BlobSize::Long) => Some("Binary(BlobSize::Long)".into()),
ColumnType::VarBinary(s) => Some(format!("VarBinary({s})")),
ColumnType::Binary(s) => Some(format!("Binary({s})")),
ColumnType::VarBinary(s) => match s {
StringLen::N(s) => Some(format!("VarBinary(StringLen::N({s}))")),
StringLen::None => Some("VarBinary(StringLen::None)".to_owned()),
StringLen::Max => Some("VarBinary(StringLen::Max)".to_owned()),
},
_ => None,
};
col_type.map(|ty| quote! { column_type = #ty })
Expand All @@ -117,8 +115,9 @@ impl Column {
None => quote! { ColumnType::Char(None) },
},
ColumnType::String(s) => match s {
Some(s) => quote! { ColumnType::String(Some(#s)) },
None => quote! { ColumnType::String(None) },
StringLen::N(s) => quote! { ColumnType::string(Some(#s)) },
StringLen::None => quote! { ColumnType::string(None) },
StringLen::Max => quote! { ColumnType::String(StringLen::Max) },
},
ColumnType::Text => quote! { ColumnType::Text },
ColumnType::TinyInteger => quote! { ColumnType::TinyInteger },
Expand All @@ -142,24 +141,14 @@ impl Column {
}
ColumnType::Time => quote! { ColumnType::Time },
ColumnType::Date => quote! { ColumnType::Date },
ColumnType::Binary(BlobSize::Blob(None)) => {
quote! { ColumnType::Binary(BlobSize::Blob(None)) }
}
ColumnType::Binary(BlobSize::Blob(Some(s))) => {
quote! { ColumnType::Binary(BlobSize::Blob(Some(#s))) }
}
ColumnType::Binary(BlobSize::Tiny) => {
quote! { ColumnType::Binary(BlobSize::Tiny) }
}
ColumnType::Binary(BlobSize::Medium) => {
quote! { ColumnType::Binary(BlobSize::Medium) }
}
ColumnType::Binary(BlobSize::Long) => {
quote! { ColumnType::Binary(BlobSize::Long) }
}
ColumnType::VarBinary(s) => {
quote! { ColumnType::VarBinary(#s) }
ColumnType::Binary(s) => {
quote! { ColumnType::Binary(#s) }
}
ColumnType::VarBinary(s) => match s {
StringLen::N(s) => quote! { ColumnType::VarBinary(StringLen::N(#s)) },
StringLen::None => quote! { ColumnType::VarBinary(StringLen::None) },
StringLen::Max => quote! { ColumnType::VarBinary(StringLen::Max) },
},
ColumnType::Boolean => quote! { ColumnType::Boolean },
ColumnType::Money(s) => match s {
Some((s1, s2)) => quote! { ColumnType::Money(Some((#s1, #s2))) },
Expand Down Expand Up @@ -298,7 +287,7 @@ mod tests {
use crate::{Column, DateTimeCrate};
use proc_macro2::TokenStream;
use quote::quote;
use sea_query::{Alias, BlobSize, ColumnDef, ColumnType, SeaRc};
use sea_query::{Alias, ColumnDef, ColumnType, SeaRc, StringLen};

fn setup() -> Vec<Column> {
macro_rules! make_col {
Expand All @@ -313,7 +302,8 @@ mod tests {
};
}
vec![
make_col!("id", ColumnType::String(Some(255))),
make_col!("id", ColumnType::string(Some(255))),
make_col!("id", ColumnType::string(None)),
make_col!(
"cake_id",
ColumnType::Custom(SeaRc::new(Alias::new("cus_col")))
Expand All @@ -328,15 +318,10 @@ mod tests {
make_col!("CakeFillingId", ColumnType::BigUnsigned),
make_col!("cake-filling-id", ColumnType::Float),
make_col!("CAKE_FILLING_ID", ColumnType::Double),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Blob(None))),
make_col!(
"CAKE-FILLING-ID",
ColumnType::Binary(BlobSize::Blob(Some(10)))
),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Tiny)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Medium)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(BlobSize::Long)),
make_col!("CAKE-FILLING-ID", ColumnType::VarBinary(10)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(10)),
make_col!("CAKE-FILLING-ID", ColumnType::VarBinary(StringLen::None)),
make_col!("CAKE-FILLING-ID", ColumnType::VarBinary(StringLen::N(10))),
make_col!("CAKE-FILLING-ID", ColumnType::VarBinary(StringLen::Max)),
make_col!("CAKE", ColumnType::Boolean),
make_col!("date", ColumnType::Date),
make_col!("time", ColumnType::Time),
Expand All @@ -350,6 +335,7 @@ mod tests {
fn test_get_name_snake_case() {
let columns = setup();
let snack_cases = vec![
"id",
"id",
"cake_id",
"cake_id",
Expand All @@ -366,8 +352,6 @@ mod tests {
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake",
"date",
"time",
Expand All @@ -384,6 +368,7 @@ mod tests {
fn test_get_name_camel_case() {
let columns = setup();
let camel_cases = vec![
"Id",
"Id",
"CakeId",
"CakeId",
Expand All @@ -400,8 +385,6 @@ mod tests {
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"Cake",
"Date",
"Time",
Expand All @@ -419,6 +402,7 @@ mod tests {
let columns = setup();
let chrono_crate = DateTimeCrate::Chrono;
let rs_types = vec![
"String",
"String",
"String",
"i8",
Expand All @@ -435,8 +419,6 @@ mod tests {
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"bool",
"Date",
"Time",
Expand Down Expand Up @@ -466,6 +448,7 @@ mod tests {
let columns = setup();
let time_crate = DateTimeCrate::Time;
let rs_types = vec![
"String",
"String",
"String",
"i8",
Expand All @@ -482,8 +465,6 @@ mod tests {
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"Vec<u8>",
"bool",
"TimeDate",
"TimeTime",
Expand Down Expand Up @@ -512,7 +493,8 @@ mod tests {
fn test_get_def() {
let columns = setup();
let col_defs = vec![
"ColumnType::String(Some(255u32)).def()",
"ColumnType::string(Some(255u32)).def()",
"ColumnType::string(None).def()",
"ColumnType::custom(\"cus_col\").def()",
"ColumnType::TinyInteger.def()",
"ColumnType::TinyUnsigned.def()",
Expand All @@ -524,12 +506,10 @@ mod tests {
"ColumnType::BigUnsigned.def()",
"ColumnType::Float.def()",
"ColumnType::Double.def()",
"ColumnType::Binary(BlobSize::Blob(None)).def()",
"ColumnType::Binary(BlobSize::Blob(Some(10u32))).def()",
"ColumnType::Binary(BlobSize::Tiny).def()",
"ColumnType::Binary(BlobSize::Medium).def()",
"ColumnType::Binary(BlobSize::Long).def()",
"ColumnType::VarBinary(10u32).def()",
"ColumnType::Binary(10u32).def()",
"ColumnType::VarBinary(StringLen::None).def()",
"ColumnType::VarBinary(StringLen::N(10u32)).def()",
"ColumnType::VarBinary(StringLen::Max).def()",
"ColumnType::Boolean.def()",
"ColumnType::Date.def()",
"ColumnType::Time.def()",
Expand Down Expand Up @@ -701,7 +681,7 @@ mod tests {
assert_eq!(
column.get_def().to_string(),
quote! {
ColumnType::String(None).def().null()
ColumnType::string(None).def().null()
}
.to_string()
);
Expand Down
6 changes: 3 additions & 3 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ mod tests {
},
Column {
name: "name".to_owned(),
col_type: ColumnType::String(Some(255)),
col_type: ColumnType::string(Some(255)),
auto_increment: false,
not_null: true,
unique: false,
Expand All @@ -1020,7 +1020,7 @@ mod tests {
},
Column {
name: "name".to_owned(),
col_type: ColumnType::String(Some(255)),
col_type: ColumnType::string(Some(255)),
auto_increment: false,
not_null: true,
unique: false,
Expand Down Expand Up @@ -1074,7 +1074,7 @@ mod tests {
},
Column {
name: "_name_".to_owned(),
col_type: ColumnType::String(Some(255)),
col_type: ColumnType::string(Some(255)),
auto_increment: false,
not_null: true,
unique: false,
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(),
Self::Name => ColumnType::string(Some(255u32)).def(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(),
Self::Name => ColumnType::string(Some(255u32)).def(),
Self::CakeId => ColumnType::Integer.def().null(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(),
Self::Name => ColumnType::string(Some(255u32)).def(),
Self::FruitId => ColumnType::Integer.def().null(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_schema_name/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(),
Self::Name => ColumnType::string(Some(255u32)).def(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_schema_name/fruit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(),
Self::Name => ColumnType::string(Some(255u32)).def(),
Self::CakeId => ColumnType::Integer.def().null(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(),
Self::Name => ColumnType::string(Some(255u32)).def(),
Self::FruitId => ColumnType::Integer.def().null(),
}
}
Expand Down
Loading

0 comments on commit cef380b

Please sign in to comment.