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

Generated columns order differs from reference DB's table (1.0 beta 1 for linux) #37

Open
m3dbedb opened this issue Jun 8, 2018 · 6 comments

Comments

@m3dbedb
Copy link

m3dbedb commented Jun 8, 2018

I have 2 DB, reference DB have 10 tables, target one have 9.
Original columns order of books_lost table:

CREATE TABLE books_lost (
    id integer NOT NULL,
    title text NOT NULL,
    author_id integer,
    subject_id integer
);

Order, formed by Pgdiff (sortted by alphabet)
6-COLUMN.sql

ALTER TABLE public.books_lost ADD COLUMN author_id integer;
ALTER TABLE public.books_lost ADD COLUMN id integer NOT NULL;
ALTER TABLE public.books_lost ADD COLUMN subject_id integer;
ALTER TABLE public.books_lost ADD COLUMN title text NOT NULL;

The difference affects error on futher data synchronization between DB's. It would be right to leave original columns order.

@Amerieum
Copy link
Contributor

to solve this issue I had to to replace the following line in column.go
, {{if eq $.DbSchema "*" }}table_schema || '.' || {{end}}table_name || '.' || column_name AS compare_name
by

, {{if eq $.DbSchema "*" }}table_schema || '.' || {{end}}table_name || '.' ||lpad(cast (ordinal_position as varchar), 5, '0')|| column_name AS compare_name

this would add up the cardinal position of the column in the sort criteria compare_name leading to correct order

Amerieum added a commit to Amerieum/pgdiff that referenced this issue Dec 29, 2020
joncrlsn#37

to solve this issue I had to to replace the following line in column.go
, {{if eq $.DbSchema "*" }}table_schema || '.' || {{end}}table_name || '.' || column_name AS compare_name
by

, {{if eq $.DbSchema "*" }}table_schema || '.' || {{end}}table_name || '.' ||lpad(cast (ordinal_position as varchar), 5, '0')|| column_name AS compare_name

this would add up the cardinal position of the column in the sort criteria compare_name leading to correct order
@joncrlsn
Copy link
Owner

joncrlsn commented Jan 8, 2021

Can you create a pull request for that change?

@Amerieum
Copy link
Contributor

Amerieum commented Feb 5, 2021

Can you create a pull request for that change?

#46

@joncrlsn
Copy link
Owner

joncrlsn commented Feb 9, 2021

Thank you!

joncrlsn added a commit that referenced this issue Jun 25, 2021
Proposed solution for issue #37
@fwiesweg
Copy link

Interestingly, I have two tables whose order in an existing database has diverted over time (due to column additions) from the order in the template schema for new deployments. The most recent commit of pgdiff creates an enormous amount of CREATE/DROP COLUMN statements when diffing those two for the purpose of adjusting to the column order, so I bisected it and identified this pull request as the cause, which sorta makes sense. Reverting it makes it work as before.

Could we have this configurable? Since existing the database is quite large, I do not want to actually re-order those columns for the sole purpose of matching the template schema, and I'd still like those two schemas to be considered as equal.

Otherwise, I'll just carry a little patch on our local fork, that'd be okay to.

@nixomose
Copy link

nixomose commented Sep 30, 2023

yeah I seem to be having the same problem. I don't care about column order, just that they exist or don't exist.
in my case though, it's actually altering the table to add columns in the order of the reference table and then dropping them causing the tables to be out of sync. I think I will try and undo the change as well and see if it fixes the problem for me.

EDIT: yup, much happier now, creates diffs regardless of column order and it doesn't yield incorrect column drops after adds.

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

No branches or pull requests

5 participants