Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / versions / 165ba3fbfe53_update_path_account.py @ cd5033bd

History | View | Annotate | Download (4 kB)

1
"""update account in paths
2

3
Revision ID: 165ba3fbfe53
4
Revises: 3dd56e750a3
5
Create Date: 2012-12-04 19:08:23.933634
6

7
"""
8

    
9
# revision identifiers, used by Alembic.
10
revision = '165ba3fbfe53'
11
down_revision = '3dd56e750a3'
12

    
13
from alembic import op
14
from sqlalchemy.sql import table, column
15

    
16
from synnefo.lib.astakos import get_user_uuid, get_username as get_user_username
17
from pithos.api.settings import SERVICE_TOKEN, USER_INFO_URL
18

    
19
import sqlalchemy as sa
20

    
21
catalog = {}
22
def get_uuid(account):
23
    global catalog
24
    uuid = catalog.get(account)
25
    if uuid:
26
        return uuid
27
    try:
28
        uuid = get_user_uuid(SERVICE_TOKEN, account, USER_INFO_URL)
29
    except Exception, e:
30
        print 'Unable to retrieve uuid for %s: %s' % (account, e)
31
        return
32
    else:
33
        if uuid:
34
            catalog[account] = uuid
35
        return uuid
36
    
37
inverse_catalog = {}
38
def get_username(account):
39
    global inverse_catalog
40
    username = inverse_catalog.get(account)
41
    if username:
42
        return username
43
    try:
44
        username = get_user_username(SERVICE_TOKEN, account, USER_INFO_URL)
45
    except Exception, e:
46
        print 'Unable to retrieve username for %s: %s' % (account, e)
47
        return
48
    else:
49
        if username:
50
            catalog[account] = username
51
        return username
52

    
53
n = table(
54
    'nodes',
55
    column('node', sa.Integer),
56
    column('path', sa.String(2048))
57
)
58

    
59
p = table(
60
    'public',
61
    column('public_id', sa.Integer),
62
    column('path', sa.String(2048))
63
)
64

    
65
x = table(
66
    'xfeatures',
67
    column('feature_id', sa.Integer),
68
    column('path', sa.String(2048))
69
)
70

    
71
def upgrade():
72
    connection = op.get_bind()
73
  
74
    s = sa.select([n.c.node, n.c.path])
75
    nodes = connection.execute(s).fetchall()
76
    for node, path in nodes:
77
        account, sep, rest = path.partition('/')
78
        uuid = get_uuid(account)
79
        if not uuid:
80
            continue
81
        path = sep.join([uuid, rest])
82
        u = n.update().where(n.c.node == node).values({'path':path})
83
        connection.execute(u)
84
    
85
    s = sa.select([p.c.public_id, p.c.path])
86
    public = connection.execute(s).fetchall()
87
    for id, path in public:
88
        account, sep, rest = path.partition('/')
89
        uuid = get_uuid(account)
90
        if not uuid:
91
            continue
92
        path = sep.join([uuid, rest])
93
        u = p.update().where(p.c.public_id == id).values({'path':path})
94
        connection.execute(u)
95
    
96
    s = sa.select([x.c.feature_id, x.c.path])
97
    xfeatures = connection.execute(s).fetchall()
98
    for id, path in xfeatures:
99
        account, sep, rest = path.partition('/')
100
        uuid = get_uuid(account)
101
        if not uuid:
102
            continue
103
        path = sep.join([uuid, rest])
104
        u = x.update().where(x.c.feature_id == id).values({'path':path})
105
        connection.execute(u)
106

    
107

    
108
def downgrade():
109
    connection = op.get_bind()
110
  
111
    s = sa.select([n.c.node, n.c.path])
112
    nodes = connection.execute(s).fetchall()
113
    for node, path in nodes:
114
        account, sep, rest = path.partition('/')
115
        username = get_username(account)
116
        if not username:
117
            continue
118
        path = sep.join([username, rest])
119
        u = n.update().where(n.c.node == node).values({'path':path})
120
        connection.execute(u)
121
    
122
    s = sa.select([p.c.public_id, p.c.path])
123
    public = connection.execute(s).fetchall()
124
    for id, path in public:
125
        account, sep, rest = path.partition('/')
126
        username = get_username(account)
127
        if not username:
128
            continue
129
        path = sep.join([username, rest])
130
        u = p.update().where(p.c.public_id == id).values({'path':path})
131
        connection.execute(u)
132
    
133
    s = sa.select([x.c.feature_id, x.c.path])
134
    xfeatures = connection.execute(s).fetchall()
135
    for id, path in xfeatures:
136
        account, sep, rest = path.partition('/')
137
        username = get_username(account)
138
        if not username:
139
            continue
140
        path = sep.join([username, rest])
141
        u = x.update().where(x.c.feature_id == id).values({'path':path})
142
        connection.execute(u)