Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add feature flag AlterColumnExists #1106

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add feature flag AlterColumnExists
With #704
  • Loading branch information
j2gg0s committed Jan 13, 2025

Verified

This commit was signed with the committer’s verified signature.
alexsapran Alexandros Sapranidis
commit fc35e1222242b3d581f0b7496a9021aadfc50b07
58 changes: 57 additions & 1 deletion dialect/feature/feature.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package feature

import "github.com/uptrace/bun/internal"
import (
"fmt"
"strconv"

"github.com/uptrace/bun/internal"
)

type Feature = internal.Flag

@@ -35,4 +40,55 @@ const (
UpdateOrderLimit // UPDATE ... ORDER BY ... LIMIT ...
DeleteOrderLimit // DELETE ... ORDER BY ... LIMIT ...
DeleteReturning
AlterColumnExists // ADD/DROP COLUMN IF NOT EXISTS/IF EXISTS
)

type NotSupportError struct {
Flag Feature
}

func (err *NotSupportError) Error() string {
name, ok := flag2str[err.Flag]
if !ok {
name = strconv.FormatInt(int64(err.Flag), 10)
}
return fmt.Sprintf("bun: feature %s is not supported by current dialect", name)
}

func NewNotSupportError(flag Feature) *NotSupportError {
return &NotSupportError{Flag: flag}
}

var flag2str = map[Feature]string{
CTE: "CTE",
WithValues: "WithValues",
Returning: "Returning",
InsertReturning: "InsertReturning",
Output: "Output",
DefaultPlaceholder: "DefaultPlaceholder",
DoubleColonCast: "DoubleColonCast",
ValuesRow: "ValuesRow",
UpdateMultiTable: "UpdateMultiTable",
InsertTableAlias: "InsertTableAlias",
UpdateTableAlias: "UpdateTableAlias",
DeleteTableAlias: "DeleteTableAlias",
AutoIncrement: "AutoIncrement",
Identity: "Identity",
TableCascade: "TableCascade",
TableIdentity: "TableIdentity",
TableTruncate: "TableTruncate",
InsertOnConflict: "InsertOnConflict",
InsertOnDuplicateKey: "InsertOnDuplicateKey",
InsertIgnore: "InsertIgnore",
TableNotExists: "TableNotExists",
OffsetFetch: "OffsetFetch",
SelectExists: "SelectExists",
UpdateFromTable: "UpdateFromTable",
MSSavepoint: "MSSavepoint",
GeneratedIdentity: "GeneratedIdentity",
CompositeIn: "CompositeIn",
UpdateOrderLimit: "UpdateOrderLimit",
DeleteOrderLimit: "DeleteOrderLimit",
DeleteReturning: "DeleteReturning",
AlterColumnExists: "AlterColumnExists",
}
3 changes: 2 additions & 1 deletion dialect/pgdialect/dialect.go
Original file line number Diff line number Diff line change
@@ -53,7 +53,8 @@ func New(opts ...DialectOption) *Dialect {
feature.SelectExists |
feature.GeneratedIdentity |
feature.CompositeIn |
feature.DeleteReturning
feature.DeleteReturning |
feature.AlterColumnExists

for _, opt := range opts {
opt(d)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-168
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: order is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-169
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-170
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature UpdateOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-172
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: returning is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mysql5-172
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: returning is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mysql8-172
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: returning is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-168
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: order is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-169
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-170
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature UpdateOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-168
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: order is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-169
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-170
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature UpdateOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-168
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: order is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-169
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-170
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature DeleteOrderLimit is not supported by current dialect
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bun: limit is not supported for current dialect
bun: feature UpdateOrderLimit is not supported by current dialect
5 changes: 5 additions & 0 deletions query_column_add.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"

"github.com/uptrace/bun/dialect/feature"
"github.com/uptrace/bun/internal"
"github.com/uptrace/bun/schema"
)
@@ -133,6 +134,10 @@ func (q *AddColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte
//------------------------------------------------------------------------------

func (q *AddColumnQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
if q.ifNotExists && !q.hasFeature(feature.AlterColumnExists) {
return nil, feature.NewNotSupportError(feature.AlterColumnExists)
}

queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
if err != nil {
return nil, err
8 changes: 4 additions & 4 deletions query_delete.go
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ func (q *DeleteQuery) WhereAllWithDeleted() *DeleteQuery {

func (q *DeleteQuery) Order(orders ...string) *DeleteQuery {
if !q.hasFeature(feature.DeleteOrderLimit) {
q.err = errors.New("bun: order is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.DeleteOrderLimit)
return q
}
q.addOrder(orders...)
@@ -137,7 +137,7 @@ func (q *DeleteQuery) Order(orders ...string) *DeleteQuery {

func (q *DeleteQuery) OrderExpr(query string, args ...interface{}) *DeleteQuery {
if !q.hasFeature(feature.DeleteOrderLimit) {
q.err = errors.New("bun: order is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.DeleteOrderLimit)
return q
}
q.addOrderExpr(query, args...)
@@ -152,7 +152,7 @@ func (q *DeleteQuery) ForceDelete() *DeleteQuery {
// ------------------------------------------------------------------------------
func (q *DeleteQuery) Limit(n int) *DeleteQuery {
if !q.hasFeature(feature.DeleteOrderLimit) {
q.err = errors.New("bun: limit is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.DeleteOrderLimit)
return q
}
q.setLimit(n)
@@ -166,7 +166,7 @@ func (q *DeleteQuery) Limit(n int) *DeleteQuery {
// To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.
func (q *DeleteQuery) Returning(query string, args ...interface{}) *DeleteQuery {
if !q.hasFeature(feature.DeleteReturning) {
q.err = errors.New("bun: returning is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.DeleteOrderLimit)
return q
}

6 changes: 3 additions & 3 deletions query_update.go
Original file line number Diff line number Diff line change
@@ -207,7 +207,7 @@ func (q *UpdateQuery) WhereAllWithDeleted() *UpdateQuery {
// ------------------------------------------------------------------------------
func (q *UpdateQuery) Order(orders ...string) *UpdateQuery {
if !q.hasFeature(feature.UpdateOrderLimit) {
q.err = errors.New("bun: order is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.UpdateOrderLimit)
return q
}
q.addOrder(orders...)
@@ -216,7 +216,7 @@ func (q *UpdateQuery) Order(orders ...string) *UpdateQuery {

func (q *UpdateQuery) OrderExpr(query string, args ...interface{}) *UpdateQuery {
if !q.hasFeature(feature.UpdateOrderLimit) {
q.err = errors.New("bun: order is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.UpdateOrderLimit)
return q
}
q.addOrderExpr(query, args...)
@@ -225,7 +225,7 @@ func (q *UpdateQuery) OrderExpr(query string, args ...interface{}) *UpdateQuery

func (q *UpdateQuery) Limit(n int) *UpdateQuery {
if !q.hasFeature(feature.UpdateOrderLimit) {
q.err = errors.New("bun: limit is not supported for current dialect")
q.err = feature.NewNotSupportError(feature.UpdateOrderLimit)
return q
}
q.setLimit(n)
Loading