[deseb] r83 committed - Fixed "except Exception as e" for older pythons,...

4 views
Skip to first unread message

codesite...@google.com

unread,
Sep 28, 2009, 4:25:12 PM9/28/09
to deseb-...@googlegroups.com
Revision: 83
Author: burchik
Date: Mon Sep 28 13:25:00 2009
Log: Fixed "except Exception as e" for older pythons,
added some postgresql indexes support.


http://code.google.com/p/deseb/source/detail?r=83

Added:
/trunk/src/deseb/backends/base.py
Modified:
/trunk/src/deseb/__init__.py
/trunk/src/deseb/backends/postgresql.py
/trunk/src/deseb/backends/sqlite3.py
/trunk/src/deseb/schema_evolution.py

=======================================
--- /dev/null
+++ /trunk/src/deseb/backends/base.py Mon Sep 28 13:25:00 2009
@@ -0,0 +1,44 @@
+class DesebDatabaseOperations(object):
+ def __init__(self, connection, style):
+ self.connection = connection
+ self.style = style
+
+ def get_sql_indexes_for_field(self, tablespace, table, f):
+ "Returns the CREATE INDEX SQL statement for a single field"
+ from django.db import backend
+ output = []
+ try:
+ autopk = self.connection.features.autoindexes_primary_keys
+ except AttributeError:
+ autopk = False
+ if f.db_index and not ((f.primary_key or f.unique) and autopk):
+ unique = f.unique and 'UNIQUE ' or ''
+ try:
+ tablespace = f.db_tablespace or tablespace
+ except: # v0.96 compatibility
+ tablespace = None
+ if tablespace and backend.supports_tablespaces:
+ tablespace_sql = ' ' +
backend.get_tablespace_sql(tablespace)
+ else:
+ tablespace_sql = ''
+ qn = self.connection.ops.quote_name
+ output.append(
+ self.style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
+ self.style.SQL_TABLE(qn('%s_%s' % (table, f.column)))
+ ' ' + \
+ self.style.SQL_KEYWORD('ON') + ' ' + \
+ self.style.SQL_TABLE(qn(table)) + ' ' + \
+ "(%s)" % self.style.SQL_FIELD(qn(f.column)) + \
+ "%s;" % tablespace_sql
+ )
+ return output
+
+ def drop_sql_index_for_field(self, index_name):
+ "Returns the CREATE INDEX SQL statement for a single field"
+ from django.db import backend
+ qn = self.connection.ops.quote_name
+ output = [
+ self.style.SQL_KEYWORD('DROP INDEX') + ' ' + \
+ self.style.SQL_TABLE(qn(index_name))+";"
+ ]
+ return output
+
=======================================
--- /trunk/src/deseb/__init__.py Sat Sep 27 01:34:40 2008
+++ /trunk/src/deseb/__init__.py Mon Sep 28 13:25:00 2009
@@ -12,7 +12,7 @@
added_aka_support = False

def db_type(self):
- from django.db import get_creation_module
+ from django.db import get_creation_module #@UnresolvedImport
from django.db.models.fields.related import ForeignKey
data_types = get_creation_module().DATA_TYPES
internal_type = self.get_internal_type()
=======================================
--- /trunk/src/deseb/backends/postgresql.py Sun Feb 10 14:49:05 2008
+++ /trunk/src/deseb/backends/postgresql.py Mon Sep 28 13:25:00 2009
@@ -1,9 +1,12 @@
from deseb.schema_evolution import NotNullColumnNeedsDefaultException
+from base import DesebDatabaseOperations

try: set
except NameError: from sets import Set as set # Python 2.3 fallback

-class DatabaseOperations:
+class DatabaseOperations(DesebDatabaseOperations):
+ pk_requires_unique = False
+
def quote_value(self, s):
if type(s) is bool:
if s: return "'t'"
@@ -13,12 +16,6 @@
else:
return u"'%s'" % unicode(s).replace("'","\'")

- def __init__(self, connection, style):
- self.connection = connection
- self.style = style
-
- pk_requires_unique = False
-
def get_change_table_name_sql( self, table_name, old_table_name ):
output = []
qn = self.connection.ops.quote_name
@@ -45,6 +42,7 @@
return output

