Skip to content

Commit

Permalink
fix: AUTO_RANDOM does not support define range before TiDB 6.3.0 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
wd0517 committed Oct 23, 2023
1 parent 1ad2675 commit 0f9fb43
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ Migrate from `AUTO_INCREMENT` to `AUTO_RANDOM`:

3. Finnaly, migrate it to `BigAutoRandomField(bigint)`.

> **Note**
>
> `AUTO_RANDOM` is supported after TiDB v3.1.0, and only support define with `range` after v6.3.0, so `range` will be ignored if TiDB version is lower than v6.3.0
### Using `AUTO_ID_CACHE`

[`AUTO_ID_CACHE`](https://docs.pingcap.com/tidb/stable/auto-increment#auto_id_cache) allow users to set the cache size for allocating the auto-increment ID, as you may know, TiDB guarantees that AUTO_INCREMENT values are monotonic (always increasing) on a per-server basis, but its value may appear to jump dramatically if an INSERT operation is performed against another TiDB Server, This is caused by the fact that each server has its own cache which is controlled by `AUTO_ID_CACHE`. But from TiDB v6.4.0, it introduces a centralized auto-increment ID allocating service, you can enable [*MySQL compatibility mode*](https://docs.pingcap.com/tidb/stable/auto-increment#mysql-compatibility-mode) by set `AUTO_ID_CACHE` to `1` when creating a table without losing performance.
Expand Down
5 changes: 0 additions & 5 deletions django_tidb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ class DatabaseWrapper(MysqlDatabaseWrapper):
introspection_class = DatabaseIntrospection
ops_class = DatabaseOperations

data_types = {
**MysqlDatabaseWrapper.data_types,
"BigAutoRandomField": "bigint AUTO_RANDOM(%(shard_bits)s, %(range)s)",
}

def get_database_version(self):
return self.tidb_version

Expand Down
11 changes: 11 additions & 0 deletions django_tidb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ def django_test_skips(self):
}
}
)
if self.connection.tidb_version < (6, 3):
skips.update(
{
"auto_random": {
"tidb.test_tidb_auto_random.TiDBAutoRandomMigrateTests"
".test_create_table_explicit_auto_random_field_with_shard_bits_and_range",
"tidb.test_tidb_auto_random.TiDBAutoRandomMigrateTests"
".test_create_table_explicit_auto_random_field_with_range",
}
}
)
if self.connection.tidb_version < (6, 6):
skips.update(
{
Expand Down
9 changes: 9 additions & 0 deletions django_tidb/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ def deconstruct(self):
if self.range is not None:
kwargs["range"] = self.range
return name, path, args, kwargs

def db_type(self, connection):
data = self.db_type_parameters(connection)
if connection.tidb_version < (6, 3):
# TiDB < 6.3 doesn't support define AUTO_RANDOM with range
data_type = "bigint AUTO_RANDOM(%(shard_bits)s)"
else:
data_type = "bigint AUTO_RANDOM(%(shard_bits)s, %(range)s)"
return data_type % data
12 changes: 9 additions & 3 deletions tests/test_tidb_auto_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,15 @@ def test_rel_db_type(self):
rel_db_type = field.rel_db_type(connection)
# Currently, We can't find a general way to get the auto_random info from the field.
self.assertEqual(rel_db_type, "bigint")
self.assertEqual(
self.rel_db_type_class().db_type(connection), "bigint AUTO_RANDOM(5, 64)"
)
if connection.tidb_version < (6, 3):
self.assertEqual(
self.rel_db_type_class().db_type(connection), "bigint AUTO_RANDOM(5)"
)
else:
self.assertEqual(
self.rel_db_type_class().db_type(connection),
"bigint AUTO_RANDOM(5, 64)",
)


AUTO_RANDOM_PATTERN = re.compile(
Expand Down

0 comments on commit 0f9fb43

Please sign in to comment.