My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from django.core.management import color
from django.db import models

from common import BaseEvolutionOperations

TEMP_TABLE_NAME = 'TEMP_TABLE'

class EvolutionOperations(BaseEvolutionOperations):
def delete_column(self, model, f):
output = []

field_list = [
field for field in model._meta.local_fields
# Remove the field to be deleted
if f.name != field.name
# and any Generic fields
and field.db_type(connection=self.connection) is not None
]
table_name = model._meta.db_table

output.extend(self.create_temp_table(field_list))
output.extend(self.copy_to_temp_table(table_name, field_list))
output.extend(self.delete_table(table_name))
output.extend(self.create_table(table_name, field_list))
output.extend(self.copy_from_temp_table(table_name, field_list))
output.extend(self.delete_table(TEMP_TABLE_NAME))

return output

def copy_to_temp_table(self, source_table_name, original_field_list,
new_field_list=None):
qn = self.connection.ops.quote_name

source_columns = self.column_names(original_field_list)

if new_field_list:
temp_columns = self.column_names(new_field_list)
else:
temp_columns = source_columns

return ['INSERT INTO %s (%s) SELECT %s FROM %s;' %
(qn(TEMP_TABLE_NAME), temp_columns, source_columns,
qn(source_table_name))]

def copy_from_temp_table(self, dest_table_name, field_list):
qn = self.connection.ops.quote_name
params = {
'dest_table_name': qn(dest_table_name),
'temp_table': qn(TEMP_TABLE_NAME),
'column_names': self.column_names(field_list),
}

return ['INSERT INTO %(dest_table_name)s (%(column_names)s) SELECT %(column_names)s FROM %(temp_table)s;' % params]

def column_names(self, field_list):
qn = self.connection.ops.quote_name
columns = []

for field in field_list:
if not isinstance(field, models.ManyToManyField):
columns.append(qn(field.column))

return ', '.join(columns)

def insert_to_temp_table(self, field, initial):
qn = self.connection.ops.quote_name

# At this point, initial can only be None if null=True, otherwise it is
# a user callable or the default AddFieldInitialCallback which will shortly raise an exception.
if initial is None:
return []

params = {
'table_name': qn(TEMP_TABLE_NAME),
'column_name': qn(field.column),
}

if callable(initial):
params['value'] = initial()
return ["UPDATE %(table_name)s SET %(column_name)s = %(value)s;" % params]
else:
return [("UPDATE %(table_name)s SET %(column_name)s = %%s;" % params, (initial,))]


def create_temp_table(self, field_list):
return self.create_table(TEMP_TABLE_NAME, field_list, True, False)

def create_indexes_for_table(self, table_name, field_list):
class FakeMeta(object):
def __init__(self, table_name, field_list):
self.db_table = table_name
self.local_fields = field_list
self.fields = field_list # Required for Pre QS-RF support
self.db_tablespace = None
self.managed = True
self.proxy = False

class FakeModel(object):
def __init__(self, table_name, field_list):
self._meta = FakeMeta(table_name, field_list)

style = color.no_style()
return self.connection.creation.sql_indexes_for_model(FakeModel(table_name, field_list), style)

def create_table(self, table_name, field_list, temporary=False, create_index=True):
qn = self.connection.ops.quote_name
output = []

create = ['CREATE']
if temporary:
create.append('TEMPORARY')
create.append('TABLE %s' % qn(table_name))
output = [' '.join(create)]
output.append('(')
columns = []
for field in field_list:
if not models.ManyToManyField == field.__class__:
column_name = qn(field.column)
column_type = field.db_type(connection=self.connection)
params = [column_name, column_type]

# Always use null if this is a temporary table. It may be
# used to create a new field (which will be null while data is
# copied across from the old table).
if temporary or field.null:
params.append('NULL')
else:
params.append('NOT NULL')

if field.unique:
params.append('UNIQUE')

