diff --git a/trac/db_default.py b/trac/db_default.py
--- a/trac/db_default.py
+++ b/trac/db_default.py
@@ -17,7 +17,7 @@
 from trac.db import Table, Column, Index
 
 # Database version identifier. Used for automatic upgrades.
-db_version = 25
+db_version = 26
 
 def __mkreports(reports):
     """Utility function used to create report data in same syntax as the
diff --git a/trac/upgrades/db26.py b/trac/upgrades/db26.py
new file mode 100644
--- /dev/null
+++ b/trac/upgrades/db26.py
@@ -0,0 +1,20 @@
+
+def do_upgrade(env, ver, cursor):
+    """Zero-pad Subversion revision numbers in the cache."""
+    cursor.execute("""
+        SELECT id, value FROM repository WHERE name='repository_dir'
+        """)
+    for id in [id for id, dir in cursor if dir.startswith('svn:')]:
+        cursor.execute("SELECT DISTINCT rev FROM revision WHERE repos=%s",
+                       (id,))
+        for rev in set(row[0] for row in cursor):
+            cursor.execute("""
+                UPDATE revision SET rev=%s WHERE repos=%s AND rev=%s
+                """, ('%010d' % int(rev), id, rev))
+        
+        cursor.execute("SELECT DISTINCT rev FROM node_change WHERE repos=%s",
+                       (id,))
+        for rev in set(row[0] for row in cursor):
+            cursor.execute("""
+                UPDATE node_change SET rev=%s WHERE repos=%s AND rev=%s
+                """, ('%010d' % int(rev), id, rev))
diff --git a/trac/versioncontrol/cache.py b/trac/versioncontrol/cache.py
--- a/trac/versioncontrol/cache.py
+++ b/trac/versioncontrol/cache.py
@@ -83,6 +83,7 @@
 
     def sync_changeset(self, rev):
         cset = self.repos.get_changeset(rev)
+        srev = '%010d' % cset.rev
         old_cset = [None]
 
         @with_transaction(self.env)
@@ -91,7 +92,7 @@
             cursor.execute("""
                 SELECT time,author,message FROM revision
                 WHERE repos=%s AND rev=%s
-                """, (self.id, str(cset.rev)))
+                """, (self.id, srev))
             for time, author, message in cursor:
                 old_cset[0] = Changeset(self.repos, cset.rev, message, author,
                                         from_utimestamp(time))
@@ -99,7 +100,7 @@
                 UPDATE revision SET time=%s, author=%s, message=%s
                 WHERE repos=%s AND rev=%s
                 """, (to_utimestamp(cset.date), cset.author, cset.message,
-                      self.id, str(cset.rev)))
+                      self.id, srev))
         return old_cset[0]
         
     def _metadata(self, db):
@@ -205,11 +206,12 @@
 
             if next_youngest is None: # nothing to cache yet
                 return
+            srev = '%010d' % next_youngest
 
             # 0. first check if there's no (obvious) resync in progress
             cursor.execute("""
                SELECT rev FROM revision WHERE repos=%s AND rev=%s
-               """, (self.id, str(next_youngest)))
+               """, (self.id, srev))
             for rev, in cursor:
                 # already there, but in progress, so keep ''previous''
                 # notion of 'youngest'
@@ -223,6 +225,7 @@
             actionmap = dict(zip(_actionmap.values(), _actionmap.keys()))
 
             while next_youngest is not None:
+                srev = '%010d' % next_youngest
                 
                 # 1.1 Attempt to resync the 'revision' table
                 self.log.info("Trying to sync revision [%s]",
@@ -233,8 +236,7 @@
                         INSERT INTO revision
                             (repos,rev,time,author,message)
                         VALUES (%s,%s,%s,%s,%s)
-                        """, (self.id, str(next_youngest),
-                              to_utimestamp(cset.date),
+                        """, (self.id, srev, to_utimestamp(cset.date),
                               cset.author, cset.message))
                 except Exception, e: # *another* 1.1. resync attempt won 
                     self.log.warning('Revision %s already cached: %r',
@@ -251,7 +253,7 @@
                 for path, kind, action, bpath, brev in cset.get_changes():
                     self.log.debug("Caching node change in [%s]: %r",
                                    next_youngest,
-                                   (path,kind,action,bpath,brev))
+                                   (path, kind, action, bpath, brev))
                     kind = kindmap[kind]
                     action = actionmap[action]
                     cursor.execute("""
@@ -259,8 +261,7 @@
                             (repos,rev,path,node_type,
                              change_type,base_path,base_rev)
                         VALUES (%s,%s,%s,%s,%s,%s,%s)
-                        """, (self.id, str(next_youngest),
-                              path, kind, action, bpath, brev))
+                        """, (self.id, srev, path, kind, action, bpath, brev))
 
                 # 1.3. iterate (1.1 should always succeed now)
                 youngest = next_youngest
@@ -286,26 +287,25 @@
         revisions.
         """
         last = self.normalize_rev(last)
