#!/usr/local/bin/python2.6
# -*- coding: UTF-8 -*-
"""
Update from version 1 to version 2 of Yokadi DB

@author: Aurélien Gâteau <aurelien.gateau@free.fr>
@license: GPLv3
"""
import sys

from sqlobject import connectionForURI, BoolCol, DateTimeCol, sqlhub, SQLObject, UnicodeCol

""" This is the v2 table """
class Config(SQLObject):
    """yokadi config"""
    class sqlmeta:
        defaultOrder = "name"
    name  = UnicodeCol(alternateID=True, notNone=True)
    value = UnicodeCol(default="", notNone=True)
    system = BoolCol(default=False, notNone=True)
    desc = UnicodeCol(default="", notNone=True)


def createConfigTable():
    Config.createTable()
    rows = [
        ("DB_VERSION"     , "2"      , True , "Database schema release number"),
        ("TEXT_WIDTH"     , "60"     , False, "Width of task display output with t_list command"),
        ("DEFAULT_PROJECT", "default", False, "Default project used when no project name given"),
        ("ALARM_DELAY_CMD", '''kdialog --sorry "task {TITLE} ({ID}) is due for {DATE}" --title "Yokadi Daemon"''', False,
                             "Command executed by Yokadi Daemon when a tasks due date is reached soon (see ALARM_DELAY"),
        ("ALARM_DUE_CMD"  , '''kdialog --error "task {TITLE} ({ID}) should be done now" --title "Yokadi Daemon"''', False,
                             "Command executed by Yokadi Daemon when a tasks due date is reached soon (see ALARM_DELAY")
        ]
    for name, value, system, desc in rows:
        Config(name=name, value=value, system=system, desc=desc)


def alterProjectTable():
    class Project(SQLObject):
        pass
    Project.sqlmeta.addColumn(BoolCol("active", default=True), changeSchema=True)
    for project in Project.select():
        project.active = True


def alterTaskTable():
    class Task(SQLObject):
        pass
    Task.sqlmeta.addColumn(DateTimeCol("dueDate", default=None), changeSchema=True)


def main():
    sqlhub.processConnection = connectionForURI('sqlite:' + sys.argv[1])
    createConfigTable()
    alterProjectTable()
    alterTaskTable()


if __name__ == "__main__":
    main()
# vi: ts=4 sw=4 et
