diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index 8a3ce3ff4..6921e02b2 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -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})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-98 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-98 new file mode 100644 index 000000000..c1671bbcc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-98 @@ -0,0 +1 @@ +ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-98 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-98 new file mode 100644 index 000000000..c1671bbcc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-98 @@ -0,0 +1 @@ +ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-98 b/internal/dbtest/testdata/snapshots/TestQuery-pg-98 new file mode 100644 index 000000000..c1671bbcc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-98 @@ -0,0 +1 @@ +ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-98 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-98 new file mode 100644 index 000000000..c1671bbcc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-98 @@ -0,0 +1 @@ +ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-98 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-98 new file mode 100644 index 000000000..c1671bbcc --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-98 @@ -0,0 +1 @@ +ALTER TABLE mytable ADD IF NOT EXISTS column_name VARCHAR(123) diff --git a/query_column_add.go b/query_column_add.go index 30d76e610..dff514904 100644 --- a/query_column_add.go +++ b/query_column_add.go @@ -11,6 +11,8 @@ import ( type AddColumnQuery struct { baseQuery + + ifNotExists bool } func NewAddColumnQuery(db *DB) *AddColumnQuery { @@ -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 { @@ -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 @@ -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) }