~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/maasserver/sequence.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""SQL Sequence."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'Sequence',
 
15
    'INT_MAX',
 
16
    ]
 
17
 
 
18
 
 
19
from django.db import (
 
20
    connection,
 
21
    transaction,
 
22
    )
 
23
 
 
24
 
 
25
BIGINT_MAX = 2 ** 63 - 1
 
26
 
 
27
INT_MAX = 2 ** 32 - 1
 
28
 
 
29
 
 
30
class Sequence:
 
31
    """SQL sequence."""
 
32
 
 
33
    def __init__(self, name, incr=1, minvalue=1, maxvalue=BIGINT_MAX):
 
34
        self.name = name
 
35
        self.incr = incr
 
36
        self.minvalue = minvalue
 
37
        self.maxvalue = maxvalue
 
38
 
 
39
    def create(self):
 
40
        """Create this sequence in the database."""
 
41
        cursor = connection.cursor()
 
42
        query = "CREATE SEQUENCE %s" % self.name
 
43
        cursor.execute(
 
44
            query + " INCREMENT BY %s MINVALUE %s MAXVALUE %s CYCLE",
 
45
            [self.incr, self.minvalue, self.maxvalue])
 
46
        transaction.commit_unless_managed()
 
47
 
 
48
    def nextval(self):
 
49
        """Return the next value of this sequence.
 
50
 
 
51
        :return: The sequence value.
 
52
        :rtype: int
 
53
        """
 
54
        cursor = connection.cursor()
 
55
        cursor.execute(
 
56
            "SELECT nextval(%s)", [self.name])
 
57
        return cursor.fetchone()[0]
 
58
 
 
59
    def drop(self):
 
60
        """Drop this sequence from the database."""
 
61
        cursor = connection.cursor()
 
62
        cursor.execute(
 
63
            "DROP SEQUENCE %s" % self.name)
 
64
        transaction.commit_unless_managed()