|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | +import "database/sql" |
| 5 | +import "github.com/joncrlsn/pgutil" |
| 6 | + |
| 7 | +// UniqueSchema holds a channel streaming foreign key data from one of the databases as well as |
| 8 | +// a reference to the current row of data we're viewing. |
| 9 | +// |
| 10 | +// UniqueSchema implements the Schema interface defined in pgdiff.go |
| 11 | +type UniqueSchema struct { |
| 12 | + channel chan map[string]string |
| 13 | + row map[string]string |
| 14 | + done bool |
| 15 | +} |
| 16 | + |
| 17 | +// NextRow reads from the channel and tells you if there are (probably) more or not |
| 18 | +func (c *UniqueSchema) NextRow() bool { |
| 19 | + c.row = <-c.channel |
| 20 | + if len(c.row) == 0 { |
| 21 | + c.done = true |
| 22 | + } |
| 23 | + return !c.done |
| 24 | +} |
| 25 | + |
| 26 | +// Compare tells you, in one pass, whether or not the first row matches, is less than, or greater than the second row |
| 27 | +func (c *UniqueSchema) Compare(obj interface{}) int { |
| 28 | + c2, ok := obj.(*UniqueSchema) |
| 29 | + if !ok { |
| 30 | + fmt.Println("Error!!!, Change(...) needs a UniqueSchema instance", c2) |
| 31 | + return +999 |
| 32 | + } |
| 33 | + val := _compareString(c.row["table_name"], c2.row["table_name"]) |
| 34 | + if val != 0 { |
| 35 | + return val |
| 36 | + } |
| 37 | + val = _compareString(c.row["constraint_name"], c2.row["constraint_name"]) |
| 38 | + return val |
| 39 | +} |
| 40 | + |
| 41 | +// Add returns SQL to add the primary key |
| 42 | +func (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()) |
| 46 | +} |
| 47 | + |
| 48 | +// Drop returns SQL to drop the foreign key |
| 49 | +func (c UniqueSchema) Drop() { |
| 50 | + fmt.Printf("ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s;\n", c.row["table_name"], c.row["constraint_name"]) |
| 51 | +} |
| 52 | + |
| 53 | +// Change handles the case where the table name matches, but the details do not |
| 54 | +func (c UniqueSchema) Change(obj interface{}) { |
| 55 | + c2, ok := obj.(*UniqueSchema) |
| 56 | + if !ok { |
| 57 | + fmt.Println("Error!!!, change needs a UniqueSchema instance", c2) |
| 58 | + } |
| 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 |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * Compare the primary keys in the two databases. This SQL can handle up to 5 columns |
| 88 | + * as part of the primary key |
| 89 | + */ |
| 90 | +func compareUniqueConstraints(conn1 *sql.DB, conn2 *sql.DB) { |
| 91 | + 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;` |
| 107 | + |
| 108 | + rowChan1, _ := pgutil.QueryStrings(conn1, sql) |
| 109 | + rowChan2, _ := pgutil.QueryStrings(conn2, sql) |
| 110 | + |
| 111 | + // We have to explicitly type this as Schema for some unknown reason |
| 112 | + var schema1 Schema = &UniqueSchema{channel: rowChan1} |
| 113 | + var schema2 Schema = &UniqueSchema{channel: rowChan2} |
| 114 | + |
| 115 | + // Compare the columns |
| 116 | + doDiff(schema1, schema2) |
| 117 | +} |
0 commit comments