Skip to content

Commit 6cb64e4

Browse files
author
Miguel Molina
committed
fix debug for inserts and deletes, return error for models with no fields
1 parent ee9b487 commit 6cb64e4

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

common_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,44 @@ func (m *rel) SetRelationship(field string, record interface{}) error {
182182
return fmt.Errorf("kallax: no relationship found for field %s", field)
183183
}
184184

185+
type onlyPkModel struct {
186+
Model
187+
ID int64 `pk:"autoincr"`
188+
}
189+
190+
func newOnlyPkModel() *onlyPkModel {
191+
m := new(onlyPkModel)
192+
return m
193+
}
194+
195+
func (m *onlyPkModel) Value(col string) (interface{}, error) {
196+
switch col {
197+
case "id":
198+
return m.ID, nil
199+
}
200+
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
201+
}
202+
203+
func (m *onlyPkModel) ColumnAddress(col string) (interface{}, error) {
204+
switch col {
205+
case "id":
206+
return &m.ID, nil
207+
}
208+
return nil, fmt.Errorf("kallax: column does not exist: %s", col)
209+
}
210+
211+
func (m *onlyPkModel) NewRelationshipRecord(field string) (Record, error) {
212+
return nil, fmt.Errorf("kallax: no relationship found for field %s", field)
213+
}
214+
215+
func (m *onlyPkModel) SetRelationship(field string, record interface{}) error {
216+
return fmt.Errorf("kallax: no relationship found for field %s", field)
217+
}
218+
219+
func (m *onlyPkModel) GetID() Identifier {
220+
return (*NumericID)(&m.ID)
221+
}
222+
185223
var ModelSchema = NewBaseSchema(
186224
"model",
187225
"__model",
@@ -215,6 +253,18 @@ var RelSchema = NewBaseSchema(
215253
f("foo"),
216254
)
217255

256+
var onlyPkModelSchema = NewBaseSchema(
257+
"model",
258+
"__model",
259+
f("id"),
260+
nil,
261+
func() Record {
262+
return new(onlyPkModel)
263+
},
264+
true,
265+
f("id"),
266+
)
267+
218268
func f(name string) SchemaField {
219269
return NewSchemaField(name)
220270
}

generator/cli/kallax/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"gopkg.in/urfave/cli.v1"
99
)
1010

11-
const version = "1.2.1"
11+
const version = "1.2.2"
1212

1313
func main() {
1414
newApp().Run(os.Args)

store.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var (
3232
// neither an autoincrement primary key nor implements the IDSetter
3333
// interface.
3434
ErrCantSetID = errors.New("kallax: model does not have an auto incrementable primary key, it needs to implement IDSetter interface")
35+
// ErrNoColumns is an error returned when the user tries to insert a model
36+
// with no other columns than the autoincrementable primary key.
37+
ErrNoColumns = errors.New("kallax: your model does not have any column besides its autoincrementable primary key and cannot be inserted")
3538
)
3639

3740
// GenericStorer is a type that contains a generic store and has methods to
@@ -123,10 +126,11 @@ func (s *Store) Debug() *Store {
123126
// DebugWith returns a new store that will print all SQL statements using the
124127
// given logger function.
125128
func (s *Store) DebugWith(logger LoggerFunc) *Store {
129+
proxy := &debugProxy{logger, s.proxy}
126130
return &Store{
127-
builder: s.builder,
131+
builder: s.builder.RunWith(proxy),
128132
db: s.db,
129-
proxy: &debugProxy{logger, s.proxy},
133+
proxy: proxy,
130134
}
131135
}
132136

@@ -145,6 +149,10 @@ func (s *Store) Insert(schema Schema, record Record) error {
145149
cols = cols[1:]
146150
}
147151

152+
if len(cols) == 0 {
153+
return ErrNoColumns
154+
}
155+
148156
values, err := RecordValues(record, cols...)
149157
if err != nil {
150158
return err

store_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func (s *StoreSuite) TestInsert_IDEmpty() {
6161
s.False(m.GetID().IsEmpty())
6262
}
6363

64+
func (s *StoreSuite) TestInsert_NoColumns() {
65+
var m = new(onlyPkModel)
66+
s.Equal(ErrNoColumns, s.store.Insert(onlyPkModelSchema, m))
67+
}
68+
6469
func (s *StoreSuite) TestUpdate() {
6570
var m = newModel("a", "[email protected]", 1)
6671
s.NoError(s.store.Insert(ModelSchema, m))

0 commit comments

Comments
 (0)