# [Critical bug] new collation 关键错误

## Issue

ONCALL-2789 在内部集群中，我们发现了 5.0.0 版本上的 new collation 存在重大 bug， 会导致数据索引不一致的问题。 <https://github.com/pingcap/tidb/issues/24029>

```sql
mysql> create table t(a int key, b varchar(20) collate utf8mb4_unicode_ci, c varchar(20) collate utf8mb4_general_ci, unique key idx_b_c(b, c));
Query OK, 0 rows affected (0.01 sec)

restart tidb

mysql> insert into t values (4, 'd', 'F');
Query OK, 1 row affected (0.01 sec)

mysql> admin check table t;
ERROR 1105 (HY000): col b, handle 4, index:types.Datum{k:0x5, decimal:0x0, length:0x0, i:0, collation:"utf8mb4_unicode_ci", b:[]uint8{0xe, 0x6d}, x:interface {}(nil)} != record:types.Datum{k:0x5, decimal:0x0, length:0x0, i:0, collation:"utf8mb4_unicode_ci", b:[]uint8{0x64}, x:interface {}(nil)}
```

## Root Cause

TiDB 启动时，会对表中的每个索引设置是否需要 restored data (多数情况下，涉及 collation 时为 true)。在一个重构 PR 中，加上了 collate.NewCollationEnabled() (用于检查集群是否打开 new collation) 的检查。但是在启动 TiDB 时，由于还未初始化 newCollationEnabled 变量，因此错误地认为索引不需要 restored data。这会导致索引当成没有打开 new collation 的情况处理，写入错误的索引。

## Diagnostic Steps

影响范围: v5.0.0 触发条件：打开 new collation (4.0 到 5.0 版本中，K8s 部署的新集群默认打开，其他情况默认关闭），重启 TiDB。

## Resolution

1. Upgrade to the new version, delete and rebuild the wrong index.
2. TIDB: PR <https://github.com/pingcap/tidb/pull/24030>

## Workaround

N/A