|
1
|
|
|
from typing import List |
|
2
|
|
|
|
|
3
|
|
|
from django.db import models |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class HStoreIndex(models.Index): |
|
|
|
|
|
|
7
|
|
|
"""Allows creating a index on a specific HStore index. |
|
8
|
|
|
|
|
9
|
|
|
Note: pieces of code in this class have been copied |
|
10
|
|
|
from the base class. There was no way around this.""" |
|
11
|
|
|
|
|
12
|
|
|
def __init__(self, field: str, keys: List[str], unique: bool=False, |
|
13
|
|
|
name: str=''): |
|
14
|
|
|
"""Initializes a new instance of :see:HStoreIndex. |
|
15
|
|
|
|
|
16
|
|
|
Arguments: |
|
17
|
|
|
field: |
|
18
|
|
|
Name of the hstore field for |
|
19
|
|
|
which's keys to create a index for. |
|
20
|
|
|
|
|
21
|
|
|
keys: |
|
22
|
|
|
The name of the hstore keys to |
|
23
|
|
|
create the index on. |
|
24
|
|
|
|
|
25
|
|
|
unique: |
|
26
|
|
|
Whether this index should |
|
27
|
|
|
be marked as UNIQUE. |
|
28
|
|
|
|
|
29
|
|
|
name: |
|
30
|
|
|
The name of the index. If left |
|
31
|
|
|
empty, one will be generated. |
|
32
|
|
|
""" |
|
33
|
|
|
|
|
34
|
|
|
self.field = field |
|
35
|
|
|
self.keys = keys |
|
36
|
|
|
self.unique = unique |
|
37
|
|
|
|
|
38
|
|
|
# this will eventually set self.name |
|
39
|
|
|
super(HStoreIndex, self).__init__( |
|
40
|
|
|
fields=[field], |
|
41
|
|
|
name=name |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
def get_sql_create_template_values(self, model, schema_editor, using): |
|
45
|
|
|
"""Gets the values for the SQL template. |
|
46
|
|
|
|
|
47
|
|
|
Arguments: |
|
48
|
|
|
model: |
|
49
|
|
|
The model this index applies to. |
|
50
|
|
|
|
|
51
|
|
|
schema_editor: |
|
52
|
|
|
The schema editor to modify the schema. |
|
53
|
|
|
|
|
54
|
|
|
using: |
|
55
|
|
|
Optional: "USING" statement. |
|
56
|
|
|
|
|
57
|
|
|
Returns: |
|
58
|
|
|
Dictionary of keys to pass into the SQL template. |
|
59
|
|
|
""" |
|
60
|
|
|
|
|
61
|
|
|
fields = [model._meta.get_field(field_name) for field_name, order in self.fields_orders] |
|
62
|
|
|
tablespace_sql = schema_editor._get_index_tablespace_sql(model, fields) |
|
|
|
|
|
|
63
|
|
|
quote_name = schema_editor.quote_name |
|
64
|
|
|
|
|
65
|
|
|
columns = [ |
|
66
|
|
|
'(%s->\'%s\')' % (self.field, key) |
|
67
|
|
|
for key in self.keys |
|
68
|
|
|
] |
|
69
|
|
|
|
|
70
|
|
|
return { |
|
71
|
|
|
'table': quote_name(model._meta.db_table), |
|
72
|
|
|
'name': quote_name(self.name), |
|
73
|
|
|
'columns': ', '.join(columns), |
|
74
|
|
|
'using': using, |
|
75
|
|
|
'extra': tablespace_sql, |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
def create_sql(self, model, schema_editor, using=''): |
|
79
|
|
|
"""Gets the SQL to execute when creating the index. |
|
80
|
|
|
|
|
81
|
|
|
Arguments: |
|
82
|
|
|
model: |
|
83
|
|
|
The model this index applies to. |
|
84
|
|
|
|
|
85
|
|
|
schema_editor: |
|
86
|
|
|
The schema editor to modify the schema. |
|
87
|
|
|
|
|
88
|
|
|
using: |
|
89
|
|
|
Optional: "USING" statement. |
|
90
|
|
|
|
|
91
|
|
|
Returns: |
|
92
|
|
|
SQL string to execute to create this index. |
|
93
|
|
|
""" |
|
94
|
|
|
|
|
95
|
|
|
sql_create_index = schema_editor.sql_create_index |
|
96
|
|
|
if self.unique: |
|
97
|
|
|
sql_create_index = sql_create_index.replace('CREATE', 'CREATE UNIQUE') |
|
98
|
|
|
sql_parameters = self.get_sql_create_template_values(model, schema_editor, using) |
|
99
|
|
|
return sql_create_index % sql_parameters |
|
100
|
|
|
|
|
101
|
|
|
def remove_sql(self, model, schema_editor): |
|
102
|
|
|
"""Gets the SQL to execute to remove this index. |
|
103
|
|
|
|
|
104
|
|
|
Arguments: |
|
105
|
|
|
model: |
|
106
|
|
|
The model this index applies to. |
|
107
|
|
|
|
|
108
|
|
|
schema_editor: |
|
109
|
|
|
The schema editor to modify the schema. |
|
110
|
|
|
|
|
111
|
|
|
Returns: |
|
112
|
|
|
SQL string to execute to remove this index. |
|
113
|
|
|
""" |
|
114
|
|
|
quote_name = schema_editor.quote_name |
|
115
|
|
|
return schema_editor.sql_delete_index % { |
|
116
|
|
|
'table': quote_name(model._meta.db_table), |
|
117
|
|
|
'name': quote_name(self.name), |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
def deconstruct(self): |
|
121
|
|
|
"""Gets the values to pass to :see:__init__ when |
|
122
|
|
|
re-creating this object.""" |
|
123
|
|
|
|
|
124
|
|
|
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__) |
|
125
|
|
|
return (path, (), { |
|
126
|
|
|
'field': self.field, |
|
127
|
|
|
'keys': self.keys, |
|
128
|
|
|
'unique': self.unique, |
|
129
|
|
|
'name': self.name |
|
130
|
|
|
}) |
|
131
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.