trac-ticketlinks / scripts / tracdb2env

#!/usr/bin/env python
import os
import sys
import sqlite
import ConfigParser
from trac.env import Environment

additional_config = \
(('notification', 'smtp_enabled', 'false'),
 ('notification', 'smtp_server', 'localhost'),
 ('notification', 'smtp_replyto', 'trac@localhost'),
 ('logging', 'log_type', 'none'),
 ('logging', 'log_file', 'trac.log'),
 ('logging', 'log_level', 'DEBUG'),
 ('attachment', 'max_size', '262144'),
 ('trac', 'default_charset', 'iso-8859-15'),
 ('trac', 'database', 'sqlite:db/trac.db'))

def db2env(db_path, env_path):
    env = Environment(env_path, create=1)
    # Open the databases
    old_cnx = sqlite.connect(db_path)
    new_cnx = env.get_db_cnx()
    old_cursor = old_cnx.cursor()
    new_cursor = new_cnx.cursor()
    convert_config(env, old_cursor)
    convert_db(old_cursor, new_cursor)
    new_cursor.execute("INSERT INTO system VALUES('database_version', '7')")

def convert_config(env, old_cursor):
    old_cursor.execute('SELECT section, name, value FROM config')
    while 1:
        row = old_cursor.fetchone()
        if not row:
        row = [row[0], row[1], row[2]]
        if row[0] == 'general':
            row[0] = 'trac'
        if row[1] == 'database_version':
        env.set_config(row[0], row[1], row[2])
    for v in additional_config:

def to_utf8(row):
    x = []
    for v in row:
        if type(v) == type(''):
                u = unicode(v, 'utf-8')
            except UnicodeError:
                u = unicode(v, 'iso-8859-15')
    return x

def copy_tuples(table, from_cursor, to_cursor, fields='*'):
    from_cursor.execute('SELECT %s FROM %s' % (fields, table))
    while 1:
        row = from_cursor.fetchone()
        if not row:
        row = to_utf8(row)
        if fields == '*':
            to_cursor.execute('INSERT INTO %s VALUES(%s)' \
                              % (table, ', '.join(['%s'] * len(row))), *row)
            to_cursor.execute('INSERT INTO %s (%s) VALUES(%s)' \
                              % (table, fields,
                                 ', '.join(['%s'] * len(row))), *row)

def convert_db(old_cursor, new_cursor):
    copy_tuples('revision', old_cursor, new_cursor)
    copy_tuples('node_change', old_cursor, new_cursor)
    copy_tuples('auth_cookie', old_cursor, new_cursor)
    copy_tuples('enum', old_cursor, new_cursor)
    copy_tuples('ticket_change', old_cursor, new_cursor)
    copy_tuples('permission', old_cursor, new_cursor)
    copy_tuples('component', old_cursor, new_cursor)
    copy_tuples('milestone', old_cursor, new_cursor, "name, time")
    new_cursor.execute("UPDATE milestone SET descr=''")
    copy_tuples('version', old_cursor, new_cursor)
    copy_tuples('report', old_cursor, new_cursor,
    copy_tuples('ticket', old_cursor, new_cursor,
    copy_tuples('wiki', old_cursor, new_cursor,

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print >> sys.stderr, 'Usage: %s <db-file> <env-dir>\n' % sys.argv[0]
        print >> sys.stderr, \
              'Creates a new Trac environment and initializes it with ' \
              'information\nfrom an existing pre 0.7 trac database.'
        print >> sys.stderr
    db2env(sys.argv[1], sys.argv[2])
    print >> sys.stderr, 'Environment successfully created.'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.