weldsorm / welds

BSD 3-Clause "New" or "Revised" License
131 stars 7 forks source link

Support custom sqlx types #26

Open wvffle opened 4 months ago

wvffle commented 4 months ago

I've just migrated my app to welds and it is pretty neat.

Although it throws me some false-positive errors regarding custom types when doing welds::check::schema:

Decimal type

2024-07-14T16:58:00.546408Z ERROR api::models: public.pass_types (High): The Column `price` is has changed, db_type: NUMERIC welds_type: Decimal

Here, the type is sqlx::types::Decimal and it's defined as NUMERIC(12,2) in the database.

Custom enum type

2024-07-14T16:58:00.546336Z ERROR api::models: public.passes (High): The Column `buying_place` is has changed, db_type: BUYING_PLACE welds_type: BuyingPlace

Here, it is an enum type that I want to store in the database. It derives sqlx::Type and renames the type to buying_place. Sqlx does not have any problems with that.

#[derive(Debug, sqlx::Type, Eq, PartialEq, Enum, Copy, Clone)]
#[sqlx(type_name = "buying_place", rename_all = "snake_case")]
pub enum BuyingPlace {
    PlaceA,
    PlaceB,
    PlaceC,
}

Custom newtype

2024-07-14T16:58:00.546444Z ERROR api::models: public.section_types (High): The Column `days` is has changed, db_type: BIT welds_type: Bits<7>

This is a newtype implemented by me to represent a BIT(7) type in the postgres database. It wraps around the sqlx::types::BitVec type, and manually implements sqlx::Type which contains information about the correct type name.

TheGP commented 1 week ago

So no support for enums right now? @wvffle @cajun @lex148 @sunnyregion

lex148 commented 1 week ago

@TheGP welds is really just a wrapper around sqlx and tiberius. Type wise, if they support it we do too. sqlx supports direct mapping of you enum types into custom postgres enum types. this is functional in welds, but runtime level database verification can get a little confused. welds::check::schema::* I need to bump this in priority and address.