diff --git a/code_comments/db.py b/code_comments/db.py index 1202aac..17f2530 100644 --- a/code_comments/db.py +++ b/code_comments/db.py @@ -4,7 +4,7 @@ from trac.db.api import DatabaseManager # Database version identifier for upgrades. -db_version = 1 +db_version = 2 # Database schema schema = { @@ -13,7 +13,7 @@ Column('version', type='int'), Column('text'), Column('path'), - Column('revision', type='int'), + Column('revision'), Column('line', type='int'), Column('author'), Column('time', type='int'), @@ -36,7 +36,23 @@ def create_tables(env, db): str(db_version)) # Upgrades def upgrade_from_1_to_2(env, db): - pass + columns = [c.name for c in schema['code_comments'].columns] + cursor = db.cursor() + values = cursor.execute('PRAGMA INDEX_LIST(`code_comments`)') + indexes = values.fetchall() + for index in indexes: + if index[1].startswith('code_comments'): + cursor.execute('DROP INDEX IF EXISTS %s' % index[1]) + values = cursor.execute('SELECT %s FROM code_comments' % ','.join(columns)) + lines = values.fetchall() + cursor.execute('ALTER TABLE code_comments RENAME TO tmp_code_comments') + for stmt in to_sql(env, schema['code_comments']): + cursor.execute(stmt) + for line in lines: + ins = 'INSERT INTO code_comments (%s) VALUES (%s)' % (','.join(columns), ','.join(['\'%s\'' % str(v).replace('\'','\'\'') for v in line])) + print ins + cursor.execute(ins) + cursor.execute('DROP TABLE tmp_code_comments') upgrade_map = { 2: upgrade_from_1_to_2