weldsorm / welds

BSD 3-Clause "New" or "Revised" License
119 stars 6 forks source link

Support custom sqlx types #26

Open wvffle opened 3 months ago

wvffle commented 3 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.