Skip to content

Commit

Permalink
fix(qe): support MySQL base64 newlines (#4955)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
Weakky and aqrln committed Jul 17, 2024
1 parent 7484fd8 commit 15f8fe4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libs/prisma-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn encode_bytes(bytes: &[u8]) -> String {
base64::encode(bytes)
}

pub fn decode_bytes(s: &str) -> PrismaValueResult<Vec<u8>> {
pub fn decode_bytes(s: impl AsRef<[u8]>) -> PrismaValueResult<Vec<u8>> {
base64::decode(s).map_err(|_| ConversionFailure::new("base64 encoded bytes", "PrismaValue::Bytes"))
}

Expand Down
20 changes: 19 additions & 1 deletion psl/psl-core/src/builtin_connectors/mysql_datamodel_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ impl Connector for MySqlDatamodelConnector {

// On MySQL, bytes are encoded as base64 in the database directly.
fn parse_json_bytes(&self, str: &str, _nt: Option<NativeTypeInstance>) -> PrismaValueResult<Vec<u8>> {
decode_bytes(str)
let mut buf = vec![0; str.len()];

// MySQL base64 encodes bytes with newlines every 76 characters.
decode_bytes(sanitize_base64(str, &mut buf))
}

fn runtime_join_strategy_support(&self) -> JoinStrategySupport {
Expand All @@ -300,3 +303,18 @@ impl Connector for MySqlDatamodelConnector {
}
}
}

/// Removes newlines from a base64 string.
fn sanitize_base64<'a>(mut s: &str, buf: &'a mut [u8]) -> &'a [u8] {
let mut pos = 0;

while !s.is_empty() && pos < buf.len() {
let nl = s.find('\n').unwrap_or(s.len());
let len = nl.min(buf.len() - pos);
buf[pos..pos + len].copy_from_slice(&s.as_bytes()[..len]);
pos += len;
s = &s[(nl + 1).min(s.len())..];
}

&buf[0..pos]
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ mod scalar_relations {

insta::assert_snapshot!(
run_query!(&runner, r#"{ findManyParent { id children { childId string int bInt float bytes bool dt } } }"#),
@r###"{"data":{"findManyParent":[{"id":1,"children":[{"childId":1,"string":"abc","int":1,"bInt":"1","float":1.5,"bytes":"AQID","bool":false,"dt":"1900-10-10T01:10:10.001Z"},{"childId":2,"string":"def","int":-4234234,"bInt":"14324324234324","float":-2.54367,"bytes":"FDSF","bool":true,"dt":"1999-12-12T21:12:12.121Z"}]}]}}"###
@r###"{"data":{"findManyParent":[{"id":1,"children":[{"childId":1,"string":"abc","int":1,"bInt":"1","float":1.5,"bytes":"VGhpcyBpcyBhIGxhcmdlIGJhc2U2NCBzdHJpbmcgdGhhdCBlbnN1cmVzIHdlIHNhbml0aXplIHRoZSBvdXRwdXQgb2YgTXlTUUwgYmFzZTY0IHN0cmluZy4=","bool":false,"dt":"1900-10-10T01:10:10.001Z"},{"childId":2,"string":"def","int":-4234234,"bInt":"14324324234324","float":-2.54367,"bytes":"FDSF","bool":true,"dt":"1999-12-12T21:12:12.121Z"}]}]}}"###
);

insta::assert_snapshot!(
run_query!(&runner, r#"{ findUniqueParent(where: { id: 1 }) { id children { childId string int bInt float bytes bool dt } } }"#),
@r###"{"data":{"findUniqueParent":{"id":1,"children":[{"childId":1,"string":"abc","int":1,"bInt":"1","float":1.5,"bytes":"AQID","bool":false,"dt":"1900-10-10T01:10:10.001Z"},{"childId":2,"string":"def","int":-4234234,"bInt":"14324324234324","float":-2.54367,"bytes":"FDSF","bool":true,"dt":"1999-12-12T21:12:12.121Z"}]}}}"###
@r###"{"data":{"findUniqueParent":{"id":1,"children":[{"childId":1,"string":"abc","int":1,"bInt":"1","float":1.5,"bytes":"VGhpcyBpcyBhIGxhcmdlIGJhc2U2NCBzdHJpbmcgdGhhdCBlbnN1cmVzIHdlIHNhbml0aXplIHRoZSBvdXRwdXQgb2YgTXlTUUwgYmFzZTY0IHN0cmluZy4=","bool":false,"dt":"1900-10-10T01:10:10.001Z"},{"childId":2,"string":"def","int":-4234234,"bInt":"14324324234324","float":-2.54367,"bytes":"FDSF","bool":true,"dt":"1999-12-12T21:12:12.121Z"}]}}}"###
);

insta::assert_snapshot!(
Expand Down Expand Up @@ -327,7 +327,7 @@ mod scalar_relations {
int: 1,
bInt: 1,
float: 1.5,
bytes: "AQID",
bytes: "VGhpcyBpcyBhIGxhcmdlIGJhc2U2NCBzdHJpbmcgdGhhdCBlbnN1cmVzIHdlIHNhbml0aXplIHRoZSBvdXRwdXQgb2YgTXlTUUwgYmFzZTY0IHN0cmluZy4=",
bool: false,
dt: "1900-10-10T01:10:10.001Z",
}"#,
Expand Down

0 comments on commit 15f8fe4

Please sign in to comment.