From 03bbf74e8888081b208dceaab5dd3e17707fab3e Mon Sep 17 00:00:00 2001 From: Georg Semmler Date: Tue, 13 Nov 2018 10:57:02 +0100 Subject: [PATCH] Workaround a uuid bump breaking change (This needs a cargo nightly feature so we cannot do this now, but as soon as this hit's stable) --- diesel/Cargo.toml | 1 + diesel/src/pg/types/uuid_v0_7.rs | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 diesel/src/pg/types/uuid_v0_7.rs diff --git a/diesel/Cargo.toml b/diesel/Cargo.toml index 5eabc3bd58e2..3edf4898a18c 100644 --- a/diesel/Cargo.toml +++ b/diesel/Cargo.toml @@ -24,6 +24,7 @@ serde_json = { version = ">=0.8.0, <2.0", optional = true } time = { version = "0.1", optional = true } url = { version = "1.4.0", optional = true } uuid = { version = ">=0.2.0, <0.7.0", optional = true, features = ["use_std"] } +uuidv07 = { version = "0.7.0", optional = true, package = "uuid"} ipnetwork = { version = ">=0.12.2, <0.14.0", optional = true } num-bigint = { version = ">=0.1.41, <0.3", optional = true } num-traits = { version = "0.2", optional = true } diff --git a/diesel/src/pg/types/uuid_v0_7.rs b/diesel/src/pg/types/uuid_v0_7.rs new file mode 100644 index 000000000000..03ad3ae9a85f --- /dev/null +++ b/diesel/src/pg/types/uuid_v0_7.rs @@ -0,0 +1,59 @@ +extern crate uuidv07 as uuid; + +use std::io::prelude::*; + +use deserialize::{self, FromSql}; +use pg::Pg; +use serialize::{self, IsNull, Output, ToSql}; +use sql_types::Uuid; + +#[derive(FromSqlRow, AsExpression)] +#[diesel(foreign_derive)] +#[sql_type = "Uuid"] +#[allow(dead_code)] +struct UuidProxy(uuid::Uuid); + +impl FromSql for uuid::Uuid { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let bytes = not_none!(bytes); + uuid::Uuid::from_slice(bytes).map_err(|e| e.into()) + } +} + +impl ToSql for uuid::Uuid { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + out.write_all(self.as_bytes()) + .map(|_| IsNull::No) + .map_err(Into::into) + } +} + +#[test] +fn uuid_to_sql() { + let mut bytes = Output::test(); + let test_uuid = uuid::Uuid::from_fields(0xFFFF_FFFF, 0xFFFF, 0xFFFF, b"abcdef12").unwrap(); + ToSql::::to_sql(&test_uuid, &mut bytes).unwrap(); + assert_eq!(bytes, test_uuid.as_bytes()); +} + +#[test] +fn some_uuid_from_sql() { + let input_uuid = uuid::Uuid::from_fields(0xFFFF_FFFF, 0xFFFF, 0xFFFF, b"abcdef12").unwrap(); + let output_uuid = FromSql::::from_sql(Some(input_uuid.as_bytes())).unwrap(); + assert_eq!(input_uuid, output_uuid); +} + +#[test] +fn bad_uuid_from_sql() { + let uuid = uuid::Uuid::from_sql(Some(b"boom")); + assert_eq!(uuid.unwrap_err().description(), "UUID parse error"); +} + +#[test] +fn no_uuid_from_sql() { + let uuid = uuid::Uuid::from_sql(None); + assert_eq!( + uuid.unwrap_err().description(), + "Unexpected null for non-null column" + ); +}