view src/ltpdarepo/upgrade.py @ 0:c812c3020b63

Initial import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Thu, 09 Jun 2011 13:16:24 +0200
parents
children ceb5df800c51
line wrap: on
line source

import MySQLdb as db

import itertools
from functools import partial

from .config import HOSTNAME, DATABASE, USERNAME, PASSWORD

# counter
counter = itertools.count()

# upgrade steps register
steps = []


def register(r0, r1, func=None, count=None):
    if func == None:
        return partial(register, r0, r1)
    steps.append((r0, r1, count, func))
    return func


def upgrade():
    conn = db.connect(host=HOSTNAME, db=DATABASE,
                      user=USERNAME, passwd=PASSWORD)
    curs = conn.cursor()

    # current schema version
    curs.execute("""SELECT value+0 FROM options WHERE name='version'""")
    schema = curs.fetchone()[0]

    # filter applicable upgrade steps
    todo = filter(lambda x: x[0] >= schema, steps)

    # iter upgrade steps
    for r, to, count, step in sorted(todo):

        # run upgrade step
        # print 'from %g to %g: %s.%s' % (r, to, step.__module__, step.__name__)
        step(conn)

        # update schema version
        curs.execute("""UPDATE options SET value=%s
                        WHERE name='version'""", str(to))

    conn.commit()
    conn.close()


@register(2.4, 2.41)
def set_strict_mode(conn):
    curs = conn.cursor()
    curs.execute("""SET GLOBAL sql_mode='STRICT_TRANS_TABLES'""")
    conn.commit()


@register(2.41, 2.5)
def upgrade_24_to_25(conn):
    curs = conn.cursor()

    # consolidate privileges: there is no need to specify grants
    # both for 'localhost' and for '%' hosts. drop privileges granted
    # for 'localhost'
    curs.execute("""DELETE mysql.db FROM mysql.db, users
                    WHERE User=username AND Host='localhost'""")

    # drop privileges granted explicitly on transactions tables
    curs.execute("""DELETE mysql.tables_priv FROM mysql.tables_priv, users
                    WHERE User=username AND Table_name='transactions'""")

    # tell mysql to reload grant tables
    curs.execute("FLUSH PRIVILEGES")

    # drop unused tables
    curs.execute("DROP TABLE IF EXISTS user_access")
    curs.execute("DROP TABLE IF EXISTS user_hosts")

    # drop password column from users table in administrative
    # database: authentication is done using mysql database
    curs.execute("ALTER TABLE users DROP COLUMN password")

    conn.commit()


# @register(2.5, 2.6)
def upgrade_25_to_26(conn):
    curs = conn.cursor()

    conn.commit()