|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | +import "database/sql" |
| 5 | +import "github.com/joncrlsn/pgutil" |
| 6 | + |
| 7 | +// SequenceSchema holds a channel streaming table information from one of the databases as well as |
| 8 | +// a reference to the current row of data we're viewing. |
| 9 | +// |
| 10 | +// SequenceSchema implements the Schema interface defined in pgdiff.go |
| 11 | +type SequenceSchema 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 *SequenceSchema) 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 *SequenceSchema) Compare(obj interface{}) int { |
| 28 | + c2, ok := obj.(*SequenceSchema) |
| 29 | + if !ok { |
| 30 | + fmt.Println("Error!!!, Change(...) needs a SequenceSchema instance", c2) |
| 31 | + return +999 |
| 32 | + } |
| 33 | + |
| 34 | + val := _compareString(c.row["sequence_name"], c2.row["sequence_name"]) |
| 35 | + return val |
| 36 | +} |
| 37 | + |
| 38 | +// Add returns SQL to add the table |
| 39 | +func (c SequenceSchema) Add() { |
| 40 | + fmt.Printf("CREATE SEQUENCE %s INCREMENT %s MINVALUE %s MAXVALUE %s START %s;\n", c.row["sequence_name"], c.row["increment"], c.row["minimum_value"], c.row["maximum_value"], c.row["start_value"]) |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +// Drop returns SQL to drop the table |
| 45 | +func (c SequenceSchema) Drop() { |
| 46 | + fmt.Printf("DROP SEQUENCE IF EXISTS %s;\n", c.row["sequence_name"]) |
| 47 | +} |
| 48 | + |
| 49 | +// Change handles the case where the table and column match, but the details do not |
| 50 | +func (c SequenceSchema) Change(obj interface{}) { |
| 51 | + c2, ok := obj.(*SequenceSchema) |
| 52 | + if !ok { |
| 53 | + fmt.Println("Error!!!, change needs a SequenceSchema instance", c2) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// compareSequences outputs SQL to make the sequences match between DBs |
| 58 | +func compareSequences(conn1 *sql.DB, conn2 *sql.DB) { |
| 59 | + sql := ` |
| 60 | +SELECT sequence_name, data_type, start_value |
| 61 | + , minimum_value, maximum_value |
| 62 | + , increment, cycle_option |
| 63 | +FROM information_schema.sequences |
| 64 | +WHERE sequence_schema = 'public' |
| 65 | +ORDER BY sequence_name ASC;` |
| 66 | + |
| 67 | + rowChan1, _ := pgutil.QueryStrings(conn1, sql) |
| 68 | + rowChan2, _ := pgutil.QueryStrings(conn2, sql) |
| 69 | + |
| 70 | + // We have to explicitly type this as Schema for some reason |
| 71 | + var schema1 Schema = &SequenceSchema{channel: rowChan1} |
| 72 | + var schema2 Schema = &SequenceSchema{channel: rowChan2} |
| 73 | + |
| 74 | + // Compare the tables |
| 75 | + doDiff(schema1, schema2) |
| 76 | +} |
0 commit comments