Skip to content
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
63 changes: 54 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,63 @@ on:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 25

services:
mysql:
image: mysql:8.4
env:
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_DATABASE: test
MYSQL_USER: testuser
MYSQL_PASSWORD: testuser
ports:
- 127.0.0.1:13306:3306
options: >-
--health-cmd="mysql --protocol=TCP -h 127.0.0.1 -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e 'SELECT 1'"
--health-interval=5s --health-timeout=5s --health-retries=30
mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: Sqlmer_Test_2022
ports:
- 127.0.0.1:11433:1433
options: >-
--health-cmd="/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P $MSSQL_SA_PASSWORD -C -b -Q 'SELECT 1'"
--health-interval=5s --health-timeout=5s --health-retries=30

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version-file: go.mod

- name: Build and vet
run: |
go build ./...
go vet ./...

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.24.0
- name: Prepare test databases
run: |
cat > .db.json <<'JSON'
{
"mysql": "testuser:testuser@tcp(127.0.0.1:13306)/test",
"sqlserver": "server=127.0.0.1;port=11433;database=tempdb;user id=sa;password=Sqlmer_Test_2022"
}
JSON
go run ./internal/testcmd -a PREPARE

- name: Build
run: go build -v ./...
- name: Run regression tests
env:
SQLMER_REQUIRE_TEST_DB: "1"
# mysql and wrap modify the same transaction fixtures.
run: go test -race -count=1 -p 1 ./...
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func decoratedDemo() {

## 类型映射

> nullable 的列,如果值为 NULL,均以 nil 返回。
> SQL `NULL` 映射为 `nil`,长度为零的二进制值保留为 `[]byte{}`。

### MySql

Expand Down Expand Up @@ -411,7 +411,9 @@ func decoratedDemo() {

## 测试用例

测试用例 Schema:
向 `main` 推送代码或提交以 `main` 为目标的 PR 时,GitHub Actions 会构建项目、运行 `go vet`,并使用 SQLite、MySQL 和 SQL Server 执行全量竞态测试。也可以在 Actions 的 **Go** 工作流中手动运行。

在本地运行同一套回归测试时,先按以下步骤配置数据库,准备好测试表后执行 `SQLMER_REQUIRE_TEST_DB=1 go test -race -count=1 -p 1 ./...`。`mysql` 和 `wrap` 共用测试表,按包串行执行可避免它们同时修改同一批记录。

1. 默认的数据库连接字符串定义在 `internal/testenv/conf.go` 里,默认使用本机的测试库。当需要自定义测试库连接字符串时,可在项目的根(和当前 README 同级)下新建 `.db.json`,当此文件存在时,会自动加载此文件。文件格式为:

Expand Down
13 changes: 13 additions & 0 deletions abstract_db_client_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ func Test_AbstractDbClient_SliceGet(t *testing.T) {
}
}

func Test_AbstractDbClient_SliceGet_distinguishes_empty_and_null_blob(t *testing.T) {
dbClient := newSqliteDbClientForAbstractDbTest(t)

got, err := dbClient.SliceGet("SELECT x'' AS data UNION ALL SELECT NULL")
if err != nil {
t.Fatal(err)
}
want := []map[string]any{{"data": []byte{}}, {"data": nil}}
if !reflect.DeepEqual(got, want) {
t.Fatalf("SliceGet() = %#v, want %#v", got, want)
}
}

func Test_AbstractDbClient_Rows(t *testing.T) {
dbClient := newSqliteDbClientForAbstractDbTest(t)

Expand Down
11 changes: 11 additions & 0 deletions internal/testenv/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/bunnier/sqlmer"
Expand Down Expand Up @@ -90,3 +91,13 @@ func NewSqliteClient() (sqlmer.DbClient, error) {
sqlmer.WithExecTimeout(DefaultTimeout),
)
}

// SkipUnavailableDB allows local smoke tests to skip an unavailable database.
// CI sets SQLMER_REQUIRE_TEST_DB when it provisions the database for the suite.
func SkipUnavailableDB(t testing.TB, err error) {
t.Helper()
if os.Getenv("SQLMER_REQUIRE_TEST_DB") == "1" {
t.Fatalf("integration database unavailable: %v", err)
}
t.Skipf("integration database unavailable: %v", err)
}
4 changes: 2 additions & 2 deletions mssql/mssql_db_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func getMsSqlClientOrSkip(t *testing.T) sqlmer.DbClient {

dbClient, err := testenv.NewSqlServerClient()
if err != nil {
t.Skipf("skip mssql integration smoke test: %v", err)
testenv.SkipUnavailableDB(t, err)
}
if _, _, err = dbClient.Scalar("SELECT 1"); err != nil {
t.Skipf("skip mssql integration smoke test: %v", err)
testenv.SkipUnavailableDB(t, err)
}
return dbClient
}
Expand Down
2 changes: 1 addition & 1 deletion mysql/mysql_db_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func getMysqlClientOrSkip(t *testing.T) sqlmer.DbClient {

mysqlClient, err := testenv.NewMysqlClient()
if err != nil {
t.Skipf("skip mysql integration smoke test: %v", err)
testenv.SkipUnavailableDB(t, err)
}
return mysqlClient
}
Expand Down
4 changes: 4 additions & 0 deletions sqlen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func extractNullableColumnValue(columnType *sql.ColumnType, srcP *any) {
break
}
*srcP = src.Time
case []byte:
if src == nil {
*srcP = nil
}
case sql.RawBytes:
if src == nil {
*srcP = nil
Expand Down