-        node = self.get_node(path, last)    # Check node existence and perms
+        slast = '%010d' % last
+        node = self.get_node(path, last)    # Check node existence
         db = self.env.get_db_cnx()
         cursor = db.cursor()
-        rev_as_int = db.cast('rev', 'int')
         if first is None:
             cursor.execute("SELECT rev FROM node_change "
-                           "WHERE repos=%%s AND %s<=%%s "
-                           "  AND path=%%s "
+                           "WHERE repos=%s AND rev<=%s "
+                           "  AND path=%s "
                            "  AND change_type IN ('A', 'C', 'M') "
-                           "ORDER BY %s DESC "
-                           "LIMIT 1" % ((rev_as_int,) * 2),
-                           (self.id, last, path))
+                           "ORDER BY rev DESC LIMIT 1",
+                           (self.id, slast, path))
             first = 0
             for row in cursor:
                 first = int(row[0])
+        sfirst = '%010d' % first
         cursor.execute("SELECT DISTINCT rev FROM node_change "
-                       "WHERE repos=%%s AND %s>=%%s AND %s<=%%s "
-                       " AND (path=%%s OR path %s)" % 
-                       (rev_as_int, rev_as_int, db.like()),
-                       (self.id, first, last, path,
+                       "WHERE repos=%%s AND rev>=%%s AND rev<=%%s "
+                       " AND (path=%%s OR path %s)" % db.like(),
+                       (self.id, sfirst, slast, path,
                         db.like_escape(path + '/') + '%'))
         return [int(row[0]) for row in cursor]
 
@@ -331,11 +331,12 @@
             return self.repos.next_rev(self.normalize_rev(rev), path)
 
     def _next_prev_rev(self, direction, rev, path=''):
+        srev = '%010d' % rev
         db = self.env.get_db_cnx()
         # the changeset revs are sequence of ints:
         sql = "SELECT rev FROM node_change WHERE repos=%s AND " + \
-              db.cast('rev', 'int') + direction + "%s"
-        args = [self.id, rev]
+              "rev" + direction + "%s"
+        args = [self.id, srev]
 
         if path:
             path = path.lstrip('/')
@@ -349,8 +350,8 @@
             for i in range(1, len(components) + 1):
                 args.append('/'.join(components[:i]))
 
-        sql += " ORDER BY " + db.cast('rev', 'int') + \
-                (direction == '<' and " DESC" or "") + " LIMIT 1"
+        sql += " ORDER BY rev" + (direction == '<' and " DESC" or "") \
+               + " LIMIT 1"
         
         cursor = db.cursor()
         cursor.execute(sql, args)
@@ -396,7 +397,7 @@
         cursor = db.cursor()
         cursor.execute("SELECT time,author,message FROM revision "
                        "WHERE repos=%s AND rev=%s",
-                       (repos.id, str(rev)))
+                       (repos.id, '%010d' % rev))
         row = cursor.fetchone()
         if row:
             _date, author, message = row
@@ -411,7 +412,7 @@
         cursor = db.cursor()
         cursor.execute("SELECT path,node_type,change_type,base_path,base_rev "
                        "FROM node_change WHERE repos=%s AND rev=%s "
-                       "ORDER BY path", (self.repos.id, str(self.rev)))
+                       "ORDER BY path", (self.repos.id, '%010d' % self.rev))
         for path, kind, change, base_path, base_rev in cursor:
             kind = _kindmap[kind]
             change = _actionmap[change]
diff --git a/trac/versioncontrol/tests/cache.py b/trac/versioncontrol/tests/cache.py
--- a/trac/versioncontrol/tests/cache.py
+++ b/trac/versioncontrol/tests/cache.py
@@ -71,7 +71,7 @@
                     """, [(rev[0],) + change for change in changes])
         cursor.execute("""
             UPDATE repository SET value=%s WHERE id=1 AND name='youngest_rev'
-            """, (args[-1][0][0],))
+            """, (str(int(args[-1][0][0])),))
 
     # Tests
 
