django-postpone-index 0.0.2__py3-none-any.whl → 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: django-postpone-index
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Postpone index creation to provide Zero Downtime Migration feature
5
5
  Home-page: https://github.com/nnseva/django-postpone-index
6
6
  Author: Vsevolod Novikov
@@ -169,6 +169,43 @@ calling the `apply_postponed` migration command again. All not-applied indexes w
169
169
  **NOTICE** the `apply_postponed` management command doesn't have any explicit locking mechanics. Avoid starting this
170
170
  command concurrently with itself or another `migrate` command on the same database.
171
171
 
172
+ ## Intermediate migration state
173
+
174
+ Apart from standard Django migrations, using the `postpone_index` package leads to the *intermediate migration state*
175
+ after the `migrate` management command finished:
176
+
177
+ - new model structure is applied
178
+ - indexes to be deleted are deleted
179
+ - indexes to be created are *not created yet*
180
+
181
+ You should be aware that if you introduce a new unique index or constraint, the database does not control uniqueness
182
+ based on not yet created indexes at this time.
183
+
184
+ Your code works now as expected everywhere, except the code which is based on new unique constraints introduced in applied migrations.
185
+
186
+ Apply the `apply_postponed run` management command to make these new indexes work.
187
+
188
+ Any error while `apply_postponed run` execution is stored in the `PostponedSQL` model instance.
189
+
190
+ You can see erroneous lines using `apply_postponed list` command. See the `[E]` mark at the start of the line.
191
+
192
+ You also can see the error details using the format parameter of the `apply_postponed list -f '... %(error)s'` management command.
193
+
194
+ The `apply_postponed run -x` breaks execution on any error. You can see the error in the standard error or logging streams.
195
+
196
+ The `apply_postponed run` (without `-x` parameter) doesn't stop on error, but outputs warning to the log stream instead.
197
+
198
+ When the error happened, it most probably is caused by the non-unique records. Fix the data and try to execute
199
+ `apply_postponed run` again to create an index.
200
+
201
+ After the successfull `apply_postponed run` execution, the migration state is finalised to be equal as if you applied the migration
202
+ without `postpone_index` package at all.
203
+
204
+ The `apply_postponed run` management command marks all successfully executed `PostponedSQL` instances as `done`. You can see `[X]` mark
205
+ at the start of the line produced by `apply_ponsponed list` management command.
206
+
207
+ You can cleanup `done` instances using `apply_postponed cleanup` management command. This step is optional.
208
+
172
209
  ## Django testing
173
210
 
174
211
  Django migrates testing database before tests. Always use `POSTPONE_INDEX_IGNORE = True` settings to avoid postpone index
@@ -1,9 +1,9 @@
1
1
  postpone_index/__init__.py,sha256=s1NgdtOn7SO7scs-uAaq1134F7Luh-JjB5Ghra3D2fQ,84
2
- postpone_index/_version.py,sha256=huLsL1iGeXWQKZ8bjwDdIWC7JOkj3wnzBh-HFMZl1PY,704
2
+ postpone_index/_version.py,sha256=pBZsQt6tlL02W-ri--X_4JCubpAK7jjCSnOmUp_isjc,704
3
3
  postpone_index/admin.py,sha256=TAZxaciQkEBNhReYulsU5VopPCyABtMLZvCSxk0CfYE,820
4
4
  postpone_index/apps.py,sha256=1ap2HY49uXVXjY9UWK3___fjPe22wqFOSOnuH2kIAxs,200
5
5
  postpone_index/migration_utils.py,sha256=02rm77mz_MAufl5vdqKPV1CAd1-3bRk6kSy4MIxKsEo,752
6
- postpone_index/models.py,sha256=jMsA5hdiL8Xgl-hq7bgVl5NUX6LXlSYpMuX9pD68BbU,1771
6
+ postpone_index/models.py,sha256=Aeow27GSHCkklLnUAGnRwwaCaLrQ11b2Sg7vIZIybGg,3580
7
7
  postpone_index/testing_utils.py,sha256=ycxp7ovY6JceOvNpotK7OGAebhdHicXJ-0fWJmaq9fo,1224
8
8
  postpone_index/utils.py,sha256=wYVPsQRs3LN-ZtvfSWrDDqFFp0OCkVxW9j7w9yqd47o,3823
9
9
  postpone_index/contrib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -12,12 +12,12 @@ postpone_index/contrib/postgis/base.py,sha256=a5h1k11BVSOflSSesTxObKAEYNWtisFLRb
12
12
  postpone_index/contrib/postgis/schema.py,sha256=eantuNhFYWDngDnAeURJurpW58UztPnjTIySmUlXx7s,340
13
13
  postpone_index/contrib/postgres/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
14
  postpone_index/contrib/postgres/base.py,sha256=Xq90nJkxGSgR8Im9JPnFepeMb0-bur9sRjPpm3zMd6U,281
15
- postpone_index/contrib/postgres/schema.py,sha256=45xt6mhVgGgFWYH_OVZE0WzcyBv9J6sCQVR4WnxA1Xc,10914
15
+ postpone_index/contrib/postgres/schema.py,sha256=qmJRxBCKfwWVrM7P2vSaaR4aAl_C6-2qMrKPiQQ9QpU,10383
16
16
  postpone_index/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
17
  postpone_index/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
18
  postpone_index/management/commands/apply_postponed.py,sha256=wVbw0ugUyPuYwUikGnn5MvP9l9OsEIZja9sb6nsMtOw,9107
