db18.py :  » Project-Management » Trac » Trac-0.11.7 » trac » upgrades » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Project Management » Trac 
Trac » Trac 0.11.7 » trac » upgrades » db18.py
from trac.db import Table,Column,Index,DatabaseManager

def do_upgrade(env, ver, cursor):
    cursor.execute("CREATE TEMPORARY TABLE session_old AS SELECT * FROM session")
    cursor.execute("DROP TABLE session")
    cursor.execute("CREATE TEMPORARY TABLE ticket_change_old AS SELECT * FROM ticket_change")
    cursor.execute("DROP TABLE ticket_change")

    # A more normalized session schema where the attributes are stored in
    # a separate table
    tables = [Table('session', key=('sid', 'authenticated'))[
                Column('sid'),
                Column('authenticated', type='int'),
                Column('last_visit', type='int'),
                Index(['last_visit']),
                Index(['authenticated'])],
              Table('session_attribute', key=('sid', 'authenticated', 'name'))[
                Column('sid'),
                Column('authenticated', type='int'),
                Column('name'),
                Column('value')],
              Table('ticket_change', key=('ticket', 'time', 'field'))[
                Column('ticket', type='int'),
                Column('time', type='int'),
                Column('author'),
                Column('field'),
                Column('oldvalue'),
                Column('newvalue'),
                Index(['ticket']),
                Index(['time'])]]
    
    db_connector, _ = DatabaseManager(env)._get_connector()
    for table in tables:
        for stmt in db_connector.to_sql(table):
            cursor.execute(stmt)

    # Add an index to the temporary table to speed up the conversion
    cursor.execute("CREATE INDEX session_old_sid_idx ON session_old(sid)")
    # Insert the sessions into the new table
    db = env.get_db_cnx()
    cursor.execute("INSERT INTO session (sid, last_visit, authenticated) "
                   "SELECT distinct s.sid,COALESCE(%s,0),s.authenticated "
                   "FROM session_old AS s LEFT JOIN session_old AS s2 "
                   "ON (s.sid=s2.sid AND s2.var_name='last_visit') "
                   "WHERE s.sid IS NOT NULL"
                   % db.cast('s2.var_value', 'int'))
    cursor.execute("INSERT INTO session_attribute "
                   "(sid, authenticated, name, value) "
                   "SELECT s.sid, s.authenticated, s.var_name, s.var_value "
                   "FROM session_old s "
                   "WHERE s.var_name <> 'last_visit' AND s.sid IS NOT NULL")

    # Insert ticket change data into the new table
    cursor.execute("INSERT INTO ticket_change "
                   "(ticket, time, author, field, oldvalue, newvalue) "
                   "SELECT ticket, time, author, field, oldvalue, newvalue "
                   "FROM ticket_change_old")

    cursor.execute("DROP TABLE session_old")
    cursor.execute("DROP TABLE ticket_change_old")

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.