def get_change_column_def_sql( self, table_name, col_name, col_type,
f, column_flags, f_default, updates ):
+ #print 'get_change_column_def_sql', table_name, col_name,
col_type, f, column_flags, f_default, updates
from django.db.models.fields import NOT_PROVIDED
output = []
qn = self.connection.ops.quote_name
@@ -113,6 +111,11 @@
table_name + '_' + col_name + '_unique_constraint'+
kw(' UNIQUE(') + fqn(col_name) + kw(')')+';' )

+ if updates['update_index']:
+ if f.db_index:
+ output.extend(self.get_sql_indexes_for_field(None,
table_name, f))
+ if not f.db_index:
+
output.extend(self.drop_sql_index_for_field(column_flags['db_index']))
return output

def get_add_column_sql( self, table_name, col_name, col_type, null,
unique, primary_key, f_default):
@@ -263,12 +266,15 @@
'foreign_key': False,
'unique': False,
'allow_null': False,
+ 'db_index': None,
'max_length': None
}

+ attnum = None
for row in cursor.fetchall():
if row[0] == column_name:
- # maxlength check goes here
+ attnum = row[4]
+ # maxlength check goes here
dict['allow_null'] = not row[3]
dict['coltype'] = row[1]
if row[1][0:17]=='character varying':
@@ -284,16 +290,29 @@
shared_unique_connames = set()
cursor.execute("select pg_constraint.conname,
pg_constraint.contype, pg_attribute.attname from pg_constraint,
pg_attribute, pg_class where pg_constraint.conrelid=pg_class.oid and
pg_constraint.conrelid=pg_attribute.attrelid and
pg_attribute.attnum=any(pg_constraint.conkey) and pg_class.relname='%s'" %
table_name )
for row in cursor.fetchall():
- # print row
if row[2] == column_name:
- if row[1]=='p': dict['primary_key'] = True
- if row[1]=='f': dict['foreign_key'] = True
- if row[1]=='u': unique_conname = row[0]
+ if row[1]=='p':
+ dict['primary_key'] = True
+ elif row[1]=='f':
+ dict['foreign_key'] = True
+ elif row[1]=='u':
+ unique_conname = row[0]
else:
if row[1]=='u': shared_unique_connames.add( row[0] )
+
if unique_conname and unique_conname not in shared_unique_connames:
dict['unique'] = True

