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 { if !cached {
wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist) 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", cache.query = fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.IndexPlaceholders}}1{{else}}0{{end}}, wl), 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) values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
if boil.DebugMode { if boil.DebugMode {

View file

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

View file

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

View file

@ -5,6 +5,10 @@
func test{{$tableNamePlural}}Update(t *testing.T) { func test{{$tableNamePlural}}Update(t *testing.T) {
t.Parallel() t.Parallel()
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
t.Skip("Skipping table with only primary key columns")
}
seed := randomize.NewSeed() seed := randomize.NewSeed()
var err error var err error
{{$varNameSingular}} := &{{$tableNameSingular}}{} {{$varNameSingular}} := &{{$tableNameSingular}}{}
@ -31,24 +35,18 @@ func test{{$tableNamePlural}}Update(t *testing.T) {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
} }
// If table only contains primary key columns, we need to pass if err = {{$varNameSingular}}.Update(tx); err != nil {
// them into a whitelist to get a valid test result, t.Error(err)
// 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) { func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
t.Parallel() t.Parallel()
if len({{$varNameSingular}}Columns) == len({{$varNameSingular}}PrimaryKeyColumns) {
t.Skip("Skipping table with only primary key columns")
}
seed := randomize.NewSeed() seed := randomize.NewSeed()
var err error var err error
{{$varNameSingular}} := &{{$tableNameSingular}}{} {{$varNameSingular}} := &{{$tableNameSingular}}{}

View file

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