slick-pg
slick-pg copied to clipboard
Enum codegen example
Thanks for your awesome work!
Could you please provide an example where I would generate code that would convert a postgres enum. I am new to scala and slick, and can't seem to get this right.
Thanks!
Well, I'll update the code gen example project asap.
Thanks! Appreciate it very much :)
Hi @Yaneeve, I have submitted some updates in this pull, but it's not ready yet.
Yeah, I noticed... I certainly appreciate your responsiveness :) take as much time as you need
Ok, I need more time. ;-)
I have working example if interested.
@bearrito, yes, that's great. Can you send it to me? Thanks so much!
I can see this issue is still open? Is there a clear example somewhere of slick codegen working with Postgres enum types?
Not yet.
But pg enum types can only be mapped to String
, since it can't distinguish pg enum types from others and generate a scala type for them.
Curiously, just to note something - JOOQ code generator works just fine with this situation - recognizes postgres enum types just fine and creates java enums accordingly, so it's not impossible, is it?
@vrcod thanks for the reminding! :-)
I'll check JOOQ's codes to find how to distinguish and generate it.
just a follow up - I'd also love to see possibility of generating enumeration classes from database schema.
For my case (it's the simplest - just download all enum values together with labels and orders) sufficient SQL query was
select t.typename, e.enumlabel, e.enumsortorder from pg_enum e
inner join pg_type t on t.oid=e.enumtypid
then some postprocessing is needed, but it generates just fine.
I should be able to post a full solution to GitHub later this week, let me know if you're interested
@tlewowski yes, pls. 👍
I've posted a project here: https://github.com/tlewowski/pg-scala-enums
what it needs:
- adjustment of
dbUrl
inbuild.sbt
- direct access to database for executing user (no user/password thing)
what it does:
- fetches all enums via vanilla JDBC (no layers in between)
- generates a file with all enumerations (as subclasses of
Enumeration
) -gen-enums
task - generates a profile, based on
ExPostgresProfile
with many extensions (not configurable, changing extensions requires changes in code), withimplicit
s for generated enumerations included -gen-pg-profile
task - generates a file with tables, where enumerations are included as normal types -
gen-tables
stuff like package or file name is hardcoded in several places and naming convention for generated classes is pretty opinionated, but I don't think it'd be a big deal to change it if anybody needs it.
quite bad news is that it needs three execution steps - generate enum, generate profile and generate tables (they are now in three separate, dependent projects). This is required, because enums are needed for profile generation (profile includes implicit
s for them) and profile is (I suppose?) needed for downloading tables.
I've checked the mapping in my project (not included) and it works well with PgEnumSupport
.
BTW: thanks for this project, it really helps a lot!
@tlewowski it looks good. :-)
well, the thing is that it doesn't fit very well into the current flow, so I'm not sure if it's mergeable anyhow, or should I maintain it as separate project (ideally an SBT plugin). any ideas?
I think it should be kept as a separate project, since it's more like a solution instead of a patch.