+ #get indexes
+ cursor.execute("""SELECT c2.relname, i.indisprimary,
i.indisunique, i.indisclustered,
+ i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),
c2.reltablespace
+ FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,
pg_catalog.pg_index i
+ WHERE c.relname = '%s' AND i.indkey = '%s' and c.oid = i.indrelid
AND i.indexrelid = c2.oid
+ ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;""" %
(table_name, attnum) )
+ for row in cursor.fetchall():
+ #print 'Index found on', table_name, column_name, row[0]
+ if not row[1] and not row[2]: # primary or unique
+ dict['db_index'] = row[0]
# default value check goes here
#cursor.execute("SELECT character_maximum_length, is_nullable,
column_default FROM information_schema.columns WHERE table_name = %s AND
column_name = %s" , [table_name,column_name])
#print 'cursor.fetchall()', cursor.fetchall()
=======================================
--- /trunk/src/deseb/backends/sqlite3.py Sat Sep 27 01:34:40 2008
+++ /trunk/src/deseb/backends/sqlite3.py Mon Sep 28 13:25:00 2009
@@ -39,7 +39,7 @@
model = self.get_model_from_table_name(table_name)
if not model: return []
output = []
- output.append('-- FYI: so we create a new ' + qn(table_name) +'
and delete the old ')
+ output.append('-- FYI: we create a new ' + qn(table_name) +' and
delete the old ')
output.append('-- FYI: this could take a while if you have a lot
of data')

tmp_table_name = table_name + '_1337_TMP' # unlikely to produce a
namespace conflict
@@ -74,7 +74,7 @@
if not model:
return ['-- model not found']
output = []
- output.append('-- FYI: sqlite does not support renaming columns')
+ output.append('-- FYI: renaming "%s" to "%s"' % (old_col_name,
new_col_name))
return output

def get_change_column_def_sql(self, table_name, col_name, col_type, f,
column_flags, f_default, updates):
@@ -87,8 +87,7 @@
if not model:
return ['-- model not found']
output = []
- output.append('-- FYI: sqlite does not support changing columns')
- output.append('-- FYI: have to change "%s"' % col_name)
+ output.append('-- FYI: changing traits for column "%s"' % col_name)
return output

def get_add_column_sql(self, table_name, col_name, col_type, null,
unique, primary_key, default):
@@ -100,7 +99,7 @@
tqn = lambda s: self.style.SQL_TABLE(qn(s))
fqn = lambda s: self.style.SQL_FIELD(qn(s))
if unique or primary_key or not null:
- output.append('-- FYI: sqlite does not support adding primary
keys or unique or not null fields')
+ output.append('-- FYI: adding column "%s"' % col_name)
else:
output.append(' '.join(
[ kw('ALTER TABLE'), tqn(table_name),
@@ -109,7 +108,7 @@

def get_drop_column_sql(self, table_name, col_name):
output = []
- output.append('-- FYI: sqlite does not support deleting columns')
+ output.append('-- FYI: deleting column "%s"' % col_name)
return output

def get_drop_table_sql( self, delete_tables):
=======================================
--- /trunk/src/deseb/schema_evolution.py Tue Jun 9 15:42:17 2009
+++ /trunk/src/deseb/schema_evolution.py Mon Sep 28 13:25:00 2009
@@ -198,7 +198,6 @@

def get_field_default(f):
from django.db.models.fields import NOT_PROVIDED
- print f.default, f.blank
if callable(f.default):
if f.blank: return ''
else: return NOT_PROVIDED
@@ -247,7 +246,8 @@
else:
continue # no idea what column you're talking about - should
be handled by get_sql_evolution_check_for_new_fields())
data_type = f.get_internal_type()
- if data_types.has_key(data_type):
+ #if data_types.has_key(data_type):
+ if not f.db_type() is None:
#print cf, data_type, f_col_type
is_postgresql = settings.DATABASE_ENGINE in
['postgresql', 'postgresql_psycopg2']
column_flags = introspection.get_known_column_flags(cursor,
db_table, cf)
@@ -258,13 +258,15 @@
update_unique = ( column_flags['unique']!=f.unique and not
(f.primary_key) )
update_null = column_flags['allow_null'] != f.null and not
f.primary_key
update_primary = column_flags['primary_key']!=f.primary_key
+ has_index = column_flags.get('db_index') != None
+ update_index = 'db_index' in column_flags and has_index !=
f.db_index and not f.unique
update_sequences = False
if column_flags.has_key('sequence'):
correct_seq_name = klass._meta.db_table+"_"+cf+'_seq'
update_sequences = column_flags['sequence'] !=
correct_seq_name
#if DEBUG and update_length:
# print f_col_type, column_flags['coltype'],
column_flags['maxlength']
- if update_type or update_null or update_length or
update_unique or update_primary or update_sequences:
+ if update_type or update_null or update_length or
update_unique or update_index or update_primary or update_sequences:
#print "cf, f.default, column_flags['default']", cf,
f.default, column_flags['default'], f.default.__class__
f_default = get_field_default(f)
updates = {
@@ -272,6 +274,7 @@
'update_length': update_length,
'update_unique': update_unique,
'update_null': update_null,
+ 'update_index': update_index,
'update_primary': update_primary,
'update_sequences': update_sequences,
}
@@ -728,8 +731,8 @@
if schema_fingerprint in seen_schema_fingerprints:
break

- except NotNullColumnNeedsDefaultException as e:
+ except NotNullColumnNeedsDefaultException:
print 'Exception - Not null columns need to have a default value:'
- print '\t', e
+ print '\t', sys.exc_info()[1], ':', sys.exc_info()[0]
# traceback.print_exc()

Reply all
Reply to author
Forward
0 new messages