|
| 1 | +// |
| 2 | +// Copyright (c) 2016 Jon Carlson. All rights reserved. |
| 3 | +// Use of this source code is governed by an MIT-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | +// |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import "fmt" |
| 10 | +import "sort" |
| 11 | +import "database/sql" |
| 12 | +import "github.com/joncrlsn/pgutil" |
| 13 | +import "github.com/joncrlsn/misc" |
| 14 | + |
| 15 | +// ================================== |
| 16 | +// FunctionRows definition |
| 17 | +// ================================== |
| 18 | + |
| 19 | +// FunctionRows is a sortable slice of string maps |
| 20 | +type FunctionRows []map[string]string |
| 21 | + |
| 22 | +func (slice FunctionRows) Len() int { |
| 23 | + return len(slice) |
| 24 | +} |
| 25 | + |
| 26 | +func (slice FunctionRows) Less(i, j int) bool { |
| 27 | + return slice[i]["function_name"] < slice[j]["function_name"] |
| 28 | +} |
| 29 | + |
| 30 | +func (slice FunctionRows) Swap(i, j int) { |
| 31 | + slice[i], slice[j] = slice[j], slice[i] |
| 32 | +} |
| 33 | + |
| 34 | +// FunctionSchema holds a channel streaming function information from one of the databases as well as |
| 35 | +// a reference to the current row of data we're viewing. |
| 36 | +// |
| 37 | +// FunctionSchema implements the Schema interface defined in pgdiff.go |
| 38 | +type FunctionSchema struct { |
| 39 | + rows FunctionRows |
| 40 | + rowNum int |
| 41 | + done bool |
| 42 | +} |
| 43 | + |
| 44 | +// get returns the value from the current row for the given key |
| 45 | +func (c *FunctionSchema) get(key string) string { |
| 46 | + if c.rowNum >= len(c.rows) { |
| 47 | + return "" |
| 48 | + } |
| 49 | + return c.rows[c.rowNum][key] |
| 50 | +} |
| 51 | + |
| 52 | +// NextRow increments the rowNum and tells you whether or not there are more |
| 53 | +func (c *FunctionSchema) NextRow() bool { |
| 54 | + if c.rowNum >= len(c.rows)-1 { |
| 55 | + c.done = true |
| 56 | + } |
| 57 | + c.rowNum = c.rowNum + 1 |
| 58 | + return !c.done |
| 59 | +} |
| 60 | + |
| 61 | +// Compare tells you, in one pass, whether or not the first row matches, is less than, or greater than the second row |
| 62 | +func (c *FunctionSchema) Compare(obj interface{}) int { |
| 63 | + c2, ok := obj.(*FunctionSchema) |
| 64 | + if !ok { |
| 65 | + fmt.Println("Error!!!, Compare(obj) needs a FunctionSchema instance", c2) |
| 66 | + return +999 |
| 67 | + } |
| 68 | + |
| 69 | + val := misc.CompareStrings(c.get("function_name"), c2.get("function_name")) |
| 70 | + //fmt.Printf("-- Compared %v: %s with %s \n", val, c.get("function_name"), c2.get("function_name")) |
| 71 | + return val |
| 72 | +} |
| 73 | + |
| 74 | +// Add returns SQL to create the function |
| 75 | +func (c FunctionSchema) Add() { |
| 76 | + fmt.Println("-- STATEMENT-BEGIN") |
| 77 | + fmt.Println(c.get("definition")) |
| 78 | + fmt.Println("-- STATEMENT-END") |
| 79 | +} |
| 80 | + |
| 81 | +// Drop returns SQL to drop the function |
| 82 | +func (c FunctionSchema) Drop() { |
| 83 | + fmt.Println("-- Note that CASCADE in the statement below will also drop any triggers depending on this function.") |
| 84 | + fmt.Println("-- Also, if there are two functions with this name, you will need to add arguments to identify the correct one to drop.") |
| 85 | + fmt.Println("-- (See http://www.postgresql.org/docs/9.4/interactive/sql-dropfunction.html) ") |
| 86 | + fmt.Printf("DROP FUNCTION %s CASCADE;\n", c.get("function_name")) |
| 87 | +} |
| 88 | + |
| 89 | +// Change handles the case where the function names match, but the definition does not |
| 90 | +func (c FunctionSchema) Change(obj interface{}) { |
| 91 | + c2, ok := obj.(*FunctionSchema) |
| 92 | + if !ok { |
| 93 | + fmt.Println("Error!!!, Change needs a FunctionSchema instance", c2) |
| 94 | + } |
| 95 | + if c.get("definition") != c2.get("definition") { |
| 96 | + fmt.Println("-- This function is different so we'll recreate it:") |
| 97 | + // The definition column has everything needed to rebuild the function |
| 98 | + fmt.Println("-- STATEMENT-BEGIN") |
| 99 | + fmt.Println(c.get("definition")) |
| 100 | + fmt.Println("-- STATEMENT-END") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// compareFunctions outputs SQL to make the functions match between DBs |
| 105 | +func compareFunctions(conn1 *sql.DB, conn2 *sql.DB) { |
| 106 | + sql := ` |
| 107 | + SELECT p.oid::regprocedure AS function_name |
| 108 | + , t.typname AS return_type |
| 109 | + , pg_get_functiondef(p.oid) AS definition |
| 110 | + FROM pg_proc AS p |
| 111 | + JOIN pg_type t ON (p.prorettype = t.oid) |
| 112 | + JOIN pg_namespace n ON (n.oid = p.pronamespace) |
| 113 | + JOIN pg_language l ON (p.prolang = l.oid AND l.lanname IN ('c','plpgsql', 'sql')) |
| 114 | + WHERE n.nspname = 'public'; |
| 115 | + ` |
| 116 | + |
| 117 | + rowChan1, _ := pgutil.QueryStrings(conn1, sql) |
| 118 | + rowChan2, _ := pgutil.QueryStrings(conn2, sql) |
| 119 | + |
| 120 | + rows1 := make(FunctionRows, 0) |
| 121 | + for row := range rowChan1 { |
| 122 | + rows1 = append(rows1, row) |
| 123 | + } |
| 124 | + sort.Sort(rows1) |
| 125 | + |
| 126 | + rows2 := make(FunctionRows, 0) |
| 127 | + for row := range rowChan2 { |
| 128 | + rows2 = append(rows2, row) |
| 129 | + } |
| 130 | + sort.Sort(rows2) |
| 131 | + |
| 132 | + // We must explicitly type this as Schema here |
| 133 | + var schema1 Schema = &FunctionSchema{rows: rows1, rowNum: -1} |
| 134 | + var schema2 Schema = &FunctionSchema{rows: rows2, rowNum: -1} |
| 135 | + |
| 136 | + // Compare the functions |
| 137 | + doDiff(schema1, schema2) |
| 138 | +} |
0 commit comments