Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.9 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, literal, and_
15

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

    
20
import sqlalchemy as sa
21

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

    
56
n = table(
57
    'nodes',
58
    column('node', sa.Integer),
59
    column('path', sa.String(2048))
60
)
61

    
62
p = table(
63
    'public',
64
    column('public_id', sa.Integer),
65
    column('path', sa.String(2048))
66
)
67

    
68
x = table(
69
    'xfeatures',
70
    column('feature_id', sa.Integer),
71
    column('path', sa.String(2048))
72
)
73

    
74
xvals =  table(
75
    'xfeaturevals',
76
    column('feature_id', sa.Integer),
77
    column('key', sa.Integer),
78
    column('value', sa.String(256))
79
)
80

    
81
g =  table(
82
    'groups',
83
    column('owner', sa.String(256)),
84
    column('name', sa.String(256)),
85
    column('member', sa.String(256))
86
)
87

    
88
def upgrade():
89
    connection = op.get_bind()
90
  
91
    s = sa.select([n.c.node, n.c.path])
92
    nodes = connection.execute(s).fetchall()
93
    for node, path in nodes:
94
        account, sep, rest = path.partition('/')
95
        uuid = get_uuid(account)
96
        if not uuid:
97
            continue
98
        path = sep.join([uuid, rest])
99
        u = n.update().where(n.c.node == node).values({'path':path})
100
        connection.execute(u)
101
    
102
    s = sa.select([p.c.public_id, p.c.path])
103
    public = connection.execute(s).fetchall()
104
    for id, path in public:
105
        account, sep, rest = path.partition('/')
106
        uuid = get_uuid(account)
107
        if not uuid:
108
            continue
109
        path = sep.join([uuid, rest])
110
        u = p.update().where(p.c.public_id == id).values({'path':path})
111
        connection.execute(u)
112
    
113
    s = sa.select([x.c.feature_id, x.c.path])
114
    xfeatures = connection.execute(s).fetchall()
115
    for id, path in xfeatures:
116
        account, sep, rest = path.partition('/')
117
        uuid = get_uuid(account)
118
        if not uuid:
119
            continue
120
        path = sep.join([uuid, rest])
121
        u = x.update().where(x.c.feature_id == id).values({'path':path})
122
        connection.execute(u)
123

    
124
    s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value])
125
    s = s.where(xvals.c.value != '*')
126
    xfeaturevals = connection.execute(s).fetchall()
127
    for feature_id, key, value in xfeaturevals:
128
        account, sep, group = value.partition(':')
129
        uuid = get_uuid(account)
130
        if not uuid:
131
            continue
132
        new_value = sep.join([uuid, group])
133
        u = xvals.update()
134
        u = u.where(and_(
135
                xvals.c.feature_id == feature_id,
136
                xvals.c.key == key,
137
                xvals.c.value == value))
138
        u = u.values({'value':new_value})
139
        connection.execute(u)
140

    
141
    s = sa.select([g.c.owner, g.c.name, g.c.member])
142
    groups = connection.execute(s).fetchall()
143
    for owner, name, member in groups:
144
        owner_uuid = get_uuid(owner)
145
        member_uuid = get_uuid(member)
146
        if owner_uuid or member_uuid:
147
            u = g.update()
148
            u = u.where(and_(
149
                g.c.owner == owner,
150
                g.c.name == name,
151
                g.c.member == member))
152
            values = {}
153
            if owner_uuid:
154
                values['owner'] = owner_uuid
155
            if member_uuid:
156
                values['member'] = member_uuid
157
            u = u.values(values)
158
            connection.execute(u)
159

    
160
def downgrade():
161
    connection = op.get_bind()
162
  
163
    s = sa.select([n.c.node, n.c.path])
164
    nodes = connection.execute(s).fetchall()
165
    for node, path in nodes:
166
        account, sep, rest = path.partition('/')
167
        username = get_username(account)
168
        if not username:
169
            continue
170
        path = sep.join([username, rest])
171
        u = n.update().where(n.c.node == node).values({'path':path})
172
        connection.execute(u)
173
    
174
    s = sa.select([p.c.public_id, p.c.path])
175
    public = connection.execute(s).fetchall()
176
    for id, path in public:
177
        account, sep, rest = path.partition('/')
178
        username = get_username(account)
179
        if not username:
180
            continue
181
        path = sep.join([username, rest])
182
        u = p.update().where(p.c.public_id == id).values({'path':path})
183
        connection.execute(u)
184
    
185
    s = sa.select([x.c.feature_id, x.c.path])
186
    xfeatures = connection.execute(s).fetchall()
187
    for id, path in xfeatures:
188
        account, sep, rest = path.partition('/')
189
        username = get_username(account)
190
        if not username:
191
            continue
192
        path = sep.join([username, rest])
193
        u = x.update().where(x.c.feature_id == id).values({'path':path})
194
        connection.execute(u)
195

    
196
    s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value])
197
    s = s.where(xvals.c.value != '*')
198
    xfeaturevals = connection.execute(s).fetchall()
199
    for feature_id, key, value in xfeaturevals:
200
        account, sep, group = value.partition(':')
201
        username = get_username(account)
202
        if not username:
203
            continue
204
        new_value = sep.join([username, group])
205
        u = xvals.update()
206
        u = u.where(and_(
207
                xvals.c.feature_id == feature_id,
208
                xvals.c.key == key,
209
                xvals.c.value ==value))
210
        u = u.values({'value':new_value})
211
        connection.execute(u)
212

    
213
    s = sa.select([g.c.owner, g.c.name, g.c.member])
214
    groups = connection.execute(s).fetchall()
215
    for owner, name, member in groups:
216
        owner_username = get_username(owner)
217
        member_username = get_username(member)
218
        if owner_username or member_username:
219
            u = g.update()
220
            u = u.where(and_(
221
                g.c.owner == owner,
222
                g.c.name == name,
223
                g.c.member == member))
224
            values = {}
225
            if owner_username:
226
                values['owner'] = owner_username
227
            if member_username:
228
                values['member'] = member_username
229
            u = u.values(values)
230
            connection.execute(u)