-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Introduce ALTER TABLE query #726
base: master
Are you sure you want to change the base?
Changes from all commits
d605c42
911ce6a
a6ce183
bcb8135
f8ff602
cf62d64
99743c9
e409c36
234f34f
8abcf63
533383a
7c926a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package dbtest_test | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/uptrace/bun/dialect/feature" | ||
"github.com/uptrace/bun/dialect/sqltype" | ||
"path/filepath" | ||
"regexp" | ||
"testing" | ||
|
@@ -19,6 +21,8 @@ func init() { | |
cupaloy.Global = cupaloy.Global.WithOptions(cupaloy.SnapshotSubdirectory(snapshotsDir)) | ||
} | ||
|
||
var timeRE = regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`) | ||
|
||
func TestQuery(t *testing.T) { | ||
type Model struct { | ||
ID int64 `bun:",pk,autoincrement"` | ||
|
@@ -1003,8 +1007,6 @@ func TestQuery(t *testing.T) { | |
}, | ||
} | ||
|
||
timeRE := regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`) | ||
|
||
testEachDB(t, func(t *testing.T, dbName string, db *bun.DB) { | ||
for i, fn := range queries { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
|
@@ -1021,3 +1023,128 @@ func TestQuery(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestAlterTable(t *testing.T) { | ||
needsAlterTable := needs(feature.AlterTableQuery, "ALTER TABLE") | ||
type Model struct { | ||
ID int64 | ||
Old string | ||
Active bool | ||
} | ||
|
||
cases := []TestCase{ | ||
{ | ||
"rename column", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)). | ||
RenameColumn().Column("old").To("new") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reasons why not simply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto: #726 (comment) |
||
}, | ||
}, | ||
{ | ||
"invalid model", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model(1) | ||
}, | ||
}, | ||
{ | ||
"rename table", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)). | ||
Rename().To("new_models") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, that's actually what I started with, but I wasn't sure if it wasn't more consistent with the rest of the API to mirror actual the SQL keywords. |
||
}, | ||
}, | ||
{ | ||
"rename table if exists", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)).IfExists(). | ||
Rename().To("new_models") | ||
}, | ||
}, | ||
{ | ||
"change column type", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
// Change column type with common SQL data type | ||
return db.NewAlterTable().Model((*Model)(nil)). | ||
AlterColumn().Column("old").Type(sqltype.Blob) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same suggestion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto: #726 (comment) |
||
}, | ||
}, | ||
{ | ||
"change column type chained", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
// Alter 2 columns in a chained query | ||
return db.NewAlterTable().Model((*Model)(nil)). | ||
AlterColumn().Column("old").Type(sqltype.Blob). | ||
AlterColumn().Column("active").Type(sqltype.SmallInt) | ||
}, | ||
}, | ||
{ | ||
"add column", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)).AddColumn(). | ||
ColumnExpr("deadline ?", bun.Safe(sqltype.Timestamp)) | ||
}, | ||
}, | ||
{ | ||
"add several columns", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)). | ||
AddColumn().ColumnExpr("one INTEGER"). | ||
AddColumn().ColumnExpr("two BIGINT") | ||
}, | ||
}, | ||
{ | ||
"drop column if exists", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)).DropColumn().IfExists().Column("old") | ||
}, | ||
}, | ||
{ | ||
"drop column expression", needsAlterTable, | ||
func(db *bun.DB) schema.QueryAppender { | ||
return db.NewAlterTable().Model((*Model)(nil)).DropColumn().ColumnExpr("old CASCADE") | ||
}, | ||
}, | ||
} | ||
|
||
testEachDB(t, func(t *testing.T, dbName string, db *bun.DB) { | ||
for i, tt := range cases { | ||
skipIfNotHasFeature(t, db, tt.req.Feature, tt.req.Name) | ||
|
||
t.Run(fmt.Sprintf("%d-%s", i, tt.name), func(t *testing.T) { | ||
checkQuerySnapshot(t, db, tt.fn(db)) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
func checkQuerySnapshot(t *testing.T, db *bun.DB, q schema.QueryAppender) { | ||
t.Helper() | ||
query, err := q.AppendQuery(db.Formatter(), nil) | ||
if err != nil { | ||
cupaloy.SnapshotT(t, err.Error()) | ||
return | ||
} | ||
|
||
query = timeRE.ReplaceAll(query, []byte("[TIME]")) | ||
cupaloy.SnapshotT(t, string(query)) | ||
} | ||
|
||
// Example: | ||
// tt := TestCase{ | ||
// "common table expressions", needs(feature.CTE, "CTE"), | ||
// func(db *bun.DB) schema.QueryAppender {...}, | ||
// } | ||
type TestCase struct { | ||
name string | ||
req *requiredFeature | ||
fn func(db *bun.DB) schema.QueryAppender | ||
} | ||
|
||
type requiredFeature struct { | ||
feature.Feature | ||
Name string | ||
} | ||
|
||
func needs(feat feature.Feature, name string) *requiredFeature { | ||
return &requiredFeature{feat, name} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" RENAME COLUMN "old" TO "new" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bun: Model(non-pointer int) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" RENAME TO "new_models" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE IF EXISTS "models" RENAME TO "new_models" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" ALTER COLUMN "old" SET DATA TYPE BLOB |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" ALTER COLUMN "old" SET DATA TYPE BLOB, ALTER COLUMN "active" SET DATA TYPE SMALLINT |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" ADD COLUMN deadline TIMESTAMP |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" ADD COLUMN one INTEGER, ADD COLUMN two BIGINT |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" DROP COLUMN IF EXISTS "old" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "models" DROP COLUMN old CASCADE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: skipIfNoFeature