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