Skip to content

Instantly share code, notes, and snippets.

@MarkusH
Last active May 6, 2020 01:10
Show Gist options
  • Save MarkusH/76872d2463560d4cb62ed984cb8295b9 to your computer and use it in GitHub Desktop.
Save MarkusH/76872d2463560d4cb62ed984cb8295b9 to your computer and use it in GitHub Desktop.
# Copyright 2017, Markus Holtermann
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from django.db import connection
from django.db.models.indexes import Index
class FuncIndex(Index):
suffix = 'func'
def __init__(self, expression, name=None):
self.expression, self.name = expression, name
def create_sql(self, model, schema_editor):
compiler = connection.ops.compiler('SQLCompiler')(None, connection, 'default')
func_sql, params = compiler.compile(self.expression)
params = tuple(map(schema_editor.quote_value, params))
columns = func_sql % params
quote_name = schema_editor.quote_name
return schema_editor.sql_create_index % {
'table': quote_name(model._meta.db_table),
'name': quote_name(self.name),
'columns': columns,
'using': '',
'extra': '',
}
from django.db import models
from django.db.models.expressions import Ref, Value
from django.db.models.functions import Concat, Lower
from myapp.models import MyModel
with connection.schema_editor() as schema_editor:
func_index = FuncIndex(
Concat(
Lower(Ref('some_field', None), output_field=models.CharField()),
Value('blub'),
Lower(Ref('other_field', None), output_field=models.CharField()),
),
name='some_func_index'
)
print(func_index.create_sql(MyModel, schema_editor))
# CREATE INDEX "some_func_index" ON "testapp_mymodel" (COALESCE(LOWER("some_field"), '') || COALESCE(COALESCE('blub', '') || COALESCE(LOWER("other_field"), ''), ''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment