Skip to content

Commit

Permalink
Merge pull request #273 from uptrace/feat/add-col-if-not-exists
Browse files Browse the repository at this point in the history
feat: support ADD COLUMN IF NOT EXISTS
  • Loading branch information
vmihailenco authored Oct 27, 2021
2 parents 569503b + ca7357c commit 6601d13
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,12 @@ func TestQuery(t *testing.T) {
func(db *bun.DB) schema.QueryAppender {
return db.NewInsert().Model(&Model{Str: "hello"}).On("DUPLICATE KEY UPDATE")
},
func(db *bun.DB) schema.QueryAppender {
return db.NewAddColumn().Model(new(Model)).
ModelTableExpr("mytable").
IfNotExists().
ColumnExpr("column_name VARCHAR(123)")
},
}

timeRE := regexp.MustCompile(`'\d{4}-\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-mysql5-98
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-98
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-98
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-98
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-98
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123)
19 changes: 12 additions & 7 deletions query_column_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

type AddColumnQuery struct {
baseQuery

ifNotExists bool
}

func NewAddColumnQuery(db *DB) *AddColumnQuery {
Expand Down Expand Up @@ -59,6 +61,11 @@ func (q *AddColumnQuery) ColumnExpr(query string, args ...interface{}) *AddColum
return q
}

func (q *AddColumnQuery) IfNotExists() *AddColumnQuery {
q.ifNotExists = true
return q
}

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

func (q *AddColumnQuery) Operation() string {
Expand All @@ -82,6 +89,10 @@ func (q *AddColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte

b = append(b, " ADD "...)

if q.ifNotExists {
b = append(b, "IF NOT EXISTS "...)
}

b, err = q.columns[0].AppendQuery(fmter, b)
if err != nil {
return nil, err
Expand All @@ -99,11 +110,5 @@ func (q *AddColumnQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Res
}

query := internal.String(queryBytes)

res, err := q.exec(ctx, q, query)
if err != nil {
return nil, err
}

return res, nil
return q.exec(ctx, q, query)
}

0 comments on commit 6601d13

Please sign in to comment.