slick-pg icon indicating copy to clipboard operation
slick-pg copied to clipboard

Enum codegen example

Open Yaneeve opened this issue 9 years ago • 17 comments

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!

Yaneeve avatar Jun 16 '15 08:06 Yaneeve

Well, I'll update the code gen example project asap.

tminglei avatar Jun 19 '15 13:06 tminglei

Thanks! Appreciate it very much :)

Yaneeve avatar Jun 21 '15 05:06 Yaneeve

Hi @Yaneeve, I have submitted some updates in this pull, but it's not ready yet.

tminglei avatar Jul 08 '15 11:07 tminglei

Yeah, I noticed... I certainly appreciate your responsiveness :) take as much time as you need

Yaneeve avatar Jul 08 '15 12:07 Yaneeve

Ok, I need more time. ;-)

tminglei avatar Jul 08 '15 12:07 tminglei

I have working example if interested.

bearrito avatar Jul 17 '15 22:07 bearrito

@bearrito, yes, that's great. Can you send it to me? Thanks so much!

tminglei avatar Jul 18 '15 01:07 tminglei

I can see this issue is still open? Is there a clear example somewhere of slick codegen working with Postgres enum types?

victrcodes avatar Jan 04 '16 09:01 victrcodes

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.

tminglei avatar Jan 07 '16 13:01 tminglei

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?

victrcodes avatar Jan 07 '16 14:01 victrcodes

@vrcod thanks for the reminding! :-)

I'll check JOOQ's codes to find how to distinguish and generate it.

tminglei avatar Jan 07 '16 23:01 tminglei

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 avatar Sep 07 '17 12:09 tlewowski

@tlewowski yes, pls. 👍

tminglei avatar Sep 07 '17 13:09 tminglei

I've posted a project here: https://github.com/tlewowski/pg-scala-enums

what it needs:

  • adjustment of dbUrl in build.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), with implicits 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 implicits 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 avatar Sep 08 '17 16:09 tlewowski

@tlewowski it looks good. :-)

tminglei avatar Sep 10 '17 10:09 tminglei

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?

tlewowski avatar Sep 23 '17 19:09 tlewowski

I think it should be kept as a separate project, since it's more like a solution instead of a patch.

tminglei avatar Sep 24 '17 00:09 tminglei