Skip to content

Commit 2ed96e2

Browse files
committed
Add support for extra non-standard "DATETIME UTC" type for sqlite
This allows us to explicitly opt into using `DateTime<Utc>` instead of `NaiveDateTime` without doing conversions at query time (either via `col as "col: DateTime<Utc>" or mapping the results manually). Ref: launchbadge#598 (comment)
1 parent f05c884 commit 2ed96e2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

sqlx-sqlite/src/type_info.rs

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub(crate) enum DataType {
2727
Date,
2828
Time,
2929
Datetime,
30+
DatetimeUtc,
3031
}
3132

3233
/// Type information for a SQLite type.
@@ -59,6 +60,7 @@ impl TypeInfo for SqliteTypeInfo {
5960
DataType::Date => "DATE",
6061
DataType::Time => "TIME",
6162
DataType::Datetime => "DATETIME",
63+
DataType::DatetimeUtc => "DATETIME UTC",
6264
}
6365
}
6466
}
@@ -94,6 +96,7 @@ impl FromStr for DataType {
9496
"date" => DataType::Date,
9597
"time" => DataType::Time,
9698
"datetime" | "timestamp" => DataType::Datetime,
99+
"datetime utc" | "timestamp utc" => DataType::DatetimeUtc,
97100

98101
_ if s.contains("int") => DataType::Int64,
99102

@@ -149,5 +152,7 @@ fn test_data_type_from_str() -> Result<(), BoxDynError> {
149152
assert_eq!(DataType::Time, "TIME".parse()?);
150153
assert_eq!(DataType::Date, "DATE".parse()?);
151154

155+
assert_eq!(DataType::DatetimeUtc, "DATETIME UTC".parse()?);
156+
152157
Ok(())
153158
}

sqlx-sqlite/src/types/chrono.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use chrono::{
1515

1616
impl<Tz: TimeZone> Type<Sqlite> for DateTime<Tz> {
1717
fn type_info() -> SqliteTypeInfo {
18-
SqliteTypeInfo(DataType::Datetime)
18+
SqliteTypeInfo(DataType::DatetimeUtc)
1919
}
2020

2121
fn compatible(ty: &SqliteTypeInfo) -> bool {
22-
<NaiveDateTime as Type<Sqlite>>::compatible(ty)
22+
matches!(ty.0, DataType::DatetimeUtc) || <NaiveDateTime as Type<Sqlite>>::compatible(ty)
2323
}
2424
}
2525

0 commit comments

Comments
 (0)