joncrlsn / pgdiff

Compares the PostgreSQL schema between two databases and generates SQL statements that can be run manually against the second database to make their schemas match.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

m3dbedb opened this issue · comments

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.

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

Can you create a pull request for that change?

Can you create a pull request for that change?

#46

Thank you!

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.

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.