|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +type ColumnSchema struct { |
| 6 | + channel chan map[string]string |
| 7 | + row map[string]string |
| 8 | +} |
| 9 | + |
| 10 | +// Reads from the channel and converts the end-of-channel value into a boolean |
| 11 | +func (c *ColumnSchema) NextRow(more bool) bool { |
| 12 | + c.row = <-c.channel |
| 13 | + if !more || len(c.row) == 0 { |
| 14 | + return false |
| 15 | + } |
| 16 | + return true |
| 17 | +} |
| 18 | + |
| 19 | +// Compare |
| 20 | +func (c ColumnSchema) Compare(obj interface{}) int { |
| 21 | + c2, ok := obj.(*ColumnSchema) |
| 22 | + if !ok { |
| 23 | + fmt.Println("Error!!!, change needs a ColumnSchema instance", c2) |
| 24 | + } |
| 25 | + |
| 26 | + val := _compareString(c.row["table_name"], c2.row["table_name"]) |
| 27 | + if val != 0 { |
| 28 | + // Table name differed so return that value |
| 29 | + return val |
| 30 | + } |
| 31 | + |
| 32 | + // Table name was the same so compare column name |
| 33 | + val = _compareString(c.row["column_name"], c2.row["column_name"]) |
| 34 | + return val |
| 35 | +} |
| 36 | + |
| 37 | +// Return SQL to add the column |
| 38 | +func (c ColumnSchema) Add() { |
| 39 | + if c.row["data_type"] == "character varying" { |
| 40 | + fmt.Printf("ALTER TABLE %s ADD COLUMN %s %s(%s)", c.row["table_name"], c.row["column_name"], c.row["data_type"], c.row["character_maximum_length"]) |
| 41 | + } else { |
| 42 | + fmt.Printf("ALTER TABLE %s ADD COLUMN %s %s", c.row["table_name"], c.row["column_name"], c.row["data_type"]) |
| 43 | + } |
| 44 | + |
| 45 | + if c.row["is_nullable"] == "NO" { |
| 46 | + fmt.Printf(" NOT NULL") |
| 47 | + } |
| 48 | + if c.row["column_default"] != "null" { |
| 49 | + fmt.Printf(" DEFAULT %s", c.row["column_default"]) |
| 50 | + } |
| 51 | + fmt.Printf(";\n") |
| 52 | +} |
| 53 | + |
| 54 | +// Return SQL to drop the column |
| 55 | +func (c ColumnSchema) Drop() { |
| 56 | + // if dropping column |
| 57 | + fmt.Printf("ALTER TABLE %s DROP COLUMN %s;\n", c.row["table_name"], c.row["column_name"]) |
| 58 | +} |
| 59 | + |
| 60 | +// Handle the case where the table and column match, but the details do not |
| 61 | +func (c ColumnSchema) Change(obj interface{}) { |
| 62 | + c2, ok := obj.(*ColumnSchema) |
| 63 | + if !ok { |
| 64 | + fmt.Println("Error!!!, change needs a ColumnSchema instance", c2) |
| 65 | + } |
| 66 | +// fmt.Printf("Changes? ") |
| 67 | +// // if changing type |
| 68 | +// if c.row["data_type"] == "character varying" { |
| 69 | +// // varchar needs a length specified |
| 70 | +// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s TYPE %s(%s);\n", c.row["table_name"], c.row["column_name"], c.row["data_type"], c.row["character_maximum_length"]) |
| 71 | +// } else { |
| 72 | +// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s TYPE %s;\n", c.row["table_name"], c.row["column_name"], c.row["data_type"]) |
| 73 | +// } |
| 74 | +// |
| 75 | +// // if changing/adding default value |
| 76 | +// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;\n", c.row["table_name"], c.row["column_name"], c.row["column_default"]) |
| 77 | +// |
| 78 | +// // if dropping default value |
| 79 | +// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;\n", c.row["table_name"], c.row["column_name"]) |
| 80 | +// |
| 81 | +// // if adding not null |
| 82 | +// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;\n", c.row["table_name"], c.row["column_name"]) |
| 83 | +// |
| 84 | +// // if dropping not null |
| 85 | +// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;\n", c.row["table_name"], c.row["column_name"]) |
| 86 | +// return "Change" |
| 87 | +} |
0 commit comments