Revision 50149cc8 snf-pithos-backend/pithos/backends/lib/sqlalchemy/alembic/versions/165ba3fbfe53_update_path_account.py

b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/alembic/versions/165ba3fbfe53_update_path_account.py
11 11
down_revision = '3dd56e750a3'
12 12

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

  
16 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
17
from pithos.api.settings import (
18
    SERVICE_TOKEN, USER_INFO_URL, AUTHENTICATION_USERS)
18 19

  
19 20
import sqlalchemy as sa
20 21

  
......
25 26
    if uuid:
26 27
        return uuid
27 28
    try:
28
        uuid = get_user_uuid(SERVICE_TOKEN, account, USER_INFO_URL)
29
        uuid = get_user_uuid(
30
            SERVICE_TOKEN, account, USER_INFO_URL, AUTHENTICATION_USERS)
29 31
    except Exception, e:
30 32
        print 'Unable to retrieve uuid for %s: %s' % (account, e)
31 33
        return
......
41 43
    if username:
42 44
        return username
43 45
    try:
44
        username = get_user_username(SERVICE_TOKEN, account, USER_INFO_URL)
46
        username = get_user_username(
47
            SERVICE_TOKEN, account, USER_INFO_URL, AUTHENTICATION_USERS)
45 48
    except Exception, e:
46 49
        print 'Unable to retrieve username for %s: %s' % (account, e)
47 50
        return
......
68 71
    column('path', sa.String(2048))
69 72
)
70 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

  
71 82
def upgrade():
72 83
    connection = op.get_bind()
73 84
  
......
104 115
        u = x.update().where(x.c.feature_id == id).values({'path':path})
105 116
        connection.execute(u)
106 117

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

  
107 135

  
108 136
def downgrade():
109 137
    connection = op.get_bind()
......
140 168
        path = sep.join([username, rest])
141 169
        u = x.update().where(x.c.feature_id == id).values({'path':path})
142 170
        connection.execute(u)
171

  
172
    s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value])
173
    s = s.where(xvals.c.value != '*')
174
    xfeaturevals = connection.execute(s).fetchall()
175
    for feature_id, key, value in xfeaturevals:
176
        account, sep, group = value.partition(':')
177
        username = get_username(account)
178
        if not username:
179
            continue
180
        new_value = sep.join([username, group])
181
        u = xvals.update()
182
        u = u.where(and_(
183
                xvals.c.feature_id == feature_id,
184
                xvals.c.key == key,
185
                xvals.c.value ==value))
186
        u = u.values({'value':new_value})
187
        connection.execute(u)

Also available in: Unified diff