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 query comment into the query for improved debugging #1082

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
142 changes: 142 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,148 @@ func TestQuery(t *testing.T) {
return db.NewDelete().Model(&Model{}).WherePK().Returning("*")
},
},
{
id: 173,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewAddColumn().
Model(&Model{}).
Comment("test").
ColumnExpr("column_name VARCHAR(123)")
},
},
{
id: 174,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewDropColumn().
Model(&Model{}).
Comment("test").
Column("column_name")
},
},
{
id: 175,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewDelete().
Model(&Model{}).
WherePK().
Comment("test")
},
},
{
id: 176,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewCreateIndex().
Model(&Model{}).
Unique().
Index("index_name").
Comment("test").
Column("column_name")
},
},
{
id: 177,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewDropIndex().
Model(&Model{}).
Comment("test").
Index("index_name").
Comment("test")
},
},
{
id: 178,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewInsert().
Model(&Model{}).
Comment("test").
Value("column_name", "value")
},
},
{
id: 179,
query: func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64 `bun:",pk,autoincrement"`
Name string
Value string
}

newModels := []*Model{
{Name: "A", Value: "world"},
{Name: "B", Value: "test"},
}

return db.NewMerge().
Model(new(Model)).
With("_data", db.NewValues(&newModels)).
Using("_data").
On("?TableAlias.name = _data.name").
WhenUpdate("MATCHED", func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("value = _data.value")
}).
WhenInsert("NOT MATCHED", func(q *bun.InsertQuery) *bun.InsertQuery {
return q.Value("name", "_data.name").Value("value", "_data.value")
}).
Comment("test").
Returning("$action")

},
},
{
id: 180,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewRaw("SELECT 1").Comment("test")
},
},
{
id: 181,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewSelect().
Model(&Model{}).
Comment("test")
},
},
{
id: 182,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewCreateTable().
Model(&Model{}).
Comment("test")
},
},
{
id: 183,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewDropTable().
Model(&Model{}).
Comment("test")
},
},
{
id: 184,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewTruncateTable().
Model(&Model{}).
Comment("test")
},
},
{
id: 185,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewUpdate().
Model(&Model{}).
Comment("test").
Set("name = ?", "new-name").
Where("id = ?", 1)
},
},
{
id: 186,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewValues(&[]Model{{1, "hello"}}).
Comment("test")
},
},
}

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-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE `models` ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE `models` DROP COLUMN `column_name`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM `models` WHERE (`id` = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX `index_name` ON `models` (`column_name`)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO `models` (`id`, `str`, `column_name`) VALUES (DEFAULT, '', value) RETURNING `id`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: merge not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT `model`.`id`, `model`.`str` FROM `models` AS `model`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE `models` (`id` BIGINT NOT NULL AUTO_INCREMENT, `str` VARCHAR(255), PRIMARY KEY (`id`))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE `models`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ TRUNCATE TABLE `models`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE `models` AS `model` SET name = 'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES ROW(1, 'hello')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" DROP COLUMN "column_name"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM "models" WHERE ("id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name")
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO "models" ("str", "column_name") OUTPUT INSERTED."id" VALUES (N'', value)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ WITH "_data" AS (SELECT * FROM (VALUES (NULL, N'A', N'world'), (NULL, N'B', N'test')) AS t ("id", "name", "value")) MERGE "models" AS "model" USING _data ON "model".name = _data.name WHEN MATCHED THEN UPDATE SET value = _data.value WHEN NOT MATCHED THEN INSERT ("name", "value") VALUES (_data.name, _data.value) OUTPUT $action;
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE "models" ("id" BIGINT NOT NULL IDENTITY, "str" VARCHAR(255), PRIMARY KEY ("id"))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE "models"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM "models"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE "models" SET name = N'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES (1, N'hello')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE `models` ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE `models` DROP COLUMN `column_name`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM `models` WHERE (`id` = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX `index_name` ON `models` (`column_name`)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO `models` (`id`, `str`, `column_name`) VALUES (DEFAULT, '', value)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: merge not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT `model`.`id`, `model`.`str` FROM `models` AS `model`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE `models` (`id` BIGINT NOT NULL AUTO_INCREMENT, `str` VARCHAR(255), PRIMARY KEY (`id`))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE `models`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ TRUNCATE TABLE `models`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE `models` AS `model` SET name = 'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES ROW(1, 'hello')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE `models` ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE `models` DROP COLUMN `column_name`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM `models` AS `model` WHERE (`model`.`id` = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX `index_name` ON `models` (`column_name`)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO `models` (`id`, `str`, `column_name`) VALUES (DEFAULT, '', value)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: merge not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT `model`.`id`, `model`.`str` FROM `models` AS `model`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE `models` (`id` BIGINT NOT NULL AUTO_INCREMENT, `str` VARCHAR(255), PRIMARY KEY (`id`))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE `models`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ TRUNCATE TABLE `models`
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE `models` AS `model` SET name = 'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES ROW(1, 'hello')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" DROP COLUMN "column_name"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name")
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO "models" ("id", "str", "column_name") VALUES (DEFAULT, '', value) RETURNING "id"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ WITH "_data" ("id", "name", "value") AS (VALUES (NULL::BIGINT, 'A'::VARCHAR, 'world'::VARCHAR), (NULL::BIGINT, 'B'::VARCHAR, 'test'::VARCHAR)) MERGE INTO "models" AS "model" USING _data ON "model".name = _data.name WHEN MATCHED THEN UPDATE SET value = _data.value WHEN NOT MATCHED THEN INSERT ("id", "name", "value") VALUES (DEFAULT, _data.name, _data.value);
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE "models" ("id" BIGSERIAL NOT NULL, "str" VARCHAR, PRIMARY KEY ("id"))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE "models"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ TRUNCATE TABLE "models" RESTART IDENTITY
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE "models" AS "model" SET name = 'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES (1::BIGINT, 'hello'::VARCHAR)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" DROP COLUMN "column_name"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name")
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO "models" ("id", "str", "column_name") VALUES (DEFAULT, '', value) RETURNING "id"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ WITH "_data" ("id", "name", "value") AS (VALUES (NULL::BIGINT, 'A'::VARCHAR, 'world'::VARCHAR), (NULL::BIGINT, 'B'::VARCHAR, 'test'::VARCHAR)) MERGE INTO "models" AS "model" USING _data ON "model".name = _data.name WHEN MATCHED THEN UPDATE SET value = _data.value WHEN NOT MATCHED THEN INSERT ("id", "name", "value") VALUES (DEFAULT, _data.name, _data.value);
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE "models" ("id" BIGSERIAL NOT NULL, "str" VARCHAR, PRIMARY KEY ("id"))
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE "models"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ TRUNCATE TABLE "models" RESTART IDENTITY
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE "models" AS "model" SET name = 'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES (1::BIGINT, 'hello'::VARCHAR)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-173
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" ADD column_name VARCHAR(123)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-174
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ ALTER TABLE "models" DROP COLUMN "column_name"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-175
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-176
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE UNIQUE INDEX "index_name" ON "models" ("column_name")
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-177
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP INDEX index_name
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-178
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ INSERT INTO "models" ("str", "column_name") VALUES ('', value) RETURNING "id"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-179
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: merge not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-180
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-181
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ SELECT "model"."id", "model"."str" FROM "models" AS "model"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-182
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ CREATE TABLE "models" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "str" VARCHAR)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-183
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DROP TABLE "models"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-184
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ DELETE FROM "models"
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-185
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ UPDATE "models" AS "model" SET name = 'new-name' WHERE (id = 1)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-186
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* test */ VALUES (1, 'hello')
12 changes: 12 additions & 0 deletions query_column_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type AddColumnQuery struct {
baseQuery

ifNotExists bool
comment string
}

var _ Query = (*AddColumnQuery)(nil)
Expand Down Expand Up @@ -85,6 +86,14 @@ func (q *AddColumnQuery) IfNotExists() *AddColumnQuery {

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

// Comment adds a comment to the query, wrapped by /* ... */.
func (q *AddColumnQuery) Comment(comment string) *AddColumnQuery {
q.comment = comment
return q
}

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

func (q *AddColumnQuery) Operation() string {
return "ADD COLUMN"
}
Expand All @@ -93,6 +102,9 @@ func (q *AddColumnQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte
if q.err != nil {
return nil, q.err
}

b = appendComment(b, q.comment)

if len(q.columns) != 1 {
return nil, fmt.Errorf("bun: AddColumnQuery requires exactly one column")
}
Expand Down
Loading
Loading