Primary key only tables are now handled adequately

This commit is contained in:
Patrick O'brien 2016-09-20 18:05:33 +10:00
parent d3f15c1953
commit 0a1a12cd0a
5 changed files with 21 additions and 16 deletions

View file

@ -52,6 +52,9 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string
if !cached {
wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist)
if len(wl) == 0 {
return errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist")
}
cache.query = fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, wl),
@ -63,10 +66,6 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string
}
}
if len(cache.valueMapping) == 0 {
return errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist")
}
values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
if boil.DebugMode {

View file

@ -85,6 +85,9 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName
{{$varNameSingular}}PrimaryKeyColumns,
updateColumns,
)
if len(update) == 0 {
return errors.New("{{.PkgName}}: unable to upsert {{.Table.Name}}, could not build update column list")
}
{{if ne .DriverName "mysql" -}}
var conflict []string

View file

@ -35,3 +35,4 @@ func (f *fKeyDestroyer) Read(b []byte) (int, error) {
return f.buf.Read(b)
}

View file

@ -5,6 +5,10 @@
func test{{$tableNamePlural}}Update(t *testing.T) {
t.Parallel()
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
t.Skip("Skipping table with only primary key columns")
}
seed := randomize.NewSeed()
var err error
{{$varNameSingular}} := &{{$tableNameSingular}}{}
@ -31,24 +35,18 @@ func test{{$tableNamePlural}}Update(t *testing.T) {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
// If table only contains primary key columns, we need to pass
// them into a whitelist to get a valid test result,
// otherwise the Update method will error because it will not be able to
// generate a whitelist (due to it excluding primary key columns).
if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) {
if err = {{$varNameSingular}}.Update(tx, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
t.Error(err)
}
} else {
if err = {{$varNameSingular}}.Update(tx); err != nil {
t.Error(err)
}
}
}
func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
t.Parallel()
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
t.Skip("Skipping table with only primary key columns")
}
seed := randomize.NewSeed()
var err error
{{$varNameSingular}} := &{{$tableNameSingular}}{}

View file

@ -5,6 +5,10 @@
func test{{$tableNamePlural}}Upsert(t *testing.T) {
t.Parallel()
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
t.Skip("Skipping table with only primary key columns")
}
seed := randomize.NewSeed()
var err error
// Attempt the INSERT side of an UPSERT