11package main
22
33import "fmt"
4+ import "strconv"
45import "database/sql"
56import "github.com/joncrlsn/pgutil"
67
@@ -80,8 +81,16 @@ func (c ColumnSchema) Change(obj interface{}) {
8081 if c .row ["data_type" ] == c2 .row ["data_type" ] {
8182 if c .row ["data_type" ] == "character varying" {
8283 if c .row ["character_maximum_length" ] != c2 .row ["character_maximum_length" ] {
83- if c .row ["character_maximum_length" ] < c2 .row ["character_maximum_length" ] {
84- fmt .Println ("-- WARNING: The next statement will shorten a character varying column." )
84+ max1 := c .row ["character_maximum_length" ]
85+ max2 := c2 .row ["character_maximum_length" ]
86+ if max1 != "null" && max2 != "null" {
87+ cMax , err1 := strconv .Atoi (max1 )
88+ check ("converting string to int" , err1 )
89+ c2Max , err2 := strconv .Atoi (max2 )
90+ check ("converting string to int" , err2 )
91+ if cMax < c2Max {
92+ fmt .Println ("-- WARNING: The next statement will shorten a character varying column." )
93+ }
8594 }
8695 maxLength := c .row ["character_maximum_length" ]
8796 if maxLength == "null" {
@@ -95,7 +104,8 @@ func (c ColumnSchema) Change(obj interface{}) {
95104
96105 // TODO: Code and test a column change from integer to bigint
97106 if c .row ["data_type" ] != c2 .row ["data_type" ] {
98- fmt .Printf ("-- WARNING: This program does not (yet) handle type changes (%s to %s).\n " , c2 .row ["data_type" ], c .row ["data_type" ])
107+ fmt .Printf ("-- WARNING: This type change may not work well: (%s to %s).\n " , c2 .row ["data_type" ], c .row ["data_type" ])
108+ fmt .Printf ("ALTER TABLE %s ALTER COLUMN %s TYPE %s;\n " , c .row ["table_name" ], c .row ["column_name" ], c .row ["data_type" ])
99109 }
100110
101111 // Detect column default change (or added, dropped)
0 commit comments