Binary strings a too hard to manage in DB queries and XMLRPC methods,
as we get all kinds of encoding issues.
Change HashField to use a hex string, and add a migration script for db
updates. The patches should be rehashed after migration.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
class Meta:
ordering = ['ordering']
class Meta:
ordering = ['ordering']
-class HashField(models.Field):
+class HashField(models.CharField):
__metaclass__ = models.SubfieldBase
def __init__(self, algorithm = 'sha1', *args, **kwargs):
__metaclass__ = models.SubfieldBase
def __init__(self, algorithm = 'sha1', *args, **kwargs):
try:
import hashlib
self.hashlib = True
try:
import hashlib
self.hashlib = True
+ n_bytes = len(hashlib.new(self.algorithm).hexdigest())
except ImportError:
self.hashlib = False
if algorithm == 'sha1':
except ImportError:
self.hashlib = False
if algorithm == 'sha1':
self.hash_constructor = md5.new
else:
raise NameError("Unknown algorithm '%s'" % algorithm)
self.hash_constructor = md5.new
else:
raise NameError("Unknown algorithm '%s'" % algorithm)
+ n_bytes = len(self.hash_constructor().hexdigest())
+
+ kwargs['max_length'] = n_bytes
super(HashField, self).__init__(*args, **kwargs)
def db_type(self):
if self.hashlib:
import hashlib
super(HashField, self).__init__(*args, **kwargs)
def db_type(self):
if self.hashlib:
import hashlib
- n_bytes = len(hashlib.new(self.algorithm).digest())
- else:
- n_bytes = len(self.hash_constructor().digest())
- if settings.DATABASE_ENGINE.startswith('postgresql'):
- return 'bytea'
- elif settings.DATABASE_ENGINE == 'mysql':
- return 'binary(%d)' % n_bytes
+ n_bytes = len(hashlib.new(self.algorithm).hexdigest())
- raise Exception("Unknown database engine '%s'" % \
- settings.DATABASE_ENGINE)
-
- def to_python(self, value):
- return value
-
- def get_db_prep_save(self, value):
- return ''.join(map(lambda x: '\\%03o' % ord(x), value))
-
- def get_manipulator_field_objs(self):
- return [oldforms.TextField]
+ n_bytes = len(self.hash_constructor().hexdigest())
+ return 'char(%d)' % n_bytes
class Patch(models.Model):
project = models.ForeignKey(Project)
class Patch(models.Model):
project = models.ForeignKey(Project)
headers = models.TextField(blank = True)
content = models.TextField()
commit_ref = models.CharField(max_length=255, null = True, blank = True)
headers = models.TextField(blank = True)
content = models.TextField()
commit_ref = models.CharField(max_length=255, null = True, blank = True)
- hash = HashField(null = True)
+ hash = HashField(null = True, db_index = True)
def __str__(self):
return self.name
def __str__(self):
return self.name
self.state = State.objects.get(ordering = 0)
if self.hash is None:
self.state = State.objects.get(ordering = 0)
if self.hash is None:
- self.hash = hash_patch(self.content).digest()
+ self.hash = hash_patch(self.content).hexdigest()
super(Patch, self).save()
super(Patch, self).save()
--- /dev/null
+BEGIN;
+ALTER TABLE patchwork_patch ALTER COLUMN hash DROP NOT NULL;
+UPDATE patchwork_patch SET hash = NULL;
+COMMIT;
+BEGIN;
+ALTER TABLE patchwork_patch ALTER COLUMN hash TYPE CHAR(40);
+CREATE INDEX "patchwork_patch_hash" ON "patchwork_patch" ("hash");
+COMMIT;