cmlenz@39: #!/usr/bin/env python cmlenz@39: import os cmlenz@39: import sys cmlenz@39: import sqlite cmlenz@39: import ConfigParser cmlenz@39: from trac.env import Environment cmlenz@39: cmlenz@39: additional_config = \ cmlenz@39: (('notification', 'smtp_enabled', 'false'), cmlenz@39: ('notification', 'smtp_server', 'localhost'), cmlenz@39: ('notification', 'smtp_replyto', 'trac@localhost'), cmlenz@39: ('logging', 'log_type', 'none'), cmlenz@39: ('logging', 'log_file', 'trac.log'), cmlenz@39: ('logging', 'log_level', 'DEBUG'), cmlenz@39: ('attachment', 'max_size', '262144'), cmlenz@39: ('trac', 'default_charset', 'iso-8859-15'), cmlenz@39: ('trac', 'database', 'sqlite:db/trac.db')) cmlenz@39: cmlenz@39: def db2env(db_path, env_path): cmlenz@39: env = Environment(env_path, create=1) cmlenz@39: # Open the databases cmlenz@39: old_cnx = sqlite.connect(db_path) cmlenz@39: new_cnx = env.get_db_cnx() cmlenz@39: old_cursor = old_cnx.cursor() cmlenz@39: new_cursor = new_cnx.cursor() cmlenz@39: convert_config(env, old_cursor) cmlenz@39: convert_db(old_cursor, new_cursor) cmlenz@39: new_cursor.execute("INSERT INTO system VALUES('database_version', '7')") cmlenz@39: new_cnx.commit() cmlenz@39: cmlenz@39: def convert_config(env, old_cursor): cmlenz@39: old_cursor.execute('SELECT section, name, value FROM config') cmlenz@39: while 1: cmlenz@39: row = old_cursor.fetchone() cmlenz@39: if not row: cmlenz@39: break cmlenz@39: row = [row[0], row[1], row[2]] cmlenz@39: if row[0] == 'general': cmlenz@39: row[0] = 'trac' cmlenz@39: if row[1] == 'database_version': cmlenz@39: continue cmlenz@39: env.set_config(row[0], row[1], row[2]) cmlenz@39: for v in additional_config: cmlenz@39: env.set_config(*v) cmlenz@39: env.save_config() cmlenz@39: cmlenz@39: def to_utf8(row): cmlenz@39: x = [] cmlenz@39: for v in row: cmlenz@39: if type(v) == type(''): cmlenz@39: try: cmlenz@39: u = unicode(v, 'utf-8') cmlenz@39: x.append(v) cmlenz@39: except UnicodeError: cmlenz@39: u = unicode(v, 'iso-8859-15') cmlenz@39: x.append(u.encode('utf-8')) cmlenz@39: else: cmlenz@39: x.append(v) cmlenz@39: return x cmlenz@39: cmlenz@39: def copy_tuples(table, from_cursor, to_cursor, fields='*'): cmlenz@39: from_cursor.execute('SELECT %s FROM %s' % (fields, table)) cmlenz@39: while 1: cmlenz@39: row = from_cursor.fetchone() cmlenz@39: if not row: cmlenz@39: break cmlenz@39: row = to_utf8(row) cmlenz@39: if fields == '*': cmlenz@39: to_cursor.execute('INSERT INTO %s VALUES(%s)' \ cmlenz@39: % (table, ', '.join(['%s'] * len(row))), *row) cmlenz@39: else: cmlenz@39: to_cursor.execute('INSERT INTO %s (%s) VALUES(%s)' \ cmlenz@39: % (table, fields, cmlenz@39: ', '.join(['%s'] * len(row))), *row) cmlenz@39: cmlenz@39: def convert_db(old_cursor, new_cursor): cmlenz@39: copy_tuples('revision', old_cursor, new_cursor) cmlenz@39: copy_tuples('node_change', old_cursor, new_cursor) cmlenz@39: copy_tuples('auth_cookie', old_cursor, new_cursor) cmlenz@39: copy_tuples('enum', old_cursor, new_cursor) cmlenz@39: copy_tuples('ticket_change', old_cursor, new_cursor) cmlenz@39: copy_tuples('permission', old_cursor, new_cursor) cmlenz@39: copy_tuples('component', old_cursor, new_cursor) cmlenz@39: copy_tuples('milestone', old_cursor, new_cursor, "name, time") cmlenz@39: new_cursor.execute("UPDATE milestone SET descr=''") cmlenz@39: copy_tuples('version', old_cursor, new_cursor) cmlenz@39: copy_tuples('report', old_cursor, new_cursor, cmlenz@39: 'id,author,title,sql') cmlenz@39: copy_tuples('ticket', old_cursor, new_cursor, cmlenz@39: 'id,time,changetime,component,severity,priority,' cmlenz@39: 'owner,reporter,cc,url,version,milestone,status,' cmlenz@39: 'resolution,summary,description') cmlenz@39: copy_tuples('wiki', old_cursor, new_cursor, cmlenz@39: 'name,version,time,author,ipnr,text') cmlenz@39: cmlenz@39: if __name__ == '__main__': cmlenz@39: if len(sys.argv) != 3: cmlenz@39: print >> sys.stderr, 'Usage: %s \n' % sys.argv[0] cmlenz@39: print >> sys.stderr, \ cmlenz@39: 'Creates a new Trac environment and initializes it with ' \ cmlenz@39: 'information\nfrom an existing pre 0.7 trac database.' cmlenz@39: print >> sys.stderr cmlenz@39: sys.exit(1) cmlenz@39: db2env(sys.argv[1], sys.argv[2]) cmlenz@39: print >> sys.stderr, 'Environment successfully created.'