Files
seaweedfs/weed/filer/mysql/mysql_sql_gen_test.go
T
Chris LuandClaude Opus 5.5 abe623c570 filer/sql: generators bind their own statement arguments (#13251)
* filer/store_test: conformance suite for filer stores

Entries in and out of buckets, byte-ordered and prefixed listings,
key-value records, transactions, bucket tables and the snapshot scan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: run postgres2 and mysql2 through the conformance suite

RUN_POSTGRES_TESTS and RUN_MYSQL_TESTS enable them, as RUN_REDIS_TESTS
does the Redis stores; each test gets a schema or database of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* ci: give the go test job Postgres and MySQL services

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer/sql: generators bind the arguments of row statements

Insert, update, find and delete now take the row key and return their
arguments with the query, so a generator decides how a key maps to its
columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer/sql: generators bind the arguments of directory statements

Listing and deleting a directory's children take the directory and return
their arguments with the query.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer/sql: batch lookup takes row keys and matches rows by name and directory

A key-value key's directory column already determines its dirhash, so the
batch read no longer selects dirhash to match its rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer/sql: scan pages take the row key of the last row as their cursor

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer/sql: test the arguments each generator binds

Each statement's arguments must match its placeholders in count and
column. Check them for every generator method, including the Postgres
UPDATE whose parameters are now numbered key first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer/postgres: escape the table name inside its quotes

A double quote in it is doubled, as PostgreSQL reads a quoted identifier.
Bucket names cannot hold one; the statements no longer rely on that.
Reported by CodeQL (go/unsafe-quoting) on the lines this branch changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer/postgres: keep the nameExpr doc comment on nameExpr

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 03:45:44 -08:00

144 lines
5.7 KiB
Go

package mysql
import (
"reflect"
"regexp"
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
)
func TestDefaultUpsertQueryUsesOnDuplicateKey(t *testing.T) {
gen := &SqlGenMysql{UpsertQueryTemplate: DefaultUpsertQuery}
got, _ := gen.GetSqlInsert("filemeta", abstract_sql.RowKey{}, nil)
if !strings.Contains(got, "ON DUPLICATE KEY UPDATE") {
t.Fatalf("expected ON DUPLICATE KEY UPDATE in default upsert, got: %s", got)
}
if strings.Contains(got, "AS `new`") {
t.Fatalf("default should avoid MySQL 8.0.19 row-alias syntax for MariaDB compat, got: %s", got)
}
if !strings.Contains(got, "`filemeta`") {
t.Fatalf("expected backticked table name, got: %s", got)
}
}
func TestDefaultCreateTableQueryRendersValidSql(t *testing.T) {
gen := &SqlGenMysql{CreateTableSqlTemplate: DefaultCreateTableQuery}
got := gen.GetSqlCreateTable("filemeta")
if strings.Contains(got, "%!") {
t.Fatalf("template rendered to malformed SQL, got: %s", got)
}
if !strings.Contains(got, "`filemeta`") {
t.Fatalf("expected backticked table name, got: %s", got)
}
}
func TestEmptyUpsertTemplateFallsBackToPlainInsert(t *testing.T) {
gen := &SqlGenMysql{}
got, _ := gen.GetSqlInsert("filemeta", abstract_sql.RowKey{}, nil)
if strings.Contains(got, "ON DUPLICATE KEY UPDATE") {
t.Fatalf("plain INSERT path should not contain ON DUPLICATE KEY UPDATE, got: %s", got)
}
}
func TestListSqlDefaultOrderingFollowsColumn(t *testing.T) {
gen := &SqlGenMysql{}
for _, got := range listQueries(gen) {
if strings.Contains(got, "BINARY") {
t.Fatalf("default list query should not force BINARY, got: %s", got)
}
if !strings.Contains(got, "ORDER BY `name` ASC") {
t.Fatalf("expected plain name ordering, got: %s", got)
}
}
}
func TestListSqlBinaryOrderingOnNonBinaryColumn(t *testing.T) {
gen := &SqlGenMysql{ForceBinaryCollation: true}
for _, got := range listQueries(gen) {
if !strings.Contains(got, "ORDER BY BINARY `name` ASC") {
t.Fatalf("expected BINARY ordering, got: %s", got)
}
if !strings.Contains(got, "BINARY `name` LIKE ?") {
t.Fatalf("expected BINARY prefix filter, got: %s", got)
}
if strings.Contains(got, "AND `name` > ?") || strings.Contains(got, "AND `name` >= ?") {
t.Fatalf("pagination comparison must also be BINARY, got: %s", got)
}
}
}
func TestListSqlEscapesLikeWildcards(t *testing.T) {
gen := &SqlGenMysql{}
for _, got := range listQueries(gen) {
if !strings.Contains(got, "LIKE ? ESCAPE '!'") {
t.Fatalf("list query must declare the escape char matching LikePattern, got: %s", got)
}
}
}
func TestIsBinaryCollation(t *testing.T) {
binary := []string{"", "binary", "utf8mb4_bin", "utf8mb3_bin", "latin1_bin", "UTF8MB4_BIN"}
for _, c := range binary {
if !isBinaryCollation(c) {
t.Fatalf("expected %q to be treated as binary", c)
}
}
ci := []string{"utf8mb4_general_ci", "utf8mb3_general_ci", "utf8mb4_0900_ai_ci", "latin1_swedish_ci"}
for _, c := range ci {
if isBinaryCollation(c) {
t.Fatalf("expected %q to be treated as non-binary", c)
}
}
}
func listQueries(gen *SqlGenMysql) []string {
exclusive, _ := gen.GetSqlListExclusive("filemeta", "/dir", "", "%", 10)
inclusive, _ := gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10)
return []string{exclusive, inclusive}
}
func TestStatementArguments(t *testing.T) {
key := abstract_sql.EntryRowKey("/dir", "name")
other := abstract_sql.EntryRowKey("/other", "file")
meta := []byte("meta")
column := map[string]interface{}{"dirhash": key.DirHash, "name": key.Name, "directory": key.Directory, "meta": meta}
plain, upsert := &SqlGenMysql{}, &SqlGenMysql{UpsertQueryTemplate: DefaultUpsertQuery}
type statement struct {
sql string
args []interface{}
}
stmt := func(sql string, args []interface{}) statement { return statement{sql, args} }
for name, tc := range map[string]struct {
got statement
want []interface{}
}{
"insert": {stmt(plain.GetSqlInsert("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
"upsert": {stmt(upsert.GetSqlInsert("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
"update": {stmt(plain.GetSqlUpdate("t", key, meta)), []interface{}{meta, key.DirHash, "name", "/dir"}},
"find": {stmt(plain.GetSqlFind("t", key)), []interface{}{key.DirHash, "name", "/dir"}},
"find batch": {stmt(plain.GetSqlFindBatch("t", []abstract_sql.RowKey{key, other})), []interface{}{key.DirHash, "name", "/dir", other.DirHash, "file", "/other"}},
"delete": {stmt(plain.GetSqlDelete("t", key)), []interface{}{key.DirHash, "name", "/dir"}},
"delete children": {stmt(plain.GetSqlDeleteFolderChildren("t", "/dir")), []interface{}{key.DirHash, "/dir"}},
"list exclusive": {stmt(plain.GetSqlListExclusive("t", "/dir", "start", "p%", 10)), []interface{}{key.DirHash, "start", "/dir", "p%", int64(10)}},
"list inclusive": {stmt(plain.GetSqlListInclusive("t", "/dir", "start", "p%", 10)), []interface{}{key.DirHash, "start", "/dir", "p%", int64(10)}},
"scan page": {stmt(plain.GetSqlScanPage("t", key, 10)), []interface{}{key.DirHash, key.DirHash, "name", 10}},
} {
if !reflect.DeepEqual(tc.got.args, tc.want) {
t.Errorf("%s: args %v, want %v", name, tc.got.args, tc.want)
}
if n := strings.Count(tc.got.sql, "?"); n != len(tc.got.args) {
t.Errorf("%s: %d placeholders for %d args in %s", name, n, len(tc.got.args), tc.got.sql)
}
if name == "find batch" {
continue
}
for i, m := range regexp.MustCompile("(?:`(\\w+)` = )?\\?").FindAllStringSubmatch(tc.got.sql, -1) {
if want, ok := column[m[1]]; ok && i < len(tc.got.args) && !reflect.DeepEqual(tc.got.args[i], want) {
t.Errorf("%s: `%s` bound to %v, want %v", name, m[1], tc.got.args[i], want)
}
}
}
}