summaryrefslogtreecommitdiffstats
path: root/kittystore/storm/schema/patch_5.py
blob: 1ce101ba83d2308529b27b914af3589814cabcf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-

from __future__ import absolute_import

from . import get_db_type
from kittystore.storm.model import Thread
from kittystore.analysis import compute_thread_order_and_depth


SQL = {
    "sqlite": [
        'ALTER TABLE "email" ADD COLUMN "thread_order" INTEGER NOT NULL DEFAULT 0;',
        'ALTER TABLE "email" ADD COLUMN "thread_depth" INTEGER NOT NULL DEFAULT 0;',
        'CREATE INDEX "ix_email_thread_order" ON "email" (thread_order);',
        ],
    "postgres": [
        'ALTER TABLE "email" ADD COLUMN "thread_order" INTEGER NOT NULL DEFAULT 0;',
        'ALTER TABLE "email" ADD COLUMN "thread_depth" INTEGER NOT NULL DEFAULT 0;',
        'CREATE INDEX "ix_email_thread_order" ON "email" USING btree (thread_order);',
        ],
    "mysql": [
        'ALTER TABLE `email` ADD COLUMN `thread_order` INTEGER NOT NULL DEFAULT 0;',
        'ALTER TABLE `email` ADD COLUMN `thread_depth` INTEGER NOT NULL DEFAULT 0;',
        'CREATE INDEX `ix_email_thread_order` ON `email` (thread_order);',
        ],
    }


def apply(store):
    """Add the thread_order and thread_depth columns and populate them"""
    dbtype = get_db_type(store)
    for statement in SQL[dbtype]:
        store.execute(statement)
    for thread in store.find(Thread):
        compute_thread_order_and_depth(thread)
        store.add(thread)
    store.commit()