@@ -102,17 +102,18 @@
 
         cursor = self.db.cursor()
         cursor.execute("SELECT rev,time,author,message FROM revision")
-        self.assertEquals(('0', to_utimestamp(t1), '', ''), cursor.fetchone())
-        self.assertEquals(('1', to_utimestamp(t2), 'joe', 'Import'),
+        self.assertEquals(('0000000000', to_utimestamp(t1), '', ''),
+                          cursor.fetchone())
+        self.assertEquals(('0000000001', to_utimestamp(t2), 'joe', 'Import'),
                           cursor.fetchone())
         self.assertEquals(None, cursor.fetchone())
         cursor.execute("""
             SELECT rev,path,node_type,change_type,base_path,base_rev
             FROM node_change
             """)
-        self.assertEquals(('1', 'trunk', 'D', 'A', None, None),
+        self.assertEquals(('0000000001', 'trunk', 'D', 'A', None, None),
                           cursor.fetchone())
-        self.assertEquals(('1', 'trunk/README', 'F', 'A', None, None),
+        self.assertEquals(('0000000001', 'trunk/README', 'F', 'A', None, None),
                           cursor.fetchone())
         self.assertEquals(None, cursor.fetchone())
 
@@ -121,8 +122,8 @@
         t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
         t3 = datetime(2003, 1, 1, 1, 1, 1, 0, utc)
         self.preset_cache(
-            (('0', to_utimestamp(t1), '', ''), []),
-            (('1', to_utimestamp(t2), 'joe', 'Import'),
+            (('0000000000', to_utimestamp(t1), '', ''), []),
+            (('0000000001', to_utimestamp(t2), 'joe', 'Import'),
              [('trunk', 'D', 'A', None, None),
               ('trunk/README', 'F', 'A', None, None)]),
             )
@@ -141,14 +142,14 @@
 
         cursor = self.db.cursor()
         cursor.execute("""
-            SELECT time,author,message FROM revision WHERE rev='2'
+            SELECT time,author,message FROM revision WHERE rev='0000000002'
             """)
         self.assertEquals((to_utimestamp(t3), 'joe', 'Update'),
                           cursor.fetchone())
         self.assertEquals(None, cursor.fetchone())
         cursor.execute("""
             SELECT path,node_type,change_type,base_path,base_rev
-            FROM node_change WHERE rev='2'
+            FROM node_change WHERE rev='0000000002'
             """)
         self.assertEquals(('trunk/README', 'F', 'E', 'trunk/README', '1'),
                           cursor.fetchone())
@@ -194,11 +195,12 @@
             SELECT rev,path,node_type,change_type,base_path,base_rev
             FROM node_change ORDER BY rev
             """)
-        self.assertEquals(('1', 'trunk', 'D', 'A', None, None),
+        self.assertEquals(('0000000001', 'trunk', 'D', 'A', None, None),
                           cursor.fetchone())
-        self.assertEquals(('1', 'trunk/README', 'F', 'A', None, None),
+        self.assertEquals(('0000000001', 'trunk/README', 'F', 'A', None, None),
                           cursor.fetchone())
-        self.assertEquals(('2', 'trunk/README', 'F', 'E', 'trunk/README', '1'),
+        self.assertEquals(('0000000002', 'trunk/README', 'F', 'E', 'trunk/README',
+                           '1'),
                           cursor.fetchone())
         self.assertEquals(None, cursor.fetchone())
 
@@ -206,8 +208,8 @@
         t1 = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
         t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
         self.preset_cache(
-            (('0', to_utimestamp(t1), '', ''), []),
-            (('1', to_utimestamp(t2), 'joe', 'Import'),
+            (('0000000000', to_utimestamp(t1), '', ''), []),
+            (('0000000001', to_utimestamp(t2), 'joe', 'Import'),
              [('trunk', 'D', 'A', None, None),
               ('trunk/README', 'F', 'A', None, None)]),
             )
@@ -236,8 +238,8 @@
         t1 = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
         t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
         self.preset_cache(
-            (('0', to_utimestamp(t1), '', ''), []),
-            (('1', to_utimestamp(t2), 'joe', 'Import'),
+            (('0000000000', to_utimestamp(t1), '', ''), []),
+            (('0000000001', to_utimestamp(t2), 'joe', 'Import'),
              [('trunk', 'D', 'A', None, None),
               ('trunk/RDME', 'F', 'A', None, None)]),
             )

