Fix strmangle's AutoIncPrimaryKey function.

- It's test was also a dead unicorn of unhappiness.
This commit is contained in:
Aaron L 2016-06-19 17:17:09 -07:00
parent c07d21cffc
commit 30ece150e7
2 changed files with 92 additions and 53 deletions

View file

@ -302,15 +302,18 @@ func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) stri
return "" return ""
} }
for _, c := range cols { for _, pkeyColumn := range pkey.Columns {
if rgxAutoIncColumn.MatchString(c.Default) && for _, c := range cols {
c.IsNullable == false && if c.Name != pkeyColumn {
(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) { continue
for _, p := range pkey.Columns {
if c.Name == p {
return p
}
} }
if !rgxAutoIncColumn.MatchString(c.Default) || c.IsNullable ||
!(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) {
continue
}
return pkeyColumn
} }
} }

View file

@ -90,57 +90,93 @@ func TestCamelCaseCommaList(t *testing.T) {
func TestAutoIncPrimaryKey(t *testing.T) { func TestAutoIncPrimaryKey(t *testing.T) {
t.Parallel() t.Parallel()
var pkey *dbdrivers.PrimaryKey tests := map[string]struct {
var cols []dbdrivers.Column Expect string
Pkey *dbdrivers.PrimaryKey
r := AutoIncPrimaryKey(cols, pkey) Columns []dbdrivers.Column
if r != "" { }{
t.Errorf("Expected empty string, got %s", r) "easycase": {
} Expect: "one",
Pkey: &dbdrivers.PrimaryKey{
pkey = &dbdrivers.PrimaryKey{ Name: "pkey",
Columns: []string{ Columns: []string{"one"},
"col1", "auto", },
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "int32",
IsNullable: false,
Default: `nextval('abc'::regclass)`,
},
},
}, },
Name: "", "missingcase": {
} Expect: "",
Pkey: &dbdrivers.PrimaryKey{
cols = []dbdrivers.Column{ Name: "pkey",
{ Columns: []string{"two"},
Name: "thing", },
IsNullable: true, Columns: []dbdrivers.Column{
Type: "int64", dbdrivers.Column{
Default: "nextval('abc'::regclass)", Name: "one",
Type: "int32",
IsNullable: false,
Default: `nextval('abc'::regclass)`,
},
},
}, },
{ "wrongtype": {
Name: "stuff", Expect: "",
IsNullable: false, Pkey: &dbdrivers.PrimaryKey{
Type: "string", Name: "pkey",
Default: "nextval('abc'::regclass)", Columns: []string{"one"},
},
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "string",
IsNullable: false,
Default: `nextval('abc'::regclass)`,
},
},
}, },
{ "nodefault": {
Name: "other", Expect: "",
IsNullable: false, Pkey: &dbdrivers.PrimaryKey{
Type: "int64", Name: "pkey",
Default: "nextval", 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) for testName, test := range tests {
if r != "" { primaryKey := AutoIncPrimaryKey(test.Columns, test.Pkey)
t.Errorf("Expected empty string, got %s", r) 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)
} }
} }