Skip to content

Commit

Permalink
feat: setError on attempt to set non-positive .Varchar()
Browse files Browse the repository at this point in the history
  • Loading branch information
bevzzz committed Dec 29, 2022
1 parent 36e43ae commit 3335e0b
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/dbtest/query_test.go
Expand Up @@ -982,6 +982,10 @@ func TestQuery(t *testing.T) {
// Set default VARCHAR length to 10
return db.NewCreateTable().Model((*Model)(nil)).Varchar(10)
},
func(db *bun.DB) schema.QueryAppender {
// Non-positive VARCHAR length is illegal
return db.NewCreateTable().Model((*Model)(nil)).Varchar(-20)
},
}

timeRE := regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-157
@@ -0,0 +1 @@
bun: illegal VARCHAR length: -20
8 changes: 6 additions & 2 deletions query_table_create.go
Expand Up @@ -3,6 +3,7 @@ package bun
import (
"context"
"database/sql"
"fmt"
"sort"
"strconv"
"strings"
Expand All @@ -18,12 +19,12 @@ type CreateTableQuery struct {

temp bool
ifNotExists bool

// varchar changes the default length for VARCHAR columns.
// Because some dialects require that length is always specified for VARCHAR type,
// we will use the exact user-defined type if length is set explicitly, as in `bun:",type:varchar(5)"`,
// but assume the new default length when it's omitted, e.g. `bun:",type:varchar"`.
varchar int
varchar int

fks []schema.QueryWithArgs
partitionBy schema.QueryWithArgs
Expand Down Expand Up @@ -91,6 +92,9 @@ func (q *CreateTableQuery) IfNotExists() *CreateTableQuery {

// Varchar sets default length for VARCHAR columns.
func (q *CreateTableQuery) Varchar(n int) *CreateTableQuery {
if n <= 0 {
q.setErr(fmt.Errorf("bun: illegal VARCHAR length: %d", n))
}
q.varchar = n
return q
}
Expand Down

0 comments on commit 3335e0b

Please sign in to comment.