Ticket #20: south-support-gis-columns.diff
| File south-support-gis-columns.diff, 5.9 KB (added by sean_legassick@…, 5 years ago) |
|---|
-
migration.py
243 243 db.start_transaction() 244 244 try: 245 245 klass().backwards() 246 db.execute_deferred_sql() 246 247 except: 247 248 db.rollback_transaction() 248 249 raise -
db/generic.py
1 1 2 from django.core.management.color import no_style 2 3 from django.db import connection, transaction, models 3 4 from django.db.backends.util import truncate_name 4 5 from django.dispatch import dispatcher … … 61 62 for field_name, field in fields 62 63 ] 63 64 64 self.execute('CREATE TABLE %s (%s);' % ( table_name, ', '.join([col for col in columns])))65 self.execute('CREATE TABLE %s (%s);' % (qn(table_name), ', '.join([col for col in columns if col]))) 65 66 66 67 add_table = create_table # Alias for consistency's sake 67 68 … … 101 102 """ 102 103 qn = connection.ops.quote_name 103 104 sql = self.column_sql(table_name, name, field) 104 params = ( 105 qn(table_name), 106 sql, 107 ) 108 sql = 'ALTER TABLE %s ADD COLUMN %s;' % params 109 self.execute(sql) 105 if sql: 106 params = ( 107 qn(table_name), 108 sql, 109 ) 110 sql = 'ALTER TABLE %s ADD COLUMN %s;' % params 111 self.execute(sql) 110 112 111 113 112 114 alter_string_set_type = 'ALTER COLUMN %(column)s TYPE %(type)s' … … 174 176 Creates the SQL snippet for a column. Used by add_column and add_table. 175 177 """ 176 178 qn = connection.ops.quote_name 179 177 180 field.set_attributes_from_name(field_name) 178 181 179 182 # hook for the field to do any resolution prior to it's attributes being queried … … 181 184 field.south_init() 182 185 183 186 sql = field.db_type() 184 if not sql: 185 return None 186 187 field_output = [qn(field.column), sql] 188 field_output.append('%sNULL' % (not field.null and 'NOT ' or '')) 189 if field.primary_key: 190 field_output.append('PRIMARY KEY') 191 elif field.unique: 192 field_output.append('UNIQUE') 187 if sql: 188 field_output = [qn(field.column), sql] 189 field_output.append('%sNULL' % (not field.null and 'NOT ' or '')) 190 if field.primary_key: 191 field_output.append('PRIMARY KEY') 192 elif field.unique: 193 field_output.append('UNIQUE') 193 194 194 tablespace = field.db_tablespace or tablespace195 if tablespace and connection.features.supports_tablespaces and field.unique:196 # We must specify the index tablespace inline, because we197 # won't be generating a CREATE INDEX statement for this field.198 field_output.append(connection.ops.tablespace_sql(tablespace, inline=True))195 tablespace = field.db_tablespace or tablespace 196 if tablespace and connection.features.supports_tablespaces and field.unique: 197 # We must specify the index tablespace inline, because we 198 # won't be generating a CREATE INDEX statement for this field. 199 field_output.append(connection.ops.tablespace_sql(tablespace, inline=True)) 199 200 200 sql = ' '.join(field_output)201 sqlparams = ()202 # if the field is "NOT NULL" and a default value is provided, create the column with it203 # this allows the addition of a NOT NULL field to a table with existing rows204 if not field.null and field.has_default():205 default = field.get_default()206 if isinstance(default, basestring):207 default = "'%s'" % default208 sql += " DEFAULT %s"209 sqlparams = (default)201 sql = ' '.join(field_output) 202 sqlparams = () 203 # if the field is "NOT NULL" and a default value is provided, create the column with it 204 # this allows the addition of a NOT NULL field to a table with existing rows 205 if not field.null and field.has_default(): 206 default = field.get_default() 207 if isinstance(default, basestring): 208 default = "'%s'" % default 209 sql += " DEFAULT %s" 210 sqlparams = (default) 210 211 211 if field.rel: 212 self.add_deferred_sql( 213 self.foreign_key_sql( 214 table_name, 215 field.column, 216 field.rel.to._meta.db_table, 217 field.rel.to._meta.get_field(field.rel.field_name).column 212 if field.rel: 213 self.add_deferred_sql( 214 self.foreign_key_sql( 215 table_name, 216 field.column, 217 field.rel.to._meta.db_table, 218 field.rel.to._meta.get_field(field.rel.field_name).column 219 ) 218 220 ) 219 )220 221 221 if field.db_index and not field.unique:222 self.add_deferred_sql(self.create_index_sql(table_name, [field.column]))222 if field.db_index and not field.unique: 223 self.add_deferred_sql(self.create_index_sql(table_name, [field.column])) 223 224 224 return sql % sqlparams 225 if hasattr(field, 'post_create_sql'): 226 style = no_style() 227 for stmt in field.post_create_sql(style, table_name): 228 self.add_deferred_sql(stmt) 229 230 if sql: 231 return sql % sqlparams 232 else: 233 return None 225 234 226 235 def foreign_key_sql(self, from_table_name, from_column_name, to_table_name, to_column_name): 227 236 """
