-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
160 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2021 PingCAP, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from django.db.models.functions import Chr | ||
from django.db.models import options | ||
from django.db.migrations import state | ||
|
||
|
||
def char(self, compiler, connection, **extra_context): | ||
# TiDB doesn't support utf16 | ||
return self.as_sql( | ||
compiler, | ||
connection, | ||
function="CHAR", | ||
template="%(function)s(%(expressions)s USING utf8mb4)", | ||
**extra_context, | ||
) | ||
|
||
|
||
def patch_model_functions(): | ||
Chr.as_mysql = char | ||
|
||
|
||
def patch_model_options(): | ||
# Patch `tidb_auto_id_cache` to options.DEFAULT_NAMES, | ||
# so that user can define it in model's Meta class. | ||
options.DEFAULT_NAMES += ("tidb_auto_id_cache",) | ||
# Because Django named import DEFAULT_NAMES in migrations, | ||
# so we need to patch it again here. | ||
# Django will record `tidb_auto_id_cache` in migration files, | ||
# and then restore it when applying migrations. | ||
state.DEFAULT_NAMES += ("tidb_auto_id_cache",) | ||
|
||
|
||
def monkey_patch(): | ||
patch_model_functions() | ||
patch_model_options() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import re | ||
|
||
from django.db import models, connection | ||
from django.db.utils import ProgrammingError | ||
from django.test import TransactionTestCase | ||
from django.test.utils import isolate_apps | ||
|
||
|
||
AUTO_ID_CACHE_PATTERN = re.compile(r"\/\*T!\[auto_id_cache\] AUTO_ID_CACHE=(\d+) \*\/") | ||
|
||
|
||
class TiDBAutoIDCacheTests(TransactionTestCase): | ||
available_apps = ["tidb"] | ||
|
||
def get_auto_id_cache_info(self, table): | ||
with connection.cursor() as cursor: | ||
cursor.execute( | ||
# It seems that SHOW CREATE TABLE is the only way to get the auto_random info. | ||
# Use parameterized query will add quotes to the table name, which will cause syntax error. | ||
f"SHOW CREATE TABLE {table}", | ||
) | ||
row = cursor.fetchone() | ||
if row is None: | ||
return None | ||
match = AUTO_ID_CACHE_PATTERN.search(row[1]) | ||
if match: | ||
return match.groups()[0] | ||
return None | ||
|
||
@isolate_apps("tidb") | ||
def test_create_table_with_tidb_auto_id_cache_1(self): | ||
class AutoIDCacheNode1(models.Model): | ||
title = models.CharField(max_length=255) | ||
|
||
class Meta: | ||
app_label = "tidb" | ||
tidb_auto_id_cache = 1 | ||
|
||
with connection.schema_editor() as editor: | ||
editor.create_model(AutoIDCacheNode1) | ||
self.assertEqual( | ||
self.get_auto_id_cache_info(AutoIDCacheNode1._meta.db_table), "1" | ||
) | ||
|
||
@isolate_apps("tidb") | ||
def test_create_table_with_tidb_auto_id_cache_non_1(self): | ||
class AutoIDCacheNode2(models.Model): | ||
title = models.CharField(max_length=255) | ||
|
||
class Meta: | ||
app_label = "tidb" | ||
tidb_auto_id_cache = 10 | ||
|
||
with connection.schema_editor() as editor: | ||
editor.create_model(AutoIDCacheNode2) | ||
self.assertEqual( | ||
self.get_auto_id_cache_info(AutoIDCacheNode2._meta.db_table), "10" | ||
) | ||
|
||
@isolate_apps("tidb") | ||
def test_create_table_with_invalid_tidb_auto_id_cache(self): | ||
class AutoIDCacheNode3(models.Model): | ||
title = models.CharField(max_length=255) | ||
|
||
class Meta: | ||
app_label = "tidb" | ||
tidb_auto_id_cache = "invalid" | ||
|
||
with self.assertRaises(ProgrammingError): | ||
with connection.schema_editor() as editor: | ||
editor.create_model(AutoIDCacheNode3) | ||
|
||
@isolate_apps("tidb") | ||
def test_create_table_without_tidb_auto_id_cache(self): | ||
class AutoIDCacheNode4(models.Model): | ||
title = models.CharField(max_length=255) | ||
|
||
class Meta: | ||
app_label = "tidb" | ||
|
||
with connection.schema_editor() as editor: | ||
editor.create_model(AutoIDCacheNode4) | ||
self.assertIsNone(self.get_auto_id_cache_info(AutoIDCacheNode4._meta.db_table)) |