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 ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, pkeyColumn := range pkey.Columns {
|
||||||
for _, c := range cols {
|
for _, c := range cols {
|
||||||
if rgxAutoIncColumn.MatchString(c.Default) &&
|
if c.Name != pkeyColumn {
|
||||||
c.IsNullable == false &&
|
continue
|
||||||
(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) {
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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",
|
|
||||||
},
|
},
|
||||||
Name: "",
|
Columns: []dbdrivers.Column{
|
||||||
}
|
dbdrivers.Column{
|
||||||
|
Name: "one",
|
||||||
cols = []dbdrivers.Column{
|
Type: "int32",
|
||||||
{
|
|
||||||
Name: "thing",
|
|
||||||
IsNullable: true,
|
|
||||||
Type: "int64",
|
|
||||||
Default: "nextval('abc'::regclass)",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "stuff",
|
|
||||||
IsNullable: false,
|
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",
|
Type: "string",
|
||||||
Default: "nextval('abc'::regclass)",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "other",
|
|
||||||
IsNullable: false,
|
IsNullable: false,
|
||||||
Type: "int64",
|
Default: `nextval('abc'::regclass)`,
|
||||||
Default: "nextval",
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"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)
|
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue