Statistics
| Branch: | Tag: | Revision:

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

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_displayname as get_user_displayname
17
from pithos.api.settings import (
18
    SERVICE_TOKEN, USER_CATALOG_URL, AUTHENTICATION_USERS)
19

    
20
USER_CATALOG_URL = USER_CATALOG_URL.replace('user_catalogs', 'service/api/user_catalogs')
21

    
22
import sqlalchemy as sa
23

    
24
catalog = {}
25
def get_uuid(account):
26
    global catalog
27
    uuid = catalog.get(account)
28
    if uuid:
29
        return uuid
30
    try:
31
        uuid = get_user_uuid(
32
            SERVICE_TOKEN, account, USER_CATALOG_URL, AUTHENTICATION_USERS)
33
    except:
34
        raise
35
    else:
36
        if uuid:
37
            catalog[account] = uuid
38
        return uuid
39

    
40
inverse_catalog = {}
41
def get_displayname(account):
42
    global inverse_catalog
43
    displayname = inverse_catalog.get(account)
44
    if displayname:
45
        return displayname
46
    try:
47
        displayname = get_user_displayname(
48
            SERVICE_TOKEN, account, USER_CATALOG_URL, AUTHENTICATION_USERS)
49
    except:
50
        raise
51
    else:
52
        if displayname:
53
            catalog[account] = displayname
54
        return displayname
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
        displayname = get_displayname(account)
168
        if not displayname:
169
            continue
170
        path = sep.join([displayname, 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
        displayname = get_displayname(account)
179
        if not displayname:
180
            continue
181
        path = sep.join([displayname, 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
        displayname = get_displayname(account)
190
        if not displayname:
191
            continue
192
        path = sep.join([displayname, 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
        displayname = get_displayname(account)
202
        if not displayname:
203
            continue
204
        new_value = sep.join([displayname, 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_displayname = get_displayname(owner)
217
        member_displayname = get_displayname(member)
218
        if owner_displayname or member_displayname:
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_displayname:
226
                values['owner'] = owner_displayname
227
            if member_displayname:
228
                values['member'] = member_displayname
229
            u = u.values(values)
230
            connection.execute(u)