Skip to main content

Generate gap-less sequences of integer values.

Project description

The problem

Django’s default, implicit primary keys aren’t guaranteed to be sequential.

If a transaction inserts a row and then is rolled back, the sequence counter isn’t rolled back for performance reasons, creating a gap in primary keys.

This can cause compliance issues for some use cases such as accounting.

This risk isn’t well known. Since most transactions succeed, values look sequential. Gaps will only be revealed by audits.

The solution

django-sequences provides just one function, get_next_value, which is designed to be used as follows:

from django.db import transaction

from sequences import get_next_value

from invoices.models import Invoice

with transaction.atomic():
    Invoice.objects.create(number=get_next_value('invoice_numbers'))

The guarantees of django-sequences only apply if you call get_next_value and save its return value to the database within the same transaction!

Installation

django-sequences is compatible with Django 1.11 (LTS), 2.0 and 2.1.

Install django-sequences:

$ pip install django-sequences

Add it to the list of applications in your project’s settings:

INSTALLED_APPS += ['sequences.apps.SequencesConfig']

Run migrations:

$ django-admin migrate

API

get_next_value generates a gap-less sequence of integer values:

>>> get_next_value()
1
>>> get_next_value()
2
>>> get_next_value()
3

It supports multiple independent sequences:

>>> get_next_value('cases')
1
>>> get_next_value('cases')
2
>>> get_next_value('invoices')
1
>>> get_next_value('invoices')
2

The first value defaults to 1. It can be customized:

>>> get_next_value('customers', initial_value=1000)  # pro growth hacking

The initial_value parameter only matters when get_next_value is called for the first time for a given sequence — assuming the corresponding database transaction gets committed; as discussed above, if the transaction is rolled back, the generated value isn’t consumed. It’s also possible to initialize a sequence in a data migration and not use initial_value in actual code.

Sequences can loop:

>>> get_next_value('seconds', initial_value=0, reset_value=60)

When the sequence reaches reset_value, it restarts at initial_value. In other works, it generates reset_value - 2, reset_value - 1, initial_value, initial_value + 1, etc. In that case, each call to get_next_value must provide initial_value and reset_value.

Database transactions that call get_next_value for a given sequence are serialized. In other words, when you call get_next_value in a database transaction, other callers which attempt to get a value from the same sequence will block until the transaction completes, either with a commit or a rollback. You should keep such transactions short to minimize the impact on performance.

(This is why databases default to a faster behavior that may create gaps.)

Passing nowait=True will cause get_next_value to raise an exception instead of blocking. This will rarely be useful. Also it doesn’t work for the first call. (Arguably this is a bug. Patches welcome.)

Calls to get_next_value for distinct sequences don’t interact with one another.

Finally, passing using='...' allows selecting the database on which the current sequence value is stored. When this parameter isn’t provided, the current value is stored in the default database for writing to models of the sequences application. See “Multiple databases” below for details.

To sum up, the complete signature of get_next_value is:

get_next_value(sequence_name='default', initial_value=1, reset_value=None,
               *, nowait=False, using=None)

Under the hood, it relies on the database’s transactional integrity to guarantee that each value will be returned exactly once.

Contributing

You can run tests with:

$ make test

If you’d like to contribute, please open an issue or a pull request on GitHub!

Database support

django-sequences is tested on PostgreSQL, MySQL, Oracle, and SQLite.

MySQL only supports the nowait parameter when it’s MariaDB ≥ 8.0.1.

Applications that will only ever be deployed with an SQLite database don’t need django-sequences because SQLite’s INTEGER PRIMARY KEY AUTOINCREMENT fields are guaranteed to be sequential.

Multiple databases

Since django-sequences relies on the database to guarantee transactional integrity, the current value for a given sequence must be stored in the same database as models containing generated values.

In a project that uses multiple databases, you must write a suitable database router to create tables for the sequences application on all databases storing models containing sequential numbers.

Each database has its own namespace: a sequence with the same name stored in two databases will have independent counters in each database.

Changelog

2.3

  • Tested on MySQL, SQLite and Oracle.

  • Optimized performance on MySQL.

2.2

  • Optimized performance on PostgreSQL ≥ 9.5.

2.1

  • Provide looping sequences with reset_value.

2.0

  • Add support for multiple databases.

  • Add translations.

  • nowait becomes keyword-only argument.

  • Drop support for Python 2.

1.0

  • Initial stable release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-sequences-2.3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sequences-2.3-py2.py3-none-any.whl (29.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-sequences-2.3.tar.gz.

File metadata

  • Download URL: django-sequences-2.3.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for django-sequences-2.3.tar.gz
Algorithm Hash digest
SHA256 15acae104db2574db849e0814930b00aa61524e1ac80bc340c09be0b15c2a1b1
MD5 585e5e29098218a90f95a265937ff987
BLAKE2b-256 f275f021bd70ac62a6ede70766c96f464c9c6f61e02a83c70fd835bc40f6f349

See more details on using hashes here.

File details

Details for the file django_sequences-2.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_sequences-2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1058fd16db38044d8999c85530cae8f77ec329b600fd8034479ab384d3ee3e63
MD5 d6c8acb557fe3123228ca7446648a69a
BLAKE2b-256 e836cc2d9a0ddec6636cfa7d636f4433ca10992964f4680a686c479f4ef43370

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page