Skip to content

Commit 2ac5ab1

Browse files
committed
column compare and usage output improvements
1 parent 8e89bf9 commit 2ac5ab1

2 files changed

Lines changed: 9 additions & 37 deletions

File tree

column.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c ColumnSchema) Drop() {
6868
func (c ColumnSchema) Change(obj interface{}) {
6969
c2, ok := obj.(*ColumnSchema)
7070
if !ok {
71-
fmt.Println("Error!!!, change needs a ColumnSchema instance", c2)
71+
fmt.Println("Error!!!, ColumnSchema.Change(obj) needs a ColumnSchema instance", c2)
7272
}
7373

7474
// Detect column type change (mostly varchar length, or number size increase) (integer to/from bigint is OK)
@@ -94,39 +94,17 @@ func (c ColumnSchema) Change(obj interface{}) {
9494
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;\n", c.row["table_name"], c.row["column_name"])
9595
}
9696
} else if c.row["column_default"] != c2.row["column_default"] {
97-
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DEFAULT %s;\n", c.row["table_name"], c.row["column_name"], c.row["column_default"])
97+
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;\n", c.row["table_name"], c.row["column_name"], c.row["column_default"])
9898
}
9999

100+
// TODO Detect not-null and nullable change
100101
if c.row["is_nullable"] != c2.row["is_nullable"] {
101102
if c.row["is_nullable"] == "YES" {
102-
fmt.Printf("ALTER TABLE %s ALTER COLUMN DROP NOT NULL")
103+
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;\n", c.row["table_name"], c.row["column_name"])
103104
} else {
104-
fmt.Printf("ALTER TABLE %s ALTER COLUMN SET NOT NULL")
105+
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;\n", c.row["table_name"], c.row["column_name"])
105106
}
106107
}
107-
108-
// TODO Detect not-null and nullable change
109-
110-
// // if changing type
111-
// if c.row["data_type"] == "character varying" {
112-
// // varchar needs a length specified
113-
// 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"])
114-
// } else {
115-
// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s TYPE %s;\n", c.row["table_name"], c.row["column_name"], c.row["data_type"])
116-
// }
117-
//
118-
// // if changing/adding default value
119-
// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;\n", c.row["table_name"], c.row["column_name"], c.row["column_default"])
120-
//
121-
// // if dropping default value
122-
// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;\n", c.row["table_name"], c.row["column_name"])
123-
//
124-
// // if adding not null
125-
// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;\n", c.row["table_name"], c.row["column_name"])
126-
//
127-
// // if dropping not null
128-
// fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;\n", c.row["table_name"], c.row["column_name"])
129-
// return "Change"
130108
}
131109

132110
/*

pgdiff.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ func doDiff(db1 Schema, db2 Schema) {
108108
}
109109

110110
func usage() {
111-
fmt.Fprintf(os.Stderr, "usage: %s [database flags] <genType> <tableName> <whereClause> \n", os.Args[0])
111+
fmt.Fprintf(os.Stderr, "usage: %s [database flags] <schemaType> \n", os.Args[0])
112112
fmt.Fprintln(os.Stderr, `
113-
Copies table data as either INSERT or UPDATE statements.
113+
Compares the schema between two PostgreSQL databases and generates alter statements
114+
to be *manually* run against the second database.
114115
115116
[database flags]: (optional)
116117
-U1 : postgres user (matches psql flag)
@@ -124,14 +125,7 @@ Copies table data as either INSERT or UPDATE statements.
124125
-d2 : database name (matches psql flag)
125126
-pw2 : password for the postgres user (otherwise you'll be prompted)
126127
127-
<genType> : type of SQL to generate: insert, update
128-
129-
Database connection information can be specified in two ways:
130-
* Environment variables
131-
* Program flags (overrides environment variables. See above)
132-
* ~/.pgpass file (for the password)
133-
* Note that if password is not specified, you will be prompted.
134-
128+
<schemaTpe> : type of schema to check: TABLE, COLUMN, FOREIGN_KEY (soon: CONSTRAINT, ROLE)
135129
`)
136130

137131
os.Exit(2)

0 commit comments

Comments
 (0)