19
19
  postpone_index/sql/start.sql,sha256=44yFbZTjknjXUvl7MpO1uuPv-CVBsZcnUDuKc8mVj3k,871
20
- django_postpone_index-0.0.2.dist-info/METADATA,sha256=8jnBu6D07a_Nszgmkk9e9kP7wMJzeE3N9jCsxc5PGOk,10415
21
- django_postpone_index-0.0.2.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
22
- django_postpone_index-0.0.2.dist-info/top_level.txt,sha256=B6q_TICqApvruf_NJfnLFNLZLpxMLebQF6cPdKrWm5A,162
23
- django_postpone_index-0.0.2.dist-info/RECORD,,
20
+ django_postpone_index-0.0.3.dist-info/METADATA,sha256=aEo4ieeQpm90WE2j8ljOuz_F3m5pD1e0wzGBTBA4MIQ,12365
21
+ django_postpone_index-0.0.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
22
+ django_postpone_index-0.0.3.dist-info/top_level.txt,sha256=B6q_TICqApvruf_NJfnLFNLZLpxMLebQF6cPdKrWm5A,162
23
+ django_postpone_index-0.0.3.dist-info/RECORD,,
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '0.0.2'
32
- __version_tuple__ = version_tuple = (0, 0, 2)
31
+ __version__ = version = '0.0.3'
32
+ __version_tuple__ = version_tuple = (0, 0, 3)
33
33
 
34
34
  __commit_id__ = commit_id = None
@@ -25,20 +25,10 @@ class DatabaseSchemaEditorMixin(Utils):
25
25
  """
26
26
  if self._base_tables_created:
27
27
  return
28
-
29
28
  # Avoid circular import
30
29
  from postpone_index.models import PostponedSQL
31
30
 
32
- cursor = self.connection.cursor()
33
- cursor.execute("SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '%s' limit 1" % PostponedSQL._meta.db_table)
34
- if cursor.fetchall():
35
- self._base_tables_created = True
36
- logger.debug('[%s] The PostponedSQL storage already exists', self.connection.alias)
37
- return
38
- logger.info('[%s] The PostponedSQL storage is absent, creating', self.connection.alias)
39
- with open(os.path.join(package_folder, 'sql/start.sql')) as f:
40
- sql = f.read()
41
- cursor.execute(sql)
31
+ PostponedSQL.objects.using(self.connection.alias)._create_base_tables()
42
32
  self._base_tables_created = True
43
33
 
44
34
  def _ignore(self):
postpone_index/models.py CHANGED
@@ -1,12 +1,78 @@
1
+ import logging
2
+ import os
1
3
  import time
2
4
 
3
5
  from unlimited_char.fields import CharField
4
6
 
5
- from django.db import models
7
+ from django.db import models, connections
6
8
  from django.utils.translation import gettext_lazy as _
7
9
 
10
+ logger = logging.getLogger(__name__)
11
+ package_folder = os.path.dirname(os.path.abspath(__file__))
12
+
13
+
14
+ class PostponedSQLQuerySet(models.QuerySet):
15
+ """
16
+ QuerySet for PostponedSQL model.
17
+
18
+ Redefined for admin view when table is absent.
19
+ """
20
+
21
+ def _create_base_tables(self):
22
+ """
23
+ Create base package table if absent
24
+ """
25
+ if self._is_present():
26
+ logger.debug('[%s] The PostponedSQL storage already exists', self.db)
27
+ return
28
+ logger.info('[%s] The PostponedSQL storage is absent, creating', self.db)
29
+ with open(os.path.join(package_folder, 'sql/start.sql')) as f:
30
+ sql = f.read()
31
+ cursor = connections[self.db].cursor()
32
+ cursor.execute(sql)
33
+
34
+ def count(self):
35
+ """
36
+ Count only if table is present
37
+ """
38
+ if not self._is_present():
39
+ return 0
40
+ return super().count()
41
+
42
+ def _is_present(self):
43
+ """
44
+ Check if table is present
45
+ """
46
+ cursor = connections[self.db].cursor()
47
+ cursor.execute(
48
+ """
49
+ SELECT 1 FROM information_schema.tables
50
+ WHERE table_schema = 'public' AND table_name = '%s' limit 1
51
+ """ % PostponedSQL._meta.db_table
52
+ )
53
+ return bool(cursor.fetchall())
54
+
55
+
56
+ class PostponedSQLManager(models.Manager):
57
+ """
58
+ Manager for PostponedSQL model.
59
+
60
+ Redefined for admin view when table is absent.
61
+ """
62
+
63
+ def get_queryset(self):
64
+ """
65
+ Get custom QuerySet
66
+ """
67
+ qs = PostponedSQLQuerySet(self.model, using=self._db)
68
+ if not qs._is_present():
69
+ return qs.none()
70
+ return qs
71
+
8
72
 
9
73
  class PostponedSQL(models.Model):
74
+ """Model to store postponed SQL commands"""
75
+
10
76
  ts = models.BigIntegerField(
11
77
  primary_key=True, editable=False,
12
78
  default=time.time_ns,
@@ -47,6 +113,8 @@ class PostponedSQL(models.Model):
47
113
  help_text=_('Last error reported when tried to apply')
48
114
  )
49
115
 
116
+ objects = PostponedSQLManager()
117
+
50
118
  @property
51
119
  def d(self):
52
120
  """Status mark"""