~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/maasserver/testing/db_migrations.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-03-04 11:49:44 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: package-import@ubuntu.com-20130304114944-azcvu9anlf8mizpa
Tags: upstream-1.3+bzr1452+dfsg
ImportĀ upstreamĀ versionĀ 1.3+bzr1452+dfsg

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
"""Helpers for testing South database migrations.
 
5
 
 
6
Each Django application in MAAS tests the basic sanity of its own South
 
7
database migrations.  To minimize repetition, this single module provides all
 
8
the code those tests need.
 
9
"""
 
10
 
 
11
from __future__ import (
 
12
    absolute_import,
 
13
    print_function,
 
14
    unicode_literals,
 
15
    )
 
16
 
 
17
__metaclass__ = type
 
18
__all__ = [
 
19
    'detect_sequence_clashes',
 
20
    ]
 
21
 
 
22
from collections import Counter
 
23
import re
 
24
 
 
25
from south.migration.base import Migrations
 
26
from south.utils import ask_for_it_by_name
 
27
 
 
28
 
 
29
def extract_number(migration_name):
 
30
    """Extract the sequence number from a migration module name."""
 
31
    return int(re.match('([0-9]+)_', migration_name).group(1))
 
32
 
 
33
 
 
34
def get_duplicates(numbers):
 
35
    """Return set of those items that occur more than once."""
 
36
    return {
 
37
        numbers
 
38
        for numbers, count in Counter(numbers).items()
 
39
            if count > 1}
 
40
 
 
41
 
 
42
def list_migrations(app_name):
 
43
    """List schema migrations in the given app."""
 
44
    app = ask_for_it_by_name(app_name)
 
45
    return [migration.name() for migration in Migrations(app)]
 
46
 
 
47
 
 
48
def detect_sequence_clashes(app_name):
 
49
    """List numbering clashes among database migrations in given app.
 
50
 
 
51
    :param app_name: Name of a MAAS Django application, e.g. "metadataserver"
 
52
    :return: A sorted `list` of tuples `(number, name)` representing all
 
53
        migration modules in the app that have clashing sequence numbers.
 
54
        The `number` is as found in `name`, but in `int` form.
 
55
    """
 
56
    migrations = list_migrations(app_name)
 
57
    numbers_and_names = [(extract_number(name), name) for name in migrations]
 
58
    duplicates = get_duplicates(number for number, name in numbers_and_names)
 
59
    return sorted(
 
60
        (number, name)
 
61
        for number, name in numbers_and_names
 
62
            if number in duplicates)