Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include postgres comments in output #392

Merged
merged 6 commits into from
Sep 26, 2024
Merged

Conversation

VolkerLieber
Copy link
Contributor

@VolkerLieber VolkerLieber commented Sep 24, 2024

#391

Please double check as I'm not an expert in postgres internals!

@codecov-commenter
Copy link

codecov-commenter commented Sep 24, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.56%. Comparing base (6a0798e) to head (b5f04ff).
Report is 7 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #392      +/-   ##
==========================================
+ Coverage   90.49%   90.56%   +0.06%     
==========================================
  Files         125      125              
  Lines        7458     7460       +2     
==========================================
+ Hits         6749     6756       +7     
+ Misses        550      545       -5     
  Partials      159      159              
Flag Coverage Δ
90.56% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@go-jet
Copy link
Owner

go-jet commented Sep 24, 2024

Nice. Didn't know postgres has comment support like mysql.
Could you also include tests like the existing one for mysql:

func TestModelColumnComment(t *testing.T) {

and
func TestSQLBuilderColumnComment(t *testing.T) {

Note that test data files are in separate repo.

@VolkerLieber
Copy link
Contributor Author

WIP: Tests still missing. Will add them asap

@safaci2000
Copy link
Contributor

There's also support for individual table column comments. You could in theory add a // comment to each generated field if you like.

There's probably a much better way of doing this, but I wanted something that'll give you a single query for this.

here's the reference: https://www.postgresonline.com/journal/archives/215-Querying-table,-view,-column-and-function-descriptions.html

with table_comments as (
    SELECT n.nspname AS sname, c.relname As tname,
           CASE WHEN c.relkind = 'v' THEN 'view' ELSE 'table' END As type,
           d.description as table_comment
    FROM pg_class As c
             INNER JOIN pg_attribute As a ON c.oid = a.attrelid
             LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
             LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
             LEFT JOIN pg_description As d ON (d.objoid = c.oid AND d.objsubid = 0) --
    WHERE c.relkind IN('r', 'v') -- AND d.description > ''
    ORDER BY n.nspname, c.relname
), column_comments as (
    SELECT n.nspname as sname, c.relname as tname , a.attname As column_name,  d.description as column_description
    FROM pg_class As c
             INNER JOIN pg_attribute As a ON c.oid = a.attrelid
             LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
             LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
             LEFT JOIN pg_description As d ON (d.objoid = c.oid AND d.objsubid = a.attnum)
    WHERE  c.relkind IN('r', 'v') and d.description <> '' -- AND  n.nspname = 'SCHEMA' AND c.relname = 'TABLENAME'
    ORDER BY d.description, n.nspname, c.relname, a.attname
) select distinct t.sname, t.tname, t.table_comment, c.column_name, c.column_description from table_comments as t full outer join column_comments as c on t.tname = c.tname and t.sname = t.sname
where (t.table_comment <> '' or c.column_description <> '');

Should return the comments for both tables and columns.

@VolkerLieber
Copy link
Contributor Author

Thanks for the info :)
There seems to be a postgres function which I'm using in the latest PR.
I'll look into the url you provided tomorrow

@VolkerLieber
Copy link
Contributor Author

Waiting for go-jet/jet-test-data#6

@go-jet
Copy link
Owner

go-jet commented Sep 26, 2024

Thanks, looks good. 👍

@go-jet go-jet merged commit d17ab3d into go-jet:master Sep 26, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants