Fix strmangle's AutoIncPrimaryKey function.
- It's test was also a dead unicorn of unhappiness.
This commit is contained in:
parent
c07d21cffc
commit
30ece150e7
2 changed files with 92 additions and 53 deletions
|
@ -302,15 +302,18 @@ func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) stri
|
|||
return ""
|
||||
}
|
||||
|
||||
for _, pkeyColumn := range pkey.Columns {
|
||||
for _, c := range cols {
|
||||
if rgxAutoIncColumn.MatchString(c.Default) &&
|
||||
c.IsNullable == false &&
|
||||
(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) {
|
||||
for _, p := range pkey.Columns {
|
||||
if c.Name == p {
|
||||
return p
|
||||
if c.Name != pkeyColumn {
|
||||
continue
|
||||
}
|
||||
|
||||
if !rgxAutoIncColumn.MatchString(c.Default) || c.IsNullable ||
|
||||
!(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) {
|
||||
continue
|
||||
}
|
||||
|
||||
return pkeyColumn
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -90,57 +90,93 @@ func TestCamelCaseCommaList(t *testing.T) {
|
|||
func TestAutoIncPrimaryKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var pkey *dbdrivers.PrimaryKey
|
||||
var cols []dbdrivers.Column
|
||||
|
||||
r := AutoIncPrimaryKey(cols, pkey)
|
||||
if r != "" {
|
||||
t.Errorf("Expected empty string, got %s", r)
|
||||
}
|
||||
|
||||
pkey = &dbdrivers.PrimaryKey{
|
||||
Columns: []string{
|
||||
"col1", "auto",
|
||||
tests := map[string]struct {
|
||||
Expect string
|
||||
Pkey *dbdrivers.PrimaryKey
|
||||
Columns []dbdrivers.Column
|
||||
}{
|
||||
"easycase": {
|
||||
Expect: "one",
|
||||
Pkey: &dbdrivers.PrimaryKey{
|
||||
Name: "pkey",
|
||||
Columns: []string{"one"},
|
||||
},
|
||||
Name: "",
|
||||
}
|
||||
|
||||
cols = []dbdrivers.Column{
|
||||
{
|
||||
Name: "thing",
|
||||
IsNullable: true,
|
||||
Type: "int64",
|
||||
Default: "nextval('abc'::regclass)",
|
||||
},
|
||||
{
|
||||
Name: "stuff",
|
||||
Columns: []dbdrivers.Column{
|
||||
dbdrivers.Column{
|
||||
Name: "one",
|
||||
Type: "int32",
|
||||
IsNullable: false,
|
||||
Default: `nextval('abc'::regclass)`,
|
||||
},
|
||||
},
|
||||
},
|
||||
"missingcase": {
|
||||
Expect: "",
|
||||
Pkey: &dbdrivers.PrimaryKey{
|
||||
Name: "pkey",
|
||||
Columns: []string{"two"},
|
||||
},
|
||||
Columns: []dbdrivers.Column{
|
||||
dbdrivers.Column{
|
||||
Name: "one",
|
||||
Type: "int32",
|
||||
IsNullable: false,
|
||||
Default: `nextval('abc'::regclass)`,
|
||||
},
|
||||
},
|
||||
},
|
||||
"wrongtype": {
|
||||
Expect: "",
|
||||
Pkey: &dbdrivers.PrimaryKey{
|
||||
Name: "pkey",
|
||||
Columns: []string{"one"},
|
||||
},
|
||||
Columns: []dbdrivers.Column{
|
||||
dbdrivers.Column{
|
||||
Name: "one",
|
||||
Type: "string",
|
||||
Default: "nextval('abc'::regclass)",
|
||||
},
|
||||
{
|
||||
Name: "other",
|
||||
IsNullable: false,
|
||||
Type: "int64",
|
||||
Default: "nextval",
|
||||
Default: `nextval('abc'::regclass)`,
|
||||
},
|
||||
},
|
||||
},
|
||||
"nodefault": {
|
||||
Expect: "",
|
||||
Pkey: &dbdrivers.PrimaryKey{
|
||||
Name: "pkey",
|
||||
Columns: []string{"one"},
|
||||
},
|
||||
Columns: []dbdrivers.Column{
|
||||
dbdrivers.Column{
|
||||
Name: "one",
|
||||
Type: "string",
|
||||
IsNullable: false,
|
||||
Default: ``,
|
||||
},
|
||||
},
|
||||
},
|
||||
"nullable": {
|
||||
Expect: "",
|
||||
Pkey: &dbdrivers.PrimaryKey{
|
||||
Name: "pkey",
|
||||
Columns: []string{"one"},
|
||||
},
|
||||
Columns: []dbdrivers.Column{
|
||||
dbdrivers.Column{
|
||||
Name: "one",
|
||||
Type: "string",
|
||||
IsNullable: true,
|
||||
Default: `nextval('abc'::regclass)`,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
r = AutoIncPrimaryKey(cols, pkey)
|
||||
if r != "" {
|
||||
t.Errorf("Expected empty string, got %s", r)
|
||||
for testName, test := range tests {
|
||||
primaryKey := AutoIncPrimaryKey(test.Columns, test.Pkey)
|
||||
if primaryKey != test.Expect {
|
||||
t.Errorf("%s) wrong primary key, want: %q, got %q", testName, test.Expect, primaryKey)
|
||||
}
|
||||
|
||||
cols = append(cols, dbdrivers.Column{
|
||||
Name: "auto",
|
||||
IsNullable: false,
|
||||
Type: "int64",
|
||||
Default: "nextval('abc'::regclass)",
|
||||
})
|
||||
|
||||
r = AutoIncPrimaryKey(cols, pkey)
|
||||
if r != "auto" {
|
||||
t.Errorf("Expected empty string, got %s", r)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue