@@ -1609,68 +1609,32 @@ func TestCollation(t *testing.T) {
16091609 t .Skipf ("MySQL server not running on %s" , netAddr )
16101610 }
16111611
1612- defaultCollation := "utf8mb4_general_ci"
1612+ // MariaDB may override collation specified by handshake with `character_set_collations` variable.
1613+ // https://mariadb.com/kb/en/setting-character-sets-and-collations/#changing-default-collation
1614+ // https://mariadb.com/kb/en/server-system-variables/#character_set_collations
1615+ // utf8mb4_general_ci, utf8mb3_general_ci will be overridden by default MariaDB.
1616+ // Collations other than charasets default are not overridden. So utf8mb4_unicode_ci is safe.
16131617 testCollations := []string {
1614- "" , // do not set
1615- defaultCollation , // driver default
16161618 "latin1_general_ci" ,
16171619 "binary" ,
16181620 "utf8mb4_unicode_ci" ,
16191621 "cp1257_bin" ,
16201622 }
16211623
16221624 for _ , collation := range testCollations {
1623- var expected , tdsn string
1624- if collation != "" {
1625- tdsn = dsn + "&collation=" + collation
1626- expected = collation
1627- } else {
1628- tdsn = dsn
1629- expected = defaultCollation
1630- }
1631-
1632- runTests (t , tdsn , func (dbt * DBTest ) {
1633- // see https://mariadb.com/kb/en/setting-character-sets-and-collations/#changing-default-collation
1634- // when character_set_collations is set for the charset, it overrides the default collation
1635- // so we need to check if the default collation is overridden
1636- forceExpected := expected
1637- var defaultCollations string
1638- err := dbt .db .QueryRow ("SELECT @@character_set_collations" ).Scan (& defaultCollations )
1639- if err == nil {
1640- // Query succeeded, need to check if we should override expected collation
1641- collationMap := make (map [string ]string )
1642- pairs := strings .Split (defaultCollations , "," )
1643- for _ , pair := range pairs {
1644- parts := strings .Split (pair , "=" )
1645- if len (parts ) == 2 {
1646- collationMap [parts [0 ]] = parts [1 ]
1647- }
1648- }
1625+ t .Run (collation , func (t * testing.T ) {
1626+ tdsn := dsn + "&collation=" + collation
1627+ expected := collation
16491628
1650- // Get charset prefix from expected collation
1651- parts := strings .Split (expected , "_" )
1652- if len (parts ) > 0 {
1653- charset := parts [0 ]
1654- if newCollation , ok := collationMap [charset ]; ok {
1655- forceExpected = newCollation
1656- }
1629+ runTests (t , tdsn , func (dbt * DBTest ) {
1630+ var got string
1631+ if err := dbt .db .QueryRow ("SELECT @@collation_connection" ).Scan (& got ); err != nil {
1632+ dbt .Fatal (err )
16571633 }
1658- }
1659-
1660- var got string
1661- if err := dbt .db .QueryRow ("SELECT @@collation_connection" ).Scan (& got ); err != nil {
1662- dbt .Fatal (err )
1663- }
1664-
1665- if got != expected {
1666- if forceExpected != expected {
1667- if got != forceExpected {
1668- dbt .Fatalf ("expected forced connection collation %s but got %s" , forceExpected , got )
1669- }
1670- } else {
1634+ if got != expected {
16711635 dbt .Fatalf ("expected connection collation %s but got %s" , expected , got )
16721636 }
1673- }
1637+ })
16741638 })
16751639 }
16761640}
0 commit comments