Skip to content

Commit d1f111a

Browse files
committed
Return unsupported Postgres types as raw blobs
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent ace9687 commit d1f111a

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

crates/factor-outbound-pg/src/types.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use tokio_postgres::{types::ToSql, Row};
55
mod convert;
66
mod decimal;
77
mod interval;
8+
mod other;
89
mod pg_null;
910

1011
use convert::{
@@ -14,6 +15,7 @@ use convert::{
1415
range_wit_to_pg, time_pg_to_wit, time_wit_to_pg, timestamp_wit_to_pg, uuid_wit_to_pg,
1516
};
1617
use interval::Interval;
18+
use other::Other;
1719
use pg_null::PgNull;
1820

1921
pub fn convert_data_type(pg_type: &Type) -> DbDataType {
@@ -42,7 +44,7 @@ pub fn convert_data_type(pg_type: &Type) -> DbDataType {
4244
Type::INTERVAL => DbDataType::Interval,
4345
_ => {
4446
tracing::debug!("Couldn't convert Postgres type {} to WIT", pg_type.name(),);
45-
DbDataType::Other
47+
DbDataType::Other(pg_type.name().to_owned())
4648
}
4749
}
4850
}
@@ -126,7 +128,7 @@ pub fn convert_entry(row: &Row, index: usize) -> anyhow::Result<DbValue> {
126128
t.name(),
127129
column.name()
128130
);
129-
Ok(DbValue::Unsupported)
131+
map_db_value(row, index, DbValue::Unsupported, |v: Other| v.into())
130132
}
131133
}
132134
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// A catch-all type via which we return unsupported Postgres values as blobs.
2+
#[derive(Debug)]
3+
pub struct Other(Vec<u8>);
4+
5+
impl tokio_postgres::types::FromSql<'_> for Other {
6+
fn from_sql(
7+
_ty: &tokio_postgres::types::Type,
8+
raw: &'_ [u8],
9+
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
10+
Ok(Self(raw.to_owned()))
11+
}
12+
13+
fn accepts(_ty: &tokio_postgres::types::Type) -> bool {
14+
true
15+
}
16+
}
17+
18+
impl From<Other> for Vec<u8> {
19+
fn from(value: Other) -> Self {
20+
value.0
21+
}
22+
}

crates/world/src/conversions.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod rdbms_types {
7676
pg4::DbValue::Str(s) => v1::rdbms_types::DbValue::Str(s),
7777
pg4::DbValue::Binary(b) => v1::rdbms_types::DbValue::Binary(b),
7878
pg4::DbValue::DbNull => v1::rdbms_types::DbValue::DbNull,
79-
pg4::DbValue::Unsupported => v1::rdbms_types::DbValue::Unsupported,
79+
pg4::DbValue::Unsupported(_) => v1::rdbms_types::DbValue::Unsupported,
8080
_ => v1::rdbms_types::DbValue::Unsupported,
8181
}
8282
}
@@ -95,7 +95,7 @@ mod rdbms_types {
9595
pg4::DbValue::Str(s) => v2::rdbms_types::DbValue::Str(s),
9696
pg4::DbValue::Binary(b) => v2::rdbms_types::DbValue::Binary(b),
9797
pg4::DbValue::DbNull => v2::rdbms_types::DbValue::DbNull,
98-
pg4::DbValue::Unsupported => v2::rdbms_types::DbValue::Unsupported,
98+
pg4::DbValue::Unsupported(_) => v2::rdbms_types::DbValue::Unsupported,
9999
_ => v2::rdbms_types::DbValue::Unsupported,
100100
}
101101
}
@@ -129,7 +129,7 @@ mod rdbms_types {
129129
pg4::DbValue::ArrayStr(_) => pg3::DbValue::Unsupported,
130130
pg4::DbValue::Interval(_) => pg3::DbValue::Unsupported,
131131
pg4::DbValue::DbNull => pg3::DbValue::DbNull,
132-
pg4::DbValue::Unsupported => pg3::DbValue::Unsupported,
132+
pg4::DbValue::Unsupported(_) => pg3::DbValue::Unsupported,
133133
}
134134
}
135135
}
@@ -146,7 +146,7 @@ mod rdbms_types {
146146
pg4::DbDataType::Floating64 => v1::rdbms_types::DbDataType::Floating64,
147147
pg4::DbDataType::Str => v1::rdbms_types::DbDataType::Str,
148148
pg4::DbDataType::Binary => v1::rdbms_types::DbDataType::Binary,
149-
pg4::DbDataType::Other => v1::rdbms_types::DbDataType::Other,
149+
pg4::DbDataType::Other(_) => v1::rdbms_types::DbDataType::Other,
150150
_ => v1::rdbms_types::DbDataType::Other,
151151
}
152152
}
@@ -164,7 +164,7 @@ mod rdbms_types {
164164
pg4::DbDataType::Floating64 => v2::rdbms_types::DbDataType::Floating64,
165165
pg4::DbDataType::Str => v2::rdbms_types::DbDataType::Str,
166166
pg4::DbDataType::Binary => v2::rdbms_types::DbDataType::Binary,
167-
pg4::DbDataType::Other => v2::rdbms_types::DbDataType::Other,
167+
pg4::DbDataType::Other(_) => v2::rdbms_types::DbDataType::Other,
168168
_ => v2::rdbms_types::DbDataType::Other,
169169
}
170170
}
@@ -197,7 +197,7 @@ mod rdbms_types {
197197
pg4::DbDataType::ArrayDecimal => pg3::DbDataType::Other,
198198
pg4::DbDataType::ArrayStr => pg3::DbDataType::Other,
199199
pg4::DbDataType::Interval => pg3::DbDataType::Other,
200-
pg4::DbDataType::Other => pg3::DbDataType::Other,
200+
pg4::DbDataType::Other(_) => pg3::DbDataType::Other,
201201
}
202202
}
203203
}

wit/deps/spin-postgres@4.0.0/postgres.wit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface postgres {
1111
}
1212

1313
/// Data types for a database column
14-
enum db-data-type {
14+
variant db-data-type {
1515
boolean,
1616
int8,
1717
int16,
@@ -36,7 +36,7 @@ interface postgres {
3636
array-decimal,
3737
array-str,
3838
interval,
39-
other,
39+
other(string),
4040
}
4141

4242
/// Database values
@@ -69,7 +69,7 @@ interface postgres {
6969
array-str(list<option<string>>),
7070
interval(interval),
7171
db-null,
72-
unsupported,
72+
unsupported(list<u8>),
7373
}
7474

7575
/// Values used in parameterized queries

0 commit comments

Comments
 (0)