Skip to content

Commit 5917d9f

Browse files
committed
Some minor and some major improvements
Former-commit-id: 6ce7a47
1 parent 3366600 commit 5917d9f

9 files changed

Lines changed: 148 additions & 261 deletions

File tree

column.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import "strconv"
66
import "strings"
77
import "database/sql"
88
import "github.com/joncrlsn/pgutil"
9+
import "github.com/joncrlsn/misc"
910

1011
// ==================================
11-
// ColumnRows definition
12+
// ColumnRows definition (a sortable slice of string maps)
1213
// ==================================
1314
type ColumnRows []map[string]string
1415

@@ -17,15 +18,13 @@ func (slice ColumnRows) Len() int {
1718
}
1819

1920
func (slice ColumnRows) Less(i, j int) bool {
20-
//fmt.Printf("--Less %s:%s with %s:%s", slice[i]["table_name"], slice[i]["column_name"], slice[j]["table_name"], slice[j]["column_name"])
21-
if slice[i]["table_name"] == slice[j]["table_name"] {
22-
return slice[i]["column_name"] < slice[j]["column_name"]
21+
if slice[i]["table_name"] != slice[j]["table_name"] {
22+
return slice[i]["table_name"] < slice[j]["table_name"]
2323
}
24-
return slice[i]["table_name"] < slice[j]["table_name"]
24+
return slice[i]["column_name"] < slice[j]["column_name"]
2525
}
2626

2727
func (slice ColumnRows) Swap(i, j int) {
28-
fmt.Printf("--Swapping %d/%s:%s with %d/%s:%s \n", i, slice[i]["table_name"], slice[i]["column_name"], j, slice[j]["table_name"], slice[j]["column_name"])
2928
slice[i], slice[j] = slice[j], slice[i]
3029
}
3130

@@ -63,17 +62,17 @@ func (c *ColumnSchema) NextRow() bool {
6362
func (c *ColumnSchema) Compare(obj interface{}) int {
6463
c2, ok := obj.(*ColumnSchema)
6564
if !ok {
66-
fmt.Println("Error!!!, change needs a ColumnSchema instance", c2)
65+
fmt.Println("Error!!!, Compare needs a ColumnSchema instance", c2)
6766
}
6867

69-
val := _compareString(c.get("table_name"), c2.get("table_name"))
68+
val := misc.CompareStrings(c.get("table_name"), c2.get("table_name"))
7069
if val != 0 {
7170
// Table name differed so return that value
7271
return val
7372
}
7473

7574
// Table name was the same so compare column name
76-
val = _compareString(c.get("column_name"), c2.get("column_name"))
75+
val = misc.CompareStrings(c.get("column_name"), c2.get("column_name"))
7776
return val
7877
}
7978

@@ -132,7 +131,7 @@ func (c *ColumnSchema) Change(obj interface{}) {
132131
}
133132
}
134133

135-
// TODO: Code and test a column change from integer to bigint
134+
// Code and test a column change from integer to bigint
136135
if c.get("data_type") != c2.get("data_type") {
137136
fmt.Printf("-- WARNING: This type change may not work well: (%s to %s).\n", c2.get("data_type"), c.get("data_type"))
138137
if strings.HasPrefix(c.get("data_type"), "character") {
@@ -155,7 +154,7 @@ func (c *ColumnSchema) Change(obj interface{}) {
155154
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;\n", c.get("table_name"), c.get("column_name"), c.get("column_default"))
156155
}
157156

158-
// TODO Detect not-null and nullable change
157+
// Detect not-null and nullable change
159158
if c.get("is_nullable") != c2.get("is_nullable") {
160159
if c.get("is_nullable") == "YES" {
161160
fmt.Printf("ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;\n", c.get("table_name"), c.get("column_name"))

foreignkey.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "sort"
44
import "fmt"
55
import "database/sql"
66
import "github.com/joncrlsn/pgutil"
7+
import "github.com/joncrlsn/misc"
78

89
// ==================================
910
// ForeignKeyRows definition
@@ -15,15 +16,16 @@ func (slice ForeignKeyRows) Len() int {
1516
}
1617

1718
func (slice ForeignKeyRows) Less(i, j int) bool {
18-
//fmt.Printf("--Less %s:%s with %s:%s", slice[i]["table_name"], slice[i]["column_name"], slice[j]["table_name"], slice[j]["column_name"])
19-
if slice[i]["table_name"] == slice[j]["table_name"] {
19+
if slice[i]["table_name"] != slice[j]["table_name"] {
20+
return slice[i]["table_name"] < slice[j]["table_name"]
21+
}
22+
if slice[i]["constraint_def"] != slice[j]["constraint_def"] {
2023
return slice[i]["constraint_def"] < slice[j]["constraint_def"]
2124
}
2225
return slice[i]["table_name"] < slice[j]["table_name"]
2326
}
2427

2528
func (slice ForeignKeyRows) Swap(i, j int) {
26-
//fmt.Printf("--Swapping %d/%s:%s with %d/%s:%s \n", i, slice[i]["table_name"], slice[i]["index_name"], j, slice[j]["table_name"], slice[j]["index_name"])
2729
slice[i], slice[j] = slice[j], slice[i]
2830
}
2931

@@ -69,17 +71,17 @@ func (c *ForeignKeySchema) NextRow() bool {
6971
func (c *ForeignKeySchema) Compare(obj interface{}) int {
7072
c2, ok := obj.(*ForeignKeySchema)
7173
if !ok {
72-
fmt.Println("Error!!!, Change(...) needs a ForeignKeySchema instance", c2)
74+
fmt.Println("Error!!!, Compare(obj) needs a ForeignKeySchema instance", c2)
7375
return +999
7476
}
7577

7678
//fmt.Printf("Comparing %s with %s", c.get("table_name"), c2.get("table_name"))
77-
val := _compareString(c.get("table_name"), c2.get("table_name"))
79+
val := misc.CompareStrings(c.get("table_name"), c2.get("table_name"))
7880
if val != 0 {
7981
return val
8082
}
8183

82-
val = _compareString(c.get("constraint_def"), c2.get("constraint_def"))
84+
val = misc.CompareStrings(c.get("constraint_def"), c2.get("constraint_def"))
8385
return val
8486
}
8587

@@ -97,7 +99,7 @@ func (c ForeignKeySchema) Drop() {
9799
func (c *ForeignKeySchema) Change(obj interface{}) {
98100
c2, ok := obj.(*ForeignKeySchema)
99101
if !ok {
100-
fmt.Println("Error!!!, change needs a ForeignKeySchema instance", c2)
102+
fmt.Println("Error!!!, Change(obj) needs a ForeignKeySchema instance", c2)
101103
}
102104
// There is no "changing" a foreign key. It either gets created or dropped (or left as-is).
103105
}

grant.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,6 @@ var permMap map[string]string = map[string]string{
2525
"T": "TEMPORARY",
2626
}
2727

28-
// ==================================
29-
// Functions
30-
// ==================================
31-
32-
//
33-
// RoleAcls (a sortable slice of RoleAcl instances)
34-
//
35-
type RoleAcls []RoleAcl
36-
37-
func (slice RoleAcls) Len() int {
38-
return len(slice)
39-
}
40-
41-
func (slice RoleAcls) Less(i, j int) bool {
42-
return slice[i].role < slice[j].role
43-
}
44-
45-
func (slice RoleAcls) Swap(i, j int) {
46-
slice[i], slice[j] = slice[j], slice[i]
47-
}
48-
49-
func (slice RoleAcls) get(role string) RoleAcl {
50-
for _, roleAcl := range slice {
51-
if roleAcl.role == role {
52-
return roleAcl
53-
}
54-
}
55-
return RoleAcl{role: "", grants: []string{}}
56-
}
57-
58-
//
59-
// RoleAcl
60-
//
61-
type RoleAcl struct {
62-
role string
63-
grants []string
64-
}
65-
6628
/*
6729
parseGrants converts an ACL (access control list) line into a role and a slice of permission strings
6830
@@ -106,7 +68,8 @@ func parseGrants(acl string) (string, []string) {
10668
return role, permWords
10769
}
10870

109-
// parseAcl parses an ACL (access control list) string (e.g. 'c42=aur/postgres') into a role and some permissions
71+
// parseAcl parses an ACL (access control list) string (e.g. 'c42=aur/postgres') into a role and
72+
// a string made up of one-character permissions
11073
func parseAcl(acl string) (role string, perms string) {
11174
role, perms = "", ""
11275
matches := aclRegex.FindStringSubmatch(acl)

owner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "fmt"
44
import "database/sql"
55
import "sort"
66
import "github.com/joncrlsn/pgutil"
7+
import "github.com/joncrlsn/misc"
78

89
// ==================================
910
// OwnerRows definition (an array of string maps)
@@ -68,7 +69,7 @@ func (c *OwnerSchema) Compare(obj interface{}) int {
6869
return +999
6970
}
7071

71-
val := _compareString(c.get("relationship_name"), c2.get("relationship_name"))
72+
val := misc.CompareStrings(c.get("relationship_name"), c2.get("relationship_name"))
7273
return val
7374
}
7475

primarykey.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

privilege.md

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)