jschaf / pggen

Generate type-safe Go for any Postgres query. If Postgres can run the query, pggen can generate code for it.
MIT License
287 stars 26 forks source link

Using output dir with multiple query files with same basename overwrites files #28

Closed sadmansakib closed 3 years ago

sadmansakib commented 3 years ago

pggen release: 2021-04-18

Code generation failed for following example configuration:

 pggen gen go \
     --schema-glob '${schema_path}/*.up.sql' \
     --query-glob '${query_path}/A/query.sql' \
     --query-glob '${query_path}/B/query.sql' \
     --query-glob '${query_path}/C/query.sql' \
     --query-glob '${query_path}/D/query.sql' \
     --query-glob '${query_path}/E/query.sql' \
     --query-glob '${query_path}/F/query.sql' \
     --go-type 'int8=int8' \
     --go-type 'int=int' \
     --go-type 'varchar=string' \
     --postgres-connection '${db_url}' \
     --output-dir 'db'  

result : generated 6 query files

Although pggen is generating Codes for only F query directory and entire DBQuerier and Querier interface is missing from the output directory. I am using golang migrate for maintaining database migrations so my db schema are in A.up.sql format in schema directory

update: renaming query.sql files generates all codes. it seems pggen is overwriting generated codes because of same file name

jschaf commented 3 years ago

Yep, definitely a bug. It sounds like pggen is trying to reuse the query.sql.go name for each directory A, B, ..., F. The F directory wins since it's last.

I think I'll use the following name algorithm:

sadmansakib commented 3 years ago

Is it possible to generate the query.sql.go from the first folder than traverse other folders and append generated code to the existing query.sql.go file?

jschaf commented 3 years ago

I prefer the conceptual simplicity of 1 query file producing 1 go file. Though, it wouldn't be too hard to do it after the fact with a sed script.