/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 · 14 complexity · 851f03302d118f1f846358acfcc68c25 MD5 · raw file

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