You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 30, 2019. It is now read-only.
If you're not familiar with django Migrations, please check `Migrations Documentation <https://docs.djangoproject.com/en/1.7/topics/migrations/>`_ 1st.
8
+
9
+
10
+
.. important::
11
+
12
+
The order of Migrations.operations matters, you can only apply a index to a field if a field exists, if you delete a model/field you should remove any operation first before removing model/field.
13
+
14
+
15
+
Creating
16
+
--------
17
+
18
+
Trigger
19
+
*******
20
+
21
+
For creating of trigger is provided :class:`~pg_fts.migrations.CreateFTSTriggerOperation`.
The trigger only updates the :class:`~pg_fts.fields.TSVectorField` if data is changed in the fields that is indexing, with it's weight (default is 'D') and language.
39
+
40
+
Example for this model::
41
+
42
+
class Article(models.Model):
43
+
title = models.CharField(max_length=255)
44
+
article = models.TextField()
45
+
fts = TSVectorField(
46
+
(('title', 'A'), 'article'),
47
+
dictionary='portuguese'
48
+
)
49
+
50
+
In Migrations::
51
+
52
+
class Migration(migrations.Migration)
53
+
dependencies = [
54
+
('article', '00XX_create_fts_field'),
55
+
]
56
+
57
+
operations = [
58
+
CreateFTSTriggerOperation(
59
+
name='Article',
60
+
fts_vector='fts',
61
+
),
62
+
]
63
+
64
+
Will create this trigger:
65
+
66
+
.. code-block:: sql
67
+
68
+
CREATE FUNCTION article_article_fts_update() RETURNS TRIGGER AS $$
CREATE TRIGGER article_article_fts_update BEFORE INSERT OR UPDATE ON article_article
82
+
FOR EACH ROW EXECUTE PROCEDURE article_article_fts_update();
83
+
84
+
85
+
.. important::
86
+
87
+
Trigger will only work for future changes, for existing data use :class:`~pg_fts.migrations.UpdateVectorOperation`.
88
+
89
+
Index
90
+
*****
91
+
92
+
For creating of indexes is provided :class:`~pg_fts.migrations.CreateFTSIndexOperation`.
93
+
94
+
``name``
95
+
96
+
.. attribute:: CreateFTSIndexOperation.name
97
+
98
+
Name of the model
99
+
100
+
``fts_vector``
101
+
102
+
.. attribute:: CreateFTSIndexOperation.fts_vector
103
+
104
+
Name of the :class:`~pg_fts.fields.TSVectorField`
105
+
106
+
107
+
``index``
108
+
109
+
.. attribute:: CreateFTSIndexOperation.index
110
+
111
+
Options:
112
+
113
+
- ``gin`` GIN (Generalized Inverted Index)-based index. Faster to build, slower to lookup.
114
+
115
+
- ``gist`` GiST (Generalized Search Tree)-based index. Faster to lookup, slower to build.
116
+
117
+
For information about the ``gin`` and ``gist`` indexes types consult :pg_docs:`PostgreSQL documentation 12.9. GiST and GIN Index Types <textsearch-indexes.html>`
118
+
119
+
120
+
Migrating from existing application
121
+
-----------------------------------
122
+
123
+
For existing application with data is provided :class:`~pg_fts.migrations.UpdateVectorOperation`, this will update the vector.
124
+
125
+
``name``
126
+
127
+
.. attribute:: UpdateVectorOperation.name
128
+
129
+
Name of the model
130
+
131
+
``fts_vector``
132
+
133
+
.. attribute:: UpdateVectorOperation.fts_vector
134
+
135
+
Name of the :class:`~pg_fts.fields.TSVectorField`
136
+
137
+
138
+
Changing and Removing
139
+
---------------------
140
+
141
+
Changing Index
142
+
**************
143
+
144
+
If you have a existing index created by :class:`~pg_fts.migrations.CreateFTSIndexOperation` of type ``gin`` to migrate for ``gist`` you have to 1st remove the existing index with :class:`~pg_fts.migrations.DeleteFTSIndexOperation` and create a of type ``gist`` with :class:`~pg_fts.migrations.CreateFTSIndexOperation`.
145
+
146
+
Example::
147
+
148
+
class Migration(migrations.Migration)
149
+
dependencies = [
150
+
('article', '0003_fts_create_index_trigger'),
151
+
]
152
+
153
+
operations = [
154
+
DeleteFTSIndexOperation(
155
+
name='Article',
156
+
fts_vector='fts_index',
157
+
index='gin'
158
+
),
159
+
CreateFTSIndexOperation(
160
+
name='Article',
161
+
fts_vector='fts_index',
162
+
index='gist'
163
+
),
164
+
]
165
+
166
+
Alterations on :class:`~pg_fts.fields.TSVectorField`
If you change :class:`~pg_fts.fields.TSVectorField` is fields, ranks or dictionary you have to:
170
+
171
+
1. remove the trigger with :class:`~pg_fts.migrations.DeleteFTSTriggerOperation` and only after you can create
172
+
173
+
2. a new trigger with :class:`~pg_fts.migrations.CreateFTSTriggerOperation`.
174
+
175
+
For updating :class:`~pg_fts.fields.TSVectorField` run :class:`~pg_fts.migrations.UpdateVectorOperation`.
176
+
177
+
.. hint::
178
+
179
+
If the fields are the same (fields and rank) but you are updating to multiple dictionaries, for efficiency, keep the previous dictionary as default, as the lexemes and weight will be the same in :class:`~pg_fts.fields.TSVectorField`.
180
+
There is no need to run :class:`~pg_fts.migrations.UpdateVectorOperation`
181
+
182
+
Removing Index
183
+
**************
184
+
185
+
For removing the index is provided :class:`~pg_fts.migrations.DeleteFTSIndexOperation`.
186
+
187
+
``name``
188
+
189
+
.. attribute:: DeleteFTSIndexOperation.name
190
+
191
+
Name of the model
192
+
193
+
``fts_vector``
194
+
195
+
.. attribute:: DeleteFTSIndexOperation.fts_vector
196
+
197
+
Name of the :class:`~pg_fts.fields.TSVectorField`
198
+
199
+
The previous index type, important for regressions.
200
+
201
+
``index``
202
+
203
+
.. attribute:: CreateFTSIndexOperation.index
204
+
205
+
206
+
Removing Trigger
207
+
****************
208
+
209
+
For removing the index is provided :class:`~pg_fts.migrations.DeleteFTSTriggerOperation`.
>>> Article.objects.create(title='Django', article='is awesome, made in python, multiple databases support, it has a ORM, class based views, template layer')
176
+
>>> Article.objects.create(title='django', article='is awesome, made in python, multiple databases support, it has a ORM, class based views, template layer')
177
177
>>> Article.objects.create(title='Wordpress', article="what a pain, made in PHP, it's ok if you just add a template and some plugins")
178
178
>>> Article.objects.create(title='Javascript', article='A functional language, with c syntax. The braces nightmare')
>>> ArticleMulti.objects.create(title='Django', article='is awesome, made in python', dictionary='english')
318
+
>>> ArticleMulti.objects.create(title='django', article='is awesome, made in python', dictionary='english')
319
319
>>> ArticleMulti.objects.create(title='Wordpress', article="what a pain, made in PHP, it's ok if you just add a template and some plugins")
320
320
>>> ArticleMulti.objects.create(title='Javascript', article='A functional dictionary, with c syntax. The braces nightmare', dictionary='english')
321
321
## Portuguese
322
322
>>> ArticleMulti.objects.create(title='PHP', article='que dor, o pior do c, c++ e perl tudo junto para ser a coisa mais estupida', dictionary='portuguese')
>>> ArticleMulti.objects.create(title='Django', article='é Altamente, feito em python', dictionary='portuguese')
324
+
>>> ArticleMulti.objects.create(title='django', article='é Altamente, feito em python', dictionary='portuguese')
325
325
>>> ArticleMulti.objects.create(title='Wordpress', article="que dor, feito em PHP, não é mau para quem usa os templates e plugins")
326
326
>>> ArticleMulti.objects.create(title='Javascript', article='Uma linguagem funcional, mas tem sintaxe c para confundir. O pesadelo das chavetas', dictionary='portuguese')
@@ -360,8 +360,8 @@ reverse migration to 0001 so does not include ArticleMulti::
360
360
Delete the 0002 migration, remove ArticleMulti from models.py and add / change Article to::
361
361
362
362
class Article(models.Model):
363
-
title = models.CharField(max_length=255)
364
-
article = models.TextField()
363
+
title = models.CharField(max_length=255)
364
+
article = models.TextField()
365
365
366
366
dictionary = models.CharField(
367
367
max_length=15,
@@ -377,11 +377,11 @@ Delete the 0002 migration, remove ArticleMulti from models.py and add / change A
377
377
def __str__(self):
378
378
return self.title
379
379
380
-
Let Django find the model alterations for us::
380
+
Let django find the model alterations for us::
381
381
382
382
python manage.py makemigrations article
383
383
384
-
But we have to edit the migrations 0002 file before applying and add to operations :class:`~pg_fts.migrations.DeleteFTSTriggerOperation` and :class:`~pg_fts.migrations.DeleteFTSIndexOperation` **before** Django auto migrations, and **at the end** of operations the :class:`~pg_fts.migrations.CreateFTSIndexOperation` and :class:`~pg_fts.migrations.CreateFTSTriggerOperation`.
384
+
But we have to edit the migrations 0002 file before applying and add to operations :class:`~pg_fts.migrations.DeleteFTSTriggerOperation` and :class:`~pg_fts.migrations.DeleteFTSIndexOperation` **before** django auto migrations, and **at the end** of operations the :class:`~pg_fts.migrations.CreateFTSIndexOperation` and :class:`~pg_fts.migrations.CreateFTSTriggerOperation`.
385
385
386
386
The migrations 0002 file should be like this::
387
387
@@ -414,7 +414,7 @@ The migrations 0002 file should be like this::
414
414
fts_vector='fts_index',
415
415
index='gin'
416
416
),
417
-
# the Django created changes
417
+
# the django created changes
418
418
migrations.AddField(
419
419
model_name='article',
420
420
name='dictionary',
@@ -444,8 +444,8 @@ The migrations 0002 file should be like this::
444
444
445
445
Pay special attention to the order of creation and deleting.
446
446
447
-
You can only apply :class:`~pg_fts.migrations.CreateFTSIndexOperation` and :class:`~pg_fts.migrations.CreateFTSTriggerOperation` after Django created operations.
447
+
You can only apply :class:`~pg_fts.migrations.CreateFTSIndexOperation` and :class:`~pg_fts.migrations.CreateFTSTriggerOperation` after django created operations.
448
448
449
-
The :class:`~pg_fts.migrations.DeleteFTSTriggerOperation` and :class:`~pg_fts.migrations.DeleteFTSIndexOperation` before Django removing/altering operations
449
+
The :class:`~pg_fts.migrations.DeleteFTSTriggerOperation` and :class:`~pg_fts.migrations.DeleteFTSIndexOperation` before django removing/altering operations
0 commit comments