Skip to content

Commit

Permalink
fix: don't automatically set pk, nullzero, and autoincrement options
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Feb 22, 2022
1 parent a4ec556 commit 519a0df
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 48 deletions.
5 changes: 5 additions & 0 deletions bun.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func SetLogger(logger internal.Logging) {
internal.Logger = logger
}

// SetWarnLogger overwriters default Bun warn logger.
func SetWarnLogger(logger internal.Logging) {
internal.Warn = logger
}

//------------------------------------------------------------------------------

type InValues struct {
Expand Down
22 changes: 11 additions & 11 deletions internal/dbtest/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func testRunInTx(t *testing.T, db *bun.DB) {

func testJSONSpecialChars(t *testing.T, db *bun.DB) {
type Model struct {
ID int
ID int `bun:",pk"`
Attrs map[string]interface{} `bun:"type:json"`
}

Expand Down Expand Up @@ -718,7 +718,7 @@ func testJSONSpecialChars(t *testing.T, db *bun.DB) {

func testJSONInterface(t *testing.T, db *bun.DB) {
type Model struct {
ID int
ID int `bun:",pk"`
Value interface{} `bun:"type:json"`
}

Expand Down Expand Up @@ -762,7 +762,7 @@ func (v *JSONValue) Value() (driver.Value, error) {

func testJSONValuer(t *testing.T, db *bun.DB) {
type Model struct {
ID int
ID int `bun:",pk"`
Value JSONValue `bun:"type:json"`
}

Expand Down Expand Up @@ -794,12 +794,12 @@ func testSelectBool(t *testing.T, db *bun.DB) {

func testFKViolation(t *testing.T, db *bun.DB) {
type Deck struct {
ID int
ID int `bun:",pk"`
UserID int
}

type User struct {
ID int
ID int `bun:",pk"`
}

if db.Dialect().Name() == dialect.SQLite {
Expand Down Expand Up @@ -959,7 +959,7 @@ func testInterfaceJSON(t *testing.T, db *bun.DB) {

func testScanRawMessage(t *testing.T, db *bun.DB) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Value json.RawMessage
}

Expand Down Expand Up @@ -988,7 +988,7 @@ func testScanRawMessage(t *testing.T, db *bun.DB) {

func testPointers(t *testing.T, db *bun.DB) {
type Model struct {
ID *int64 `bun:",default:0"`
ID *int64 `bun:",pk,default:0"`
Str *string
}

Expand Down Expand Up @@ -1046,7 +1046,7 @@ func testModelNonPointer(t *testing.T, db *bun.DB) {

func testBinaryData(t *testing.T, db *bun.DB) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Data []byte
}

Expand All @@ -1066,7 +1066,7 @@ func testBinaryData(t *testing.T, db *bun.DB) {

func testUpsert(t *testing.T, db *bun.DB) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Str string
}

Expand Down Expand Up @@ -1103,7 +1103,7 @@ func testMultiUpdate(t *testing.T, db *bun.DB) {
}

type Model struct {
ID int64
ID int64 `bun:",pk"`
Str string
}

Expand Down Expand Up @@ -1131,7 +1131,7 @@ func testMultiUpdate(t *testing.T, db *bun.DB) {

func testTxScanAndCount(t *testing.T, db *bun.DB) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Str string
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dbtest/model_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func testModelHook(t *testing.T, dbName string, db *bun.DB) {
}

type ModelHookTest struct {
ID int
ID int `bun:",pk"`
Value string
}

Expand Down
16 changes: 8 additions & 8 deletions internal/dbtest/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func testRelationBelongsToSelf(t *testing.T, db *bun.DB) {
type Model struct {
bun.BaseModel `bun:"alias:m"`

ID int64
ID int64 `bun:",pk"`
ModelID int64
Model *Model `bun:"rel:belongs-to"`
}
Expand All @@ -374,13 +374,13 @@ func testRelationBelongsToSelf(t *testing.T, db *bun.DB) {

func testM2MRelationExcludeColumn(t *testing.T, db *bun.DB) {
type Item struct {
ID int64
ID int64 `bun:",pk"`
CreatedAt time.Time `bun:",notnull,nullzero"`
UpdatedAt time.Time `bun:",notnull,nullzero"`
}

type Order struct {
ID int64
ID int64 `bun:",pk"`
Items []Item `bun:"m2m:order_to_items"`
}

Expand Down Expand Up @@ -430,7 +430,7 @@ func testM2MRelationExcludeColumn(t *testing.T, db *bun.DB) {
}

type Genre struct {
ID int
ID int `bun:",pk"`
Name string
Rating int `bun:",scanonly"`

Expand All @@ -445,12 +445,12 @@ func (g Genre) String() string {
}

type Image struct {
ID int
ID int `bun:",pk"`
Path string
}

type Author struct {
ID int
ID int `bun:",pk"`
Name string `bun:",unique"`
Books []*Book `bun:"rel:has-many"`

Expand Down Expand Up @@ -480,7 +480,7 @@ type BookGenre struct {
}

type Book struct {
ID int
ID int `bun:",pk"`
Title string
AuthorID int
Author Author `bun:"rel:belongs-to"`
Expand Down Expand Up @@ -516,7 +516,7 @@ type BookWithCommentCount struct {
type Translation struct {
bun.BaseModel `bun:"alias:tr"`

ID int
ID int `bun:",pk"`
BookID int `bun:"unique:book_id_lang"`
Book *Book `bun:"rel:belongs-to"`
Lang string `bun:"unique:book_id_lang"`
Expand Down
16 changes: 8 additions & 8 deletions internal/dbtest/pg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

func TestPGArray(t *testing.T) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Array1 []string `bun:",array"`
Array2 *[]string `bun:",array"`
Array3 *[]string `bun:",array"`
Expand Down Expand Up @@ -96,7 +96,7 @@ func (h Hash) Value() (driver.Value, error) {

func TestPGArrayValuer(t *testing.T) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Array []Hash `bun:",array"`
}

Expand All @@ -122,14 +122,14 @@ func TestPGArrayValuer(t *testing.T) {
type Recipe struct {
bun.BaseModel `bun:"?tenant.recipes"`

ID int
ID int `bun:",pk"`
Ingredients []*Ingredient `bun:"m2m:?tenant.ingredients_recipes"`
}

type Ingredient struct {
bun.BaseModel `bun:"?tenant.ingredients"`

ID int
ID int `bun:",pk"`
Recipes []*Recipe `bun:"m2m:?tenant.ingredients_recipes"`
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func TestPGMultiTenant(t *testing.T) {

func TestPGInsertNoRows(t *testing.T) {
type User struct {
ID int64
ID int64 `bun:",pk"`
}

db := pg(t)
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestPGTransaction(t *testing.T) {
db := pg(t)

type Model struct {
ID int64
ID int64 `bun:",pk"`
}

_, err := db.NewDropTable().Model((*Model)(nil)).IfExists().Exec(ctx)
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestPGScanWithoutResult(t *testing.T) {
defer db.Close()

type Model struct {
ID int64
ID int64 `bun:",pk"`
}

err := db.ResetModel(ctx, (*Model)(nil))
Expand Down Expand Up @@ -428,7 +428,7 @@ func TestPGTimetz(t *testing.T) {

func TestPGOnConflictDoUpdate(t *testing.T) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
UpdatedAt time.Time
}

Expand Down
24 changes: 12 additions & 12 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func init() {

func TestQuery(t *testing.T) {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Str string
}

type User struct {
ID int64
ID int64 `bun:",pk"`
Name string
}

type Story struct {
ID int64
ID int64 `bun:",pk"`
Name string
UserID int64
User *User `bun:"rel:belongs-to"`
Expand All @@ -40,14 +40,14 @@ func TestQuery(t *testing.T) {
type SoftDelete1 struct {
bun.BaseModel `bun:"soft_deletes,alias:soft_delete"`

ID int64
ID int64 `bun:",pk"`
DeletedAt time.Time `bun:",soft_delete"`
}

type SoftDelete2 struct {
bun.BaseModel `bun:"soft_deletes,alias:soft_delete"`

ID int64
ID int64 `bun:",pk"`
DeletedAt time.Time `bun:",soft_delete,allowzero"`
}

Expand Down Expand Up @@ -234,7 +234,7 @@ func TestQuery(t *testing.T) {
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID uint64
ID uint64 `bun:",pk"`
Struct struct{}
Map map[string]interface{}
Slice []string
Expand Down Expand Up @@ -499,7 +499,7 @@ func TestQuery(t *testing.T) {
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Str1 string
Str2 string
}
Expand Down Expand Up @@ -555,7 +555,7 @@ func TestQuery(t *testing.T) {
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64 `bun:",allowzero"`
ID int64 `bun:",pk,allowzero"`
}
return db.NewInsert().Model(new(Model))
},
Expand All @@ -567,7 +567,7 @@ func TestQuery(t *testing.T) {
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Time time.Time
}
return db.NewInsert().Model(&Model{ID: 123, Time: time.Unix(0, 0)})
Expand Down Expand Up @@ -635,7 +635,7 @@ func TestQuery(t *testing.T) {
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64
ID int64 `bun:",pk"`
Int int64 `bun:",nullzero"`
Uint uint64 `bun:",nullzero"`
Str string `bun:",nullzero"`
Expand All @@ -652,7 +652,7 @@ func TestQuery(t *testing.T) {
func(db *bun.DB) schema.QueryAppender {
type ID string
type Model struct {
ID
ID `bun:",pk"`
}
return db.NewInsert().Model(&Model{ID: ID("embed")})
},
Expand All @@ -677,7 +677,7 @@ func TestQuery(t *testing.T) {
Bar string
}
type Model struct {
ID int64
ID int64 `bun:",pk"`
Slice []Item `bun:",nullzero"`
}
return db.NewInsert().Model(&Model{ID: 123, Slice: make([]Item, 0)})
Expand Down
4 changes: 2 additions & 2 deletions internal/dbtest/soft_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestSoftDelete(t *testing.T) {
}

type Video struct {
ID int64
ID int64 `bun:",pk"`
Name string
DeletedAt time.Time `bun:",soft_delete"`
DeletedAt time.Time `bun:",soft_delete,nullzero"`
}

func testSoftDeleteNilModel(t *testing.T, db *bun.DB) {
Expand Down
4 changes: 2 additions & 2 deletions internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
)

var Warn = log.New(os.Stderr, "WARN: bun: ", log.LstdFlags)
var Warn Logging = log.New(os.Stderr, "WARN: bun: ", log.LstdFlags)

var Deprecated = log.New(os.Stderr, "DEPRECATED: bun: ", log.LstdFlags)
var Deprecated Logging = log.New(os.Stderr, "DEPRECATED: bun: ", log.LstdFlags)

type Logging interface {
Printf(format string, v ...interface{})
Expand Down
2 changes: 1 addition & 1 deletion migrate/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
type Migration struct {
bun.BaseModel

ID int64
ID int64 `bun:",pk"`
Name string
GroupID int64
MigratedAt time.Time `bun:",notnull,nullzero,default:current_timestamp"`
Expand Down
2 changes: 1 addition & 1 deletion migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func (m *Migrator) validate() error {
//------------------------------------------------------------------------------

type migrationLock struct {
ID int64
ID int64 `bun:",pk"`
TableName string `bun:",unique"`
}

Expand Down
Loading

0 comments on commit 519a0df

Please sign in to comment.