if field.primary_key:
params.append('PRIMARY KEY')

columns.append(' '.join(params))

output.append(', '.join(columns))
output.append(');')
output = [''.join(output)]

if create_index:
output.extend(self.create_indexes_for_table(table_name, field_list))

return output

def rename_column(self, opts, old_field, new_field):
if old_field.column == new_field.column:
# No Operation
return []

original_fields = opts.local_fields
new_fields = []
for f in original_fields:
# Ignore Generic Fields
if f.db_type(connection=self.connection) is not None:
if f.name == old_field.name:
new_fields.append(new_field)
else:
new_fields.append(f)

table_name = opts.db_table
output = []
output.extend(self.create_temp_table(new_fields))
output.extend(self.copy_to_temp_table(table_name, original_fields,
new_fields))
output.extend(self.delete_table(table_name))
output.extend(self.create_table(table_name, new_fields))
output.extend(self.copy_from_temp_table(table_name, new_fields))
output.extend(self.delete_table(TEMP_TABLE_NAME))

return output

def add_column(self, model, f, initial):
output = []
table_name = model._meta.db_table
original_fields = [
field
for field in model._meta.local_fields
if field.db_type(connection=self.connection) is not None
]
new_fields = list(original_fields)
new_fields.append(f)

output.extend(self.create_temp_table(new_fields))
output.extend(self.copy_to_temp_table(table_name, original_fields))
output.extend(self.insert_to_temp_table(f, initial))
output.extend(self.delete_table(table_name))
output.extend(self.create_table(table_name, new_fields, create_index=False))
output.extend(self.copy_from_temp_table(table_name, new_fields))
output.extend(self.delete_table(TEMP_TABLE_NAME))
return output

def change_null(self, model, field_name, new_null_attr, initial=None):
return self.change_attribute(model, field_name, 'null', new_null_attr, initial)

def change_max_length(self, model, field_name, new_max_length, initial=None):
return self.change_attribute(model, field_name, 'max_length', new_max_length, initial)

def change_unique(self, model, field_name, new_unique_value, initial=None):
return self.change_attribute(model, field_name, '_unique', new_unique_value, initial)

def change_attribute(self, model, field_name, attr_name, new_attr_value, initial=None):
output = []
opts = model._meta
table_name = opts.db_table
setattr(opts.get_field(field_name), attr_name, new_attr_value)
fields = [
f
for f in opts.local_fields
if f.db_type(connection=self.connection) is not None
]

output.extend(self.create_temp_table(fields))
output.extend(self.copy_to_temp_table(table_name, fields))
output.extend(self.insert_to_temp_table(opts.get_field(field_name), initial))
output.extend(self.delete_table(table_name))
output.extend(self.create_table(table_name, fields, create_index=False))
output.extend(self.copy_from_temp_table(table_name, fields))
output.extend(self.delete_table(TEMP_TABLE_NAME))
return output

Change log

r202 by chipx86 on May 9, 2011   Diff
Fix multi-database support and the unit
tests for checking it.

Multi-database support wasn't fully-
featured, as the evolver wasn't
being passed the correct database. This
was verified with unit tests
when two databases were of different types
(the generated SQL would be
wrong).

In practice, if the databases were of the
...
Go to: 
Project members, sign in to write a code review

Older revisions

r188 by chipx86 on Oct 24, 2010   Diff
Add support for multiple databases in
Django 1.2.

This updates all database-related call
sites for Django 1.2's multiple
...
r184 by chipx86 on Oct 13, 2010   Diff
Fix incorrect defaults on SQLite when
adding null fields.

On SQLite, adding a null field without
a default value would cause the
...
r180 by chipx86 on May 18, 2010   Diff
Clean up some warnings from pyflakes.

There were several unused variables,
mostly in exception handlers. They're
pointless, so clean them up.
All revisions of this file

File info

Size: 8535 bytes, 221 lines
Powered by Google Project Hosting