Skip to content

Commit 3038b71

Browse files
committed
1 parent fce63fb commit 3038b71

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

code_comments/db.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from trac.db.api import DatabaseManager
55

66
# Database version identifier for upgrades.
7-
db_version = 1
7+
db_version = 2
88

99
# Database schema
1010
schema = {
@@ -13,7 +13,7 @@
1313
Column('version', type='int'),
1414
Column('text'),
1515
Column('path'),
16-
Column('revision', type='int'),
16+
Column('revision'),
1717
Column('line', type='int'),
1818
Column('author'),
1919
Column('time', type='int'),
@@ -36,7 +36,23 @@ def create_tables(env, db):
3636
str(db_version))
3737
# Upgrades
3838
def upgrade_from_1_to_2(env, db):
39-
pass
39+
columns = [c.name for c in schema['code_comments'].columns]
40+
cursor = db.cursor()
41+
values = cursor.execute('PRAGMA INDEX_LIST(`code_comments`)')
42+
indexes = values.fetchall()
43+
for index in indexes:
44+
if index[1].startswith('code_comments'):
45+
cursor.execute('DROP INDEX IF EXISTS %s' % index[1])
46+
values = cursor.execute('SELECT %s FROM code_comments' % ','.join(columns))
47+
lines = values.fetchall()
48+
cursor.execute('ALTER TABLE code_comments RENAME TO tmp_code_comments')
49+
for stmt in to_sql(env, schema['code_comments']):
50+
cursor.execute(stmt)
51+
for line in lines:
52+
ins = 'INSERT INTO code_comments (%s) VALUES (%s)' % (','.join(columns), ','.join(['\'%s\'' % str(v).replace('\'','\'\'') for v in line]))
53+
print ins
54+
cursor.execute(ins)
55+
cursor.execute('DROP TABLE tmp_code_comments')
4056

4157
upgrade_map = {
4258
2: upgrade_from_1_to_2

0 commit comments

Comments
 (0)