diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d903f93..581efbf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 ./... diff --git a/README.md b/README.md index 586f62a..ffcb252 100644 --- a/README.md +++ b/README.md @@ -374,7 +374,7 @@ func decoratedDemo() { ## 类型映射 -> nullable 的列,如果值为 NULL,均以 nil 返回。 +> SQL `NULL` 映射为 `nil`,长度为零的二进制值保留为 `[]byte{}`。 ### MySql @@ -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`,当此文件存在时,会自动加载此文件。文件格式为: diff --git a/abstract_db_client_sqlite_test.go b/abstract_db_client_sqlite_test.go index cd4016e..c1b3fcf 100644 --- a/abstract_db_client_sqlite_test.go +++ b/abstract_db_client_sqlite_test.go @@ -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) diff --git a/internal/testenv/conf.go b/internal/testenv/conf.go index 51f58a9..eb804dc 100644 --- a/internal/testenv/conf.go +++ b/internal/testenv/conf.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "testing" "time" "github.com/bunnier/sqlmer" @@ -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) +} diff --git a/mssql/mssql_db_client_test.go b/mssql/mssql_db_client_test.go index 5dd804f..ba67981 100644 --- a/mssql/mssql_db_client_test.go +++ b/mssql/mssql_db_client_test.go @@ -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 } diff --git a/mysql/mysql_db_client_test.go b/mysql/mysql_db_client_test.go index 0b00fb4..5dfcabf 100644 --- a/mysql/mysql_db_client_test.go +++ b/mysql/mysql_db_client_test.go @@ -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 } diff --git a/sqlen/types.go b/sqlen/types.go index 1596dd4..f43bd61 100644 --- a/sqlen/types.go +++ b/sqlen/types.go @@ -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