Remove get_engine
This only affects add_index, so move engine picking code into that function.
This commit is contained in:
Родитель
4faa5a629b
Коммит
878804222b
|
@ -1,21 +1,20 @@
|
|||
# Generated by Django 3.2.20 on 2023-11-02 14:22
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from django.apps.registry import Apps
|
||||
from django.db import migrations
|
||||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
||||
|
||||
|
||||
Engine = Literal["postgres", "sqlite"]
|
||||
|
||||
|
||||
def add_index(
|
||||
schema_editor: BaseDatabaseSchemaEditor,
|
||||
engine: Engine,
|
||||
index_name: str,
|
||||
table_name: str,
|
||||
schema_editor: BaseDatabaseSchemaEditor, index_name: str, table_name: str
|
||||
) -> None:
|
||||
if schema_editor.connection.vendor.startswith("postgres"):
|
||||
engine = "postgres"
|
||||
elif schema_editor.connection.vendor.startswith("sqlite"):
|
||||
engine = "sqlite"
|
||||
else:
|
||||
raise Exception(f'Unknown database vendor "{schema_editor.connection.vendor}"')
|
||||
|
||||
if_not_exists = "IF NOT EXISTS" if engine == "postgres" else ""
|
||||
schema_editor.execute(
|
||||
f"CREATE INDEX {if_not_exists} {index_name} ON {table_name} (upper(email));"
|
||||
|
@ -26,24 +25,10 @@ def drop_index(schema_editor: BaseDatabaseSchemaEditor, index_name: str) -> None
|
|||
schema_editor.execute(f"DROP INDEX {index_name};")
|
||||
|
||||
|
||||
def get_engine(schema_editor) -> Engine:
|
||||
if schema_editor.connection.vendor.startswith("postgres"):
|
||||
return "postgres"
|
||||
elif schema_editor.connection.vendor.startswith("sqlite"):
|
||||
return "sqlite"
|
||||
raise Exception(f'Unknown database vendor "{schema_editor.connection.vendor}"')
|
||||
|
||||
|
||||
def add_account_email_index(
|
||||
apps: Apps, schema_editor: BaseDatabaseSchemaEditor
|
||||
) -> None:
|
||||
engine = get_engine(schema_editor)
|
||||
add_index(
|
||||
schema_editor,
|
||||
engine,
|
||||
"account_emailaddress_email_upper",
|
||||
"account_emailaddress",
|
||||
)
|
||||
add_index(schema_editor, "account_emailaddress_email_upper", "account_emailaddress")
|
||||
|
||||
|
||||
def drop_account_email_index(
|
||||
|
@ -53,8 +38,7 @@ def drop_account_email_index(
|
|||
|
||||
|
||||
def add_auth_email_index(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
||||
engine = get_engine(schema_editor)
|
||||
add_index(schema_editor, engine, "auth_user_email_upper", "auth_user")
|
||||
add_index(schema_editor, "auth_user_email_upper", "auth_user")
|
||||
|
||||
|
||||
def drop_auth_email_index(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
||||
|
|
Загрузка…
Ссылка в новой задаче