@@ -1609,68 +1609,32 @@ func TestCollation(t *testing.T) {
1609
1609
t .Skipf ("MySQL server not running on %s" , netAddr )
1610
1610
}
1611
1611
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.
1613
1617
testCollations := []string {
1614
- "" , // do not set
1615
- defaultCollation , // driver default
1616
1618
"latin1_general_ci" ,
1617
1619
"binary" ,
1618
1620
"utf8mb4_unicode_ci" ,
1619
1621
"cp1257_bin" ,
1620
1622
}
1621
1623
1622
1624
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
1649
1628
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 )
1657
1633
}
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 {
1671
1635
dbt .Fatalf ("expected connection collation %s but got %s" , expected , got )
1672
1636
}
1673
- }
1637
+ })
1674
1638
})
1675
1639
}
1676
1640
}
0 commit comments