PageRenderTime 49ms CodeModel.GetById 28ms app.highlight 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bangkokhotel/lib/python2.5/site-packages/django/db/backends/sqlite3/creation.py

https://bitbucket.org/luisrodriguez/bangkokhotel
Python | 89 lines | 73 code | 8 blank | 8 comment | 13 complexity | 851f03302d118f1f846358acfcc68c25 MD5 | raw file
 1import os
 2import sys
 3from django.db.backends.creation import BaseDatabaseCreation
 4
 5class DatabaseCreation(BaseDatabaseCreation):
 6    # SQLite doesn't actually support most of these types, but it "does the right
 7    # thing" given more verbose field definitions, so leave them as is so that
 8    # schema inspection is more useful.
 9    data_types = {
10        'AutoField':                    'integer',
11        'BooleanField':                 'bool',
12        'CharField':                    'varchar(%(max_length)s)',
13        'CommaSeparatedIntegerField':   'varchar(%(max_length)s)',
14        'DateField':                    'date',
15        'DateTimeField':                'datetime',
16        'DecimalField':                 'decimal',
17        'FileField':                    'varchar(%(max_length)s)',
18        'FilePathField':                'varchar(%(max_length)s)',
19        'FloatField':                   'real',
20        'IntegerField':                 'integer',
21        'BigIntegerField':              'bigint',
22        'IPAddressField':               'char(15)',
23        'GenericIPAddressField':        'char(39)',
24        'NullBooleanField':             'bool',
25        'OneToOneField':                'integer',
26        'PositiveIntegerField':         'integer unsigned',
27        'PositiveSmallIntegerField':    'smallint unsigned',
28        'SlugField':                    'varchar(%(max_length)s)',
29        'SmallIntegerField':            'smallint',
30        'TextField':                    'text',
31        'TimeField':                    'time',
32    }
33
34    def sql_for_pending_references(self, model, style, pending_references):
35        "SQLite3 doesn't support constraints"
36        return []
37
38    def sql_remove_table_constraints(self, model, references_to_delete, style):
39        "SQLite3 doesn't support constraints"
40        return []
41
42    def _get_test_db_name(self):
43        test_database_name = self.connection.settings_dict['TEST_NAME']
44        if test_database_name and test_database_name != ':memory:':
45            return test_database_name
46        return ':memory:'
47
48    def _create_test_db(self, verbosity, autoclobber):
49        test_database_name = self._get_test_db_name()
50        if test_database_name != ':memory:':
51            # Erase the old test database
52            if verbosity >= 1:
53                print "Destroying old test database '%s'..." % self.connection.alias
54            if os.access(test_database_name, os.F_OK):
55                if not autoclobber:
56                    confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % test_database_name)
57                if autoclobber or confirm == 'yes':
58                  try:
59                      os.remove(test_database_name)
60                  except Exception, e:
61                      sys.stderr.write("Got an error deleting the old test database: %s\n" % e)
62                      sys.exit(2)
63                else:
64                    print "Tests cancelled."
65                    sys.exit(1)
66        return test_database_name
67
68    def _destroy_test_db(self, test_database_name, verbosity):
69        if test_database_name and test_database_name != ":memory:":
70            # Remove the SQLite database file
71            os.remove(test_database_name)
72
73    def set_autocommit(self):
74        self.connection.connection.isolation_level = None
75
76    def test_db_signature(self):
77        """
78        Returns a tuple that uniquely identifies a test database.
79
80        This takes into account the special cases of ":memory:" and "" for
81        SQLite since the databases will be distinct despite having the same
82        TEST_NAME. See http://www.sqlite.org/inmemorydb.html
83        """
84        settings_dict = self.connection.settings_dict
85        test_dbname = self._get_test_db_name()
86        sig = [self.connection.settings_dict['NAME']]
87        if test_dbname == ':memory:':
88            sig.append(self.connection.alias)
89        return tuple(sig)