|
1
|
1 |
|
import importlib |
|
2
|
1 |
|
import logging |
|
3
|
|
|
|
|
4
|
1 |
|
from django.conf import settings |
|
|
|
|
|
|
5
|
1 |
|
from django.core.exceptions import ImproperlyConfigured |
|
|
|
|
|
|
6
|
1 |
|
from django.db import ProgrammingError |
|
|
|
|
|
|
7
|
1 |
|
from django.db.backends.postgresql.base import \ |
|
|
|
|
|
|
8
|
|
|
DatabaseWrapper as Psycopg2DatabaseWrapper |
|
9
|
|
|
|
|
10
|
1 |
|
from .hstore_unique import HStoreUniqueSchemaEditorMixin |
|
11
|
1 |
|
from .hstore_required import HStoreRequiredSchemaEditorMixin |
|
12
|
|
|
|
|
13
|
1 |
|
logger = logging.getLogger(__name__) |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
1 |
|
def _get_backend_base(): |
|
17
|
|
|
"""Gets the base class for the custom database back-end. |
|
18
|
|
|
|
|
19
|
|
|
This should be the Django PostgreSQL back-end. However, |
|
20
|
|
|
some people are already using a custom back-end from |
|
21
|
|
|
another package. We are nice people and expose an option |
|
22
|
|
|
that allows them to configure the back-end we base upon. |
|
23
|
|
|
|
|
24
|
|
|
As long as the specified base eventually also has |
|
25
|
|
|
the PostgreSQL back-end as a base, then everything should |
|
26
|
|
|
work as intended. |
|
27
|
|
|
""" |
|
28
|
1 |
|
base_class_name = getattr( |
|
29
|
|
|
settings, |
|
30
|
|
|
'POSTGRES_EXTRA_DB_BACKEND_BASE', |
|
31
|
|
|
'django.db.backends.postgresql' |
|
32
|
|
|
) |
|
33
|
|
|
|
|
34
|
1 |
|
base_class_module = importlib.import_module(base_class_name + '.base') |
|
35
|
1 |
|
base_class = getattr(base_class_module, 'DatabaseWrapper', None) |
|
36
|
|
|
|
|
37
|
1 |
|
if not base_class: |
|
38
|
|
|
raise ImproperlyConfigured(( |
|
39
|
|
|
'\'%s\' is not a valid database back-end.' |
|
40
|
|
|
' The module does not define a DatabaseWrapper class.' |
|
41
|
|
|
' Check the value of POSTGRES_EXTRA_DB_BACKEND_BASE.' |
|
42
|
|
|
) % base_class_name) |
|
43
|
|
|
|
|
44
|
1 |
|
if isinstance(base_class, Psycopg2DatabaseWrapper): |
|
45
|
|
|
raise ImproperlyConfigured(( |
|
46
|
|
|
'\'%s\' is not a valid database back-end.' |
|
47
|
|
|
' It does inherit from the PostgreSQL back-end.' |
|
48
|
|
|
' Check the value of POSTGRES_EXTRA_DB_BACKEND_BASE.' |
|
49
|
|
|
) % base_class_name) |
|
50
|
|
|
|
|
51
|
1 |
|
return base_class |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
1 |
|
def _get_schema_editor_base(): |
|
55
|
|
|
"""Gets the base class for the schema editor. |
|
56
|
|
|
|
|
57
|
|
|
We have to use the configured base back-end's |
|
58
|
|
|
schema editor for this.""" |
|
59
|
|
|
|
|
60
|
1 |
|
return _get_backend_base().SchemaEditorClass |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
1 |
|
class SchemaEditor(_get_schema_editor_base()): |
|
64
|
|
|
"""Custom schema editor, see mixins for implementation.""" |
|
65
|
|
|
|
|
66
|
1 |
|
post_processing_mixins = [ |
|
67
|
|
|
HStoreUniqueSchemaEditorMixin(), |
|
68
|
|
|
HStoreRequiredSchemaEditorMixin() |
|
69
|
|
|
] |
|
70
|
|
|
|
|
71
|
1 |
|
def __init__(self, connection, collect_sql=False, atomic=True): |
|
72
|
1 |
|
super(SchemaEditor, self).__init__( |
|
73
|
|
|
connection, collect_sql, atomic) |
|
74
|
|
|
|
|
75
|
1 |
|
self.base = super(SchemaEditor, self) |
|
76
|
|
|
|
|
77
|
1 |
|
for mixin in self.post_processing_mixins: |
|
78
|
1 |
|
mixin.execute = self.execute |
|
|
|
|
|
|
79
|
1 |
|
mixin.quote_name = self.quote_name |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
1 |
|
def create_model(self, model): |
|
82
|
|
|
"""Ran when a new model is created.""" |
|
83
|
|
|
|
|
84
|
1 |
|
super().create_model(model) |
|
85
|
|
|
|
|
86
|
1 |
|
for mixin in self.post_processing_mixins: |
|
87
|
1 |
|
mixin.create_model(model) |
|
88
|
|
|
|
|
89
|
1 |
|
def delete_model(self, model): |
|
90
|
|
|
"""Ran when a model is being deleted.""" |
|
91
|
|
|
|
|
92
|
1 |
|
for mixin in self.post_processing_mixins: |
|
93
|
1 |
|
mixin.delete_model(model) |
|
94
|
|
|
|
|
95
|
1 |
|
super().delete_model(model) |
|
96
|
|
|
|
|
97
|
1 |
|
def alter_db_table(self, model, old_db_table, new_db_table): |
|
98
|
|
|
"""Ran when the name of a model is changed.""" |
|
99
|
|
|
|
|
100
|
1 |
|
super(SchemaEditor, self).alter_db_table( |
|
101
|
|
|
model, old_db_table, new_db_table |
|
102
|
|
|
) |
|
103
|
|
|
|
|
104
|
1 |
|
for mixin in self.post_processing_mixins: |
|
105
|
1 |
|
mixin.alter_db_table( |
|
106
|
|
|
model, |
|
107
|
|
|
old_db_table, |
|
108
|
|
|
new_db_table |
|
109
|
|
|
) |
|
110
|
|
|
|
|
111
|
1 |
|
def add_field(self, model, field): |
|
112
|
|
|
"""Ran when a field is added to a model.""" |
|
113
|
|
|
|
|
114
|
1 |
|
super(SchemaEditor, self).add_field(model, field) |
|
115
|
|
|
|
|
116
|
1 |
|
for mixin in self.post_processing_mixins: |
|
117
|
1 |
|
mixin.add_field(model, field) |
|
118
|
|
|
|
|
119
|
1 |
|
def remove_field(self, model, field): |
|
120
|
|
|
"""Ran when a field is removed from a model.""" |
|
121
|
|
|
|
|
122
|
1 |
|
for mixin in self.post_processing_mixins: |
|
123
|
1 |
|
mixin.remove_field(model, field) |
|
124
|
|
|
|
|
125
|
1 |
|
super(SchemaEditor, self).remove_field(model, field) |
|
126
|
|
|
|
|
127
|
1 |
|
def alter_field(self, model, old_field, new_field, strict=False): |
|
128
|
|
|
"""Ran when the configuration on a field changed.""" |
|
129
|
|
|
|
|
130
|
1 |
|
super(SchemaEditor, self).alter_field( |
|
131
|
|
|
model, old_field, new_field, strict |
|
132
|
|
|
) |
|
133
|
|
|
|
|
134
|
1 |
|
for mixin in self.post_processing_mixins: |
|
135
|
1 |
|
mixin.alter_field( |
|
136
|
|
|
model, old_field, new_field, strict |
|
137
|
|
|
) |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
1 |
|
class DatabaseWrapper(_get_backend_base()): |
|
|
|
|
|
|
141
|
|
|
"""Wraps the standard PostgreSQL database back-end. |
|
142
|
|
|
|
|
143
|
|
|
Overrides the schema editor with our custom |
|
144
|
|
|
schema editor and makes sure the `hstore` |
|
145
|
|
|
extension is enabled.""" |
|
146
|
|
|
|
|
147
|
1 |
|
SchemaEditorClass = SchemaEditor |
|
148
|
|
|
|
|
149
|
1 |
|
def prepare_database(self): |
|
150
|
|
|
"""Ran to prepare the configured database. |
|
151
|
|
|
|
|
152
|
|
|
This is where we enable the `hstore` extension |
|
153
|
|
|
if it wasn't enabled yet.""" |
|
154
|
|
|
|
|
155
|
1 |
|
super().prepare_database() |
|
156
|
1 |
|
with self.cursor() as cursor: |
|
|
|
|
|
|
157
|
1 |
|
try: |
|
158
|
1 |
|
cursor.execute('CREATE EXTENSION IF NOT EXISTS hstore') |
|
159
|
|
|
except ProgrammingError: # permission denied |
|
160
|
|
|
logger.warning( |
|
161
|
|
|
'Failed to create "hstore" extension. ' |
|
162
|
|
|
'Tables with hstore columns may fail to migrate. ' |
|
163
|
|
|
'If hstore is needed, make sure you are connected ' |
|
164
|
|
|
'to the database as a superuser ' |
|
165
|
|
|
'or add the extension manually.', |
|
166
|
|
|
exc_info=True) |
|
167
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.