Files
gf/contrib/drivers/pgsql/pgsql_z_unit_upsert_test.go
John Guo 852c3dda62 feat(contrib/drivers/dm&pgsql&mssql&oracle): add Replace/LastInsertId features support for dm/pgsql/mssql/oracle (#4547)
This pull request introduces significant improvements to the handling of
the `Replace` and `Save` operations for multiple database drivers,
especially for MSSQL and PostgreSQL. The changes ensure that these
operations now auto-detect primary keys when conflict columns are not
explicitly provided, improving usability and aligning behavior across
drivers. Additionally, the pull request updates related tests to reflect
these enhancements and includes some minor documentation and code
cleanup.

**Key changes:**

### Enhanced Replace/Save Logic for Database Drivers

* **MSSQL Driver:**
- `Replace` and `Save` operations now auto-detect primary keys if
`OnConflict` is not specified, using the `MERGE` statement for upsert
functionality. If no primary key is found in the data, a detailed error
is returned.
[[1]](diffhunk://#diff-87815aa559a927e2de09bd05148f9841dfc06a1b5f3ecc5e3d5fcb80323a87f8L23-R61)
[[2]](diffhunk://#diff-87815aa559a927e2de09bd05148f9841dfc06a1b5f3ecc5e3d5fcb80323a87f8L43-L59)
- Updated tests to verify that `Replace` correctly updates or inserts
records, and that missing conflict columns are properly handled.
[[1]](diffhunk://#diff-bdbde9d7d6ee14c795343767b414740c4396f4dd3e97788b1f9d4e615405a42dL141-R151)
[[2]](diffhunk://#diff-26338e93e473300b1313936eb0f6826546473793442f24715fa294b595f7a805L2661-R2707)

* **PostgreSQL Driver:**
- Similar to MSSQL, `Replace` and `Save` now auto-detect primary keys
for conflict resolution if `OnConflict` is not set, and treat `Replace`
as a `Save` operation.
- Adjusted tests to ensure `Save` and `Replace` work as expected,
including verifying data replacement and insertion.
[[1]](diffhunk://#diff-c22703c37ebb6836c332f7cd2ada570577ba4564fe39886db02f7c2d0e7a2048L93-R93)
[[2]](diffhunk://#diff-c22703c37ebb6836c332f7cd2ada570577ba4564fe39886db02f7c2d0e7a2048R102)
[[3]](diffhunk://#diff-c22703c37ebb6836c332f7cd2ada570577ba4564fe39886db02f7c2d0e7a2048L110-R130)

* **DM Driver:**
- Improved conflict detection: now checks that at least one primary key
exists in the provided data when `OnConflict` is not specified, and
provides clearer error messages.
- Refactored to use the core method for primary key detection and
removed redundant code.

### Minor Improvements and Documentation

* Added clarifying comments to `DoInsert` methods for ClickHouse, DM,
MSSQL, Oracle, and PostgreSQL drivers, specifying that the input list
must have at least one validated record.
[[1]](diffhunk://#diff-f2e003895041ed3c52b91bb8c270696adc3528d77c39d2f7137af3396267444cR19)
[[2]](diffhunk://#diff-f51b30e3f0b0f1284b905385a89992efd0de2fe9ff8c5a4062344dfab17d428eR23)
[[3]](diffhunk://#diff-87815aa559a927e2de09bd05148f9841dfc06a1b5f3ecc5e3d5fcb80323a87f8L23-R61)
[[4]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cR24)
[[5]](diffhunk://#diff-c1dfed79aaa3a432057d2bd74d270e4b4094ebcf72984f1161d4972bea009410R16-R72)
* Minor code and comment cleanups, including improved formatting and
error handling.
[[1]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cR37)
[[2]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cL96-R98)
[[3]](diffhunk://#diff-f61dac3fcfd5df4a3936cd8743499c8c0fc45f4f5d0f5398ed84a0cb1603202cL106-L116)
[[4]](diffhunk://#diff-a17b44c76aaac53d1f164a2bb9440a5531659f4355e7ccfabdadff8dc8633c09L170-R171)
[[5]](diffhunk://#diff-56189fa9ae1df51716b50d34d7fe56bfe67a330e8ac2c6b0de7b958db6817ed5R83-R98)

### Workflow and Documentation Updates

* Updated example Docker commands in the CI workflow for consistency and
clarity.
[[1]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL57-R57)
[[2]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL78-R78)
[[3]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL92-R92)
[[4]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL106-R106)
[[5]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL153-R153)
[[6]](diffhunk://#diff-a1a3cb9bdeb5541d148091d973cf266aa3b317e6415a86630e816cbe27cf8b9cL164-R164)
* Removed outdated note about `Replace` support from the SQLite driver
documentation.

These changes improve the consistency, reliability, and developer
experience when performing upsert operations across different database
backends.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Lance Add <1196661499@qq.com>
2025-12-09 15:46:41 +08:00

268 lines
7.6 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package pgsql_test
import (
"testing"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
)
// Test_FormatUpsert_WithOnDuplicateStr tests FormatUpsert with OnDuplicateStr
func Test_FormatUpsert_WithOnDuplicateStr(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(table).Data(g.Map{
"passport": "user1",
"password": "pwd",
"nickname": "nick1",
"create_time": CreateTime,
}).Insert()
t.AssertNil(err)
// Test Save with OnConflict (upsert)
_, err = db.Model(table).Data(g.Map{
"id": 1,
"passport": "user1",
"password": "newpwd",
"nickname": "newnick",
"create_time": CreateTime,
}).OnConflict("id").Save()
t.AssertNil(err)
// Verify the update
one, err := db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["password"].String(), "newpwd")
t.Assert(one["nickname"].String(), "newnick")
})
}
// Test_FormatUpsert_WithOnDuplicateMap tests FormatUpsert with OnDuplicateMap
func Test_FormatUpsert_WithOnDuplicateMap(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(table).Data(g.Map{
"passport": "user2",
"password": "pwd",
"nickname": "nick2",
"create_time": CreateTime,
}).Insert()
t.AssertNil(err)
// Test OnDuplicate with map - values should be column names to use EXCLUDED.column
_, err = db.Model(table).Data(g.Map{
"id": 1,
"passport": "user2",
"password": "newpwd2",
"nickname": "newnick2",
"create_time": CreateTime,
}).OnConflict("id").OnDuplicate(g.Map{
"password": "password",
"nickname": "nickname",
}).Save()
t.AssertNil(err)
// Verify - values should be from the inserted data
one, err := db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["password"].String(), "newpwd2")
t.Assert(one["nickname"].String(), "newnick2")
})
}
// Test_FormatUpsert_WithCounter tests FormatUpsert with Counter type on numeric column.
// Note: In PostgreSQL, Counter uses EXCLUDED.column which references the NEW value being inserted,
// not the current table value. This differs from MySQL's ON DUPLICATE KEY UPDATE behavior.
func Test_FormatUpsert_WithCounter(t *testing.T) {
// Create a special table with numeric id for counter test
tableName := "t_counter_test"
dropTable(tableName)
_, err := db.Exec(ctx, `
CREATE TABLE `+tableName+` (
id bigserial PRIMARY KEY,
counter_value int NOT NULL DEFAULT 0,
name varchar(45)
)
`)
if err != nil {
t.Error(err)
return
}
defer dropTable(tableName)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(tableName).Data(g.Map{
"counter_value": 10,
"name": "counter_test",
}).Insert()
t.AssertNil(err)
// Get initial ID
one, err := db.Model(tableName).Where("name", "counter_test").One()
t.AssertNil(err)
initialId := one["id"].Int64()
// Test OnDuplicate with Counter
// In PostgreSQL: counter_value = EXCLUDED.counter_value + 5
// EXCLUDED.counter_value is the value we're trying to insert (20)
// So result = 20 + 5 = 25
_, err = db.Model(tableName).Data(g.Map{
"id": initialId,
"counter_value": 20, // This is the EXCLUDED value
"name": "counter_test",
}).OnConflict("id").OnDuplicate(g.Map{
"counter_value": &gdb.Counter{
Field: "counter_value",
Value: 5,
},
}).Save()
t.AssertNil(err)
// Verify: EXCLUDED.counter_value(20) + 5 = 25
one, err = db.Model(tableName).Where("id", initialId).One()
t.AssertNil(err)
t.Assert(one["counter_value"].Int(), 25)
})
gtest.C(t, func(t *gtest.T) {
// Test Counter with negative value (decrement)
one, err := db.Model(tableName).Where("name", "counter_test").One()
t.AssertNil(err)
initialId := one["id"].Int64()
// In PostgreSQL: counter_value = EXCLUDED.counter_value - 3
// EXCLUDED.counter_value is 100, so result = 100 - 3 = 97
_, err = db.Model(tableName).Data(g.Map{
"id": initialId,
"counter_value": 100, // This is the EXCLUDED value
"name": "counter_test",
}).OnConflict("id").OnDuplicate(g.Map{
"counter_value": &gdb.Counter{
Field: "counter_value",
Value: -3,
},
}).Save()
t.AssertNil(err)
// Verify: EXCLUDED.counter_value(100) - 3 = 97
one, err = db.Model(tableName).Where("id", initialId).One()
t.AssertNil(err)
t.Assert(one["counter_value"].Int(), 97)
})
}
// Test_FormatUpsert_WithRaw tests FormatUpsert with Raw type
func Test_FormatUpsert_WithRaw(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(table).Data(g.Map{
"passport": "raw_user",
"password": "pwd",
"nickname": "nick",
"create_time": CreateTime,
}).Insert()
t.AssertNil(err)
// Get initial ID
one, err := db.Model(table).Where("passport", "raw_user").One()
t.AssertNil(err)
initialId := one["id"].Int64()
// Test OnDuplicate with Raw SQL
_, err = db.Model(table).Data(g.Map{
"id": initialId,
"passport": "raw_user",
"password": "pwd",
"nickname": "nick",
"create_time": CreateTime,
}).OnConflict("id").OnDuplicate(g.Map{
"password": gdb.Raw("'raw_password'"),
}).Save()
t.AssertNil(err)
// Verify
one, err = db.Model(table).Where("id", initialId).One()
t.AssertNil(err)
t.Assert(one["password"].String(), "raw_password")
})
}
// Test_FormatUpsert_NoOnConflict tests FormatUpsert without OnConflict (should fail)
func Test_FormatUpsert_NoOnConflict(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(table).Data(g.Map{
"passport": "no_conflict_user",
"password": "pwd",
"nickname": "nick",
"create_time": CreateTime,
}).Insert()
t.AssertNil(err)
// Try Save without OnConflict and without primary key in data - should fail
// because driver cannot auto-detect conflict columns when primary key is missing
_, err = db.Model(table).Data(g.Map{
// "id": 1,
"passport": "no_conflict_user",
"password": "newpwd",
"nickname": "newnick",
"create_time": CreateTime,
}).Save()
t.AssertNE(err, nil)
})
}
// Test_FormatUpsert_MultipleConflictKeys tests FormatUpsert with multiple conflict keys
func Test_FormatUpsert_MultipleConflictKeys(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
// Insert initial data
_, err := db.Model(table).Data(g.Map{
"passport": "multi_key_user",
"password": "pwd",
"nickname": "nick",
"create_time": CreateTime,
}).Insert()
t.AssertNil(err)
// Test with multiple conflict keys using only "id" which has a unique constraint
// Note: Using multiple keys requires a composite unique constraint to exist
_, err = db.Model(table).Data(g.Map{
"id": 1,
"passport": "multi_key_user",
"password": "newpwd",
"nickname": "newnick",
"create_time": CreateTime,
}).OnConflict("id").Save()
t.AssertNil(err)
// Verify the update
one, err := db.Model(table).Where("id", 1).One()
t.AssertNil(err)
t.Assert(one["password"].String(), "newpwd")
t.Assert(one["nickname"].String(), "newnick")
})
}