postgres-schema-ts icon indicating copy to clipboard operation
postgres-schema-ts copied to clipboard

Column order

Open neurosnap opened this issue 4 years ago • 2 comments

Greetings!

I was wondering what you thought about the column order for the tables? If I'm working on two different machines and for whatever reason the columns were not added in the same order, the definition file will contain a diff between the two computers. If we ordered them consistently instead of relying on whatever postgres returns, it might make it easier to maintain a clean diff between machines.

What do you think?

neurosnap avatar Jan 17 '21 00:01 neurosnap

That's a good idea @neurosnap. We could sort the column names based on when they were added and keep pushing new added columns to the bottom, I think that would make diffs fairly consistent.

WDYT?

nettofarah avatar Jan 27 '21 15:01 nettofarah

I found you can use eslint-plugin-sort-keys-fix to post process key order, but there doesn't seem to be a similar interface sorter. Would be very convenient to just add a sort to the allTables function in pg-client

sitch avatar Jan 26 '22 05:01 sitch

I have found a similar issue with table order. I think that changing the query inside the function tableNames https://github.com/nettofarah/postgres-schema-ts/blob/86aa97a3388767a85f140711887d4aed2b732489/src/pg-client.ts#L41C1-L47C4 from this:

`SELECT table_name FROM information_schema.columns WHERE table_schema = $1 GROUP BY table_name`

to:

`SELECT table_name FROM information_schema.columns WHERE table_schema = $1 GROUP BY table_name  order by table_name`

so the order of the tables will be fixed.

About the column order, there is a field called ordinal_position that could be used. Inside the function getTable https://github.com/nettofarah/postgres-schema-ts/blob/master/src/pg-client.ts#L75-L93 we could add the order statement to the query:

SELECT column_name, udt_name, is_nullable
        FROM information_schema.columns
        WHERE table_name = $1 and table_schema = $2
order by  order by ordinal_position

gpad avatar Jan 11 '24 15:01 gpad

Nice find. I'd be happy to merge a PR if you're down to work on it @gpad

nettofarah avatar Jan 11 '24 18:01 nettofarah

Nice find. I'd be happy to merge a PR if you're down to work on it @gpad

I'm trying, but I have some issues running the test locally. I was not able to connect to DB ... When the PG client tries to connect to DB it remains stuck ...

gpad avatar Jan 12 '24 07:01 gpad

Hi @nettofarah, I was not able to run the tests, but I have created the PR https://github.com/nettofarah/postgres-schema-ts/pull/11 let me know if it's OK for you and if you merge it and release a new version.

gpad avatar Jan 12 '24 08:01 gpad

hi @nettofarah sorry to push you again but it will be fine if you push a new version on npm.js with the last fix.

Let me know if I can help you in some way

gpad avatar Jan 23 '24 09:01 gpad

Hey, @gpad and @neurosnap sorry about the delay here. I have a newborn at home who's taking pretty much all of our free time 😍

Here's a new release: https://github.com/nettofarah/postgres-schema-ts/releases/tag/v0.5.0

nettofarah avatar Jan 23 '24 14:01 nettofarah