@@ -34,20 +34,18 @@ func (c *UniqueSchema) Compare(obj interface{}) int {
3434 if val != 0 {
3535 return val
3636 }
37- val = _compareString (c .row ["constraint_name " ], c2 .row ["constraint_name " ])
37+ val = _compareString (c .row ["constraint_def " ], c2 .row ["constraint_def " ])
3838 return val
3939}
4040
41- // Add returns SQL to add the primary key
41+ // Add returns SQL to add the unique constraint
4242func (c UniqueSchema ) Add () {
43- // ALTER TABLE ONLY t_product ADD CONSTRAINT t_product_pkey PRIMARY KEY (product_id, seq_no);
44- // ALTER TABLE ONLY t_product ADD CONSTRAINT t_product_pkey UNIQUE (product_id);
45- fmt .Printf ("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s);\n " , c .row ["table_name" ], c .row ["constraint_name" ], c .uniqueColumnString ())
43+ fmt .Printf ("ALTER TABLE %s ADD CONSTRAINT %s %s;\n " , c .row ["table_name" ], c .row ["constraint_name" ], c .row ["constraint_def" ])
4644}
4745
48- // Drop returns SQL to drop the foreign key
46+ // Drop returns SQL to drop the unique constraint
4947func (c UniqueSchema ) Drop () {
50- fmt .Printf ("ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s; \n " , c .row ["table_name" ], c .row ["constraint_name" ])
48+ fmt .Printf ("ALTER TABLE %s DROP CONSTRAINT %s; -- %s\n " , c .row ["table_name" ], c .row ["constraint_name" ], c . row [ "constraint_def " ])
5149}
5250
5351// Change handles the case where the table name matches, but the details do not
@@ -56,54 +54,23 @@ func (c UniqueSchema) Change(obj interface{}) {
5654 if ! ok {
5755 fmt .Println ("Error!!!, change needs a UniqueSchema instance" , c2 )
5856 }
59- pk1 := c .uniqueColumnString ()
60- pk2 := c .uniqueColumnString ()
61- if pk1 != pk2 {
62- fmt .Printf ("-- Warning, primary key is different for table %s pk1:%s pk2:%s\n " , c .row ["table_name" ], pk1 , pk2 )
63- fmt .Printf ("ALTER TABLE %s DROP CONSTRAINT %s;\n " , c .row ["table_name" ], c2 .row ["constraint_name" ])
64- fmt .Printf ("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s);\n " , c .row ["table_name" ], c .row ["constraint_name" ], c .uniqueColumnString ())
65- }
66- }
67-
68- // uniqueColumnString concatenates the primary key column names into one string.
69- // It's possible this could be done with SQL, I just haven't figured it out yet
70- func (c UniqueSchema ) uniqueColumnString () string {
71- pkey := ""
72- for i := 1 ; i <= 5 ; i ++ {
73- colName := fmt .Sprintf ("col%d" , i )
74- col := c .row [colName ]
75- //fmt.Printf("-- colName: %s val:'%s'\n", colName, col)
76- if len (col ) > 0 {
77- if len (pkey ) > 0 {
78- pkey = pkey + ","
79- }
80- pkey = pkey + col
81- }
82- }
83- return pkey
57+ // There is no "changing" a unique constraint. It either gets created or dropped (or left as-is).
8458}
8559
8660/*
87- * Compare the primary keys in the two databases. This SQL can handle up to 5 columns
88- * as part of the primary key
61+ * Compare the primary keys in the two databases. We do not recreate unique if just the name is different.
8962 */
9063func compareUniqueConstraints (conn1 * sql.DB , conn2 * sql.DB ) {
9164 sql := `
92- SELECT tc.table_name
93- , kcu.constraint_name
94- , MAX(CASE WHEN kcu.ordinal_position = 1 THEN kcu.column_name ELSE '' END) AS col1
95- , MAX(CASE WHEN kcu.ordinal_position = 2 THEN kcu.column_name ELSE '' END) AS col2
96- , MAX(CASE WHEN kcu.ordinal_position = 3 THEN kcu.column_name ELSE '' END) AS col3
97- , MAX(CASE WHEN kcu.ordinal_position = 4 THEN kcu.column_name ELSE '' END) AS col4
98- , MAX(CASE WHEN kcu.ordinal_position = 5 THEN kcu.column_name ELSE '' END) AS col5
99- FROM information_schema.table_constraints AS tc
100- LEFT JOIN information_schema.key_column_usage kcu
101- ON tc.constraint_catalog = kcu.constraint_catalog
102- AND tc.constraint_schema = kcu.constraint_schema
103- AND tc.constraint_name = kcu.constraint_name
104- WHERE tc.constraint_type = 'UNIQUE'
105- GROUP BY tc.table_name, kcu.constraint_name
106- ORDER BY tc.table_name, kcu.constraint_name COLLATE "C" ASC;`
65+ SELECT c.conname AS constraint_name
66+ , c.contype AS constraint_type
67+ , cl.relname AS table_name
68+ , pg_catalog.pg_get_constraintdef(c.oid, true) as constraint_def
69+ FROM pg_catalog.pg_constraint c
70+ INNER JOIN pg_class AS cl ON (c.conrelid = cl.oid)
71+ WHERE c.contype = 'u'
72+ ORDER BY cl.relname::varchar, pg_catalog.pg_get_constraintdef(c.oid, true) COLLATE "C" ASC;
73+ `
10774
10875 rowChan1 , _ := pgutil .QueryStrings (conn1 , sql )
10976 rowChan2 , _ := pgutil .QueryStrings (conn2 , sql )
0 commit comments