50
56
# Make the definition (e.g. 'foo VARCHAR(30)') for this field.
51
57
field_output = [style.SQL_FIELD(qn(f.column)),
52
58
style.SQL_COLTYPE(col_type)]
59
# Oracle treats the empty string ('') as null, so coerce the null
60
# option whenever '' is a possible value.
62
if (f.empty_strings_allowed and not f.primary_key and
63
self.connection.features.interprets_empty_strings_as_nulls):
54
66
field_output.append(style.SQL_KEYWORD('NOT NULL'))
56
68
field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
81
93
full_statement = [style.SQL_KEYWORD('CREATE TABLE') + ' ' +
82
94
style.SQL_TABLE(qn(opts.db_table)) + ' (']
83
for i, line in enumerate(table_output): # Combine and add commas.
95
for i, line in enumerate(table_output): # Combine and add commas.
84
96
full_statement.append(
85
' %s%s' % (line, i < len(table_output)-1 and ',' or ''))
97
' %s%s' % (line, i < len(table_output) - 1 and ',' or ''))
86
98
full_statement.append(')')
87
99
if opts.db_tablespace:
88
100
tablespace_sql = self.connection.ops.tablespace_sql(
161
173
Returns the CREATE INDEX SQL statements for a single model.
163
if not model._meta.managed or model._meta.proxy:
175
if not model._meta.managed or model._meta.proxy or model._meta.swapped:
166
178
for f in model._meta.local_fields:
167
179
output.extend(self.sql_indexes_for_field(model, f, style))
180
for fs in model._meta.index_together:
181
fields = [model._meta.get_field_by_name(f)[0] for f in fs]
182
output.extend(self.sql_indexes_for_fields(model, fields, style))
170
185
def sql_indexes_for_field(self, model, f, style):
172
187
Return the CREATE INDEX SQL statements for a single model field.
174
from django.db.backends.util import truncate_name
176
189
if f.db_index and not f.unique:
177
qn = self.connection.ops.quote_name
178
tablespace = f.db_tablespace or model._meta.db_tablespace
180
tablespace_sql = self.connection.ops.tablespace_sql(tablespace)
182
tablespace_sql = ' ' + tablespace_sql
185
i_name = '%s_%s' % (model._meta.db_table, self._digest(f.column))
186
output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
187
style.SQL_TABLE(qn(truncate_name(
188
i_name, self.connection.ops.max_name_length()))) + ' ' +
189
style.SQL_KEYWORD('ON') + ' ' +
190
style.SQL_TABLE(qn(model._meta.db_table)) + ' ' +
191
"(%s)" % style.SQL_FIELD(qn(f.column)) +
192
"%s;" % tablespace_sql]
190
return self.sql_indexes_for_fields(model, [f], style)
194
def sql_indexes_for_fields(self, model, fields, style):
195
from django.db.backends.util import truncate_name
197
if len(fields) == 1 and fields[0].db_tablespace:
198
tablespace_sql = self.connection.ops.tablespace_sql(fields[0].db_tablespace)
199
elif model._meta.db_tablespace:
200
tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace)
204
tablespace_sql = " " + tablespace_sql
207
qn = self.connection.ops.quote_name
209
field_names.append(style.SQL_FIELD(qn(f.column)))
211
index_name = "%s_%s" % (model._meta.db_table, self._digest([f.name for f in fields]))
214
style.SQL_KEYWORD("CREATE INDEX") + " " +
215
style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + " " +
216
style.SQL_KEYWORD("ON") + " " +
217
style.SQL_TABLE(qn(model._meta.db_table)) + " " +
218
"(%s)" % style.SQL_FIELD(", ".join(field_names)) +
219
"%s;" % tablespace_sql,
197
222
def sql_destroy_model(self, model, references_to_delete, style):
199
224
Return the DROP TABLE and restraint dropping statements for a single
202
if not model._meta.managed or model._meta.proxy:
227
if not model._meta.managed or model._meta.proxy or model._meta.swapped:
204
229
# Drop the table now
205
230
qn = self.connection.ops.quote_name
250
275
test_db_repr = ''
251
276
if verbosity >= 2:
252
277
test_db_repr = " ('%s')" % test_database_name
253
print "Creating test database for alias '%s'%s..." % (
254
self.connection.alias, test_db_repr)
278
print("Creating test database for alias '%s'%s..." % (
279
self.connection.alias, test_db_repr))
256
281
self._create_test_db(verbosity, autoclobber)
258
283
self.connection.close()
259
284
self.connection.settings_dict["NAME"] = test_database_name
261
# Confirm the feature set of the test database
262
self.connection.features.confirm()
264
286
# Report syncdb messages at one level lower than that requested.
265
287
# This ensures we don't get flooded with messages during testing
266
288
# (unless you really ask to be flooded)
325
347
"CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
348
except Exception as e:
327
349
sys.stderr.write(
328
350
"Got an error creating the test database: %s\n" % e)
329
351
if not autoclobber:
331
353
"Type 'yes' if you would like to try deleting the test "
332
354
"database '%s', or 'no' to cancel: " % test_database_name)
333
355
if autoclobber or confirm == 'yes':
335
357
if verbosity >= 1:
336
print ("Destroying old test database '%s'..."
337
% self.connection.alias)
358
print("Destroying old test database '%s'..."
359
% self.connection.alias)
339
361
"DROP DATABASE %s" % qn(test_database_name))
341
363
"CREATE DATABASE %s %s" % (qn(test_database_name),
365
except Exception as e:
344
366
sys.stderr.write(
345
367
"Got an error recreating the test database: %s\n" % e)
348
print "Tests cancelled."
370
print("Tests cancelled.")
351
373
return test_database_name
361
383
test_db_repr = ''
362
384
if verbosity >= 2:
363
385
test_db_repr = " ('%s')" % test_database_name
364
print "Destroying test database for alias '%s'%s..." % (
365
self.connection.alias, test_db_repr)
386
print("Destroying test database for alias '%s'%s..." % (
387
self.connection.alias, test_db_repr))
367
389
# Temporarily use a new connection and a copy of the settings dict.
368
390
# This prevents the production database from being exposed to potential