Revision e4d84803

b/snf-pithos-backend/pithos/backends/lib/sqlalchemy/alembic/versions/165ba3fbfe53_update_path_account.py
17 17
from pithos.api.settings import (
18 18
    SERVICE_TOKEN, USER_CATALOG_URL, AUTHENTICATION_USERS)
19 19

  
20
try:
21
    from progress.bar import IncrementalBar
22
except ImportError:
23
    class IncrementalBar():
24
        def __init__(self, label, max=100):
25
            print label
26

  
27
        def next(self):
28
            return
29

  
30
        def finish(self):
31
            return
32

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

  
22 35
import sqlalchemy as sa
......
24 37
catalog = {}
25 38
def get_uuid(account):
26 39
    global catalog
27
    uuid = catalog.get(account, -1)
28
    if uuid != -1:
29
        return uuid
40
    if account in catalog:
41
        return catalog[account]
30 42
    try:
31 43
        catalog[account] = get_user_uuid(
32 44
            SERVICE_TOKEN, account, USER_CATALOG_URL, AUTHENTICATION_USERS)
33
        print account, catalog[account]
45
        print '\n', account, '-->', catalog[account]
34 46
    except:
35 47
        raise
36 48
    else:
......
39 51
inverse_catalog = {}
40 52
def get_displayname(account):
41 53
    global inverse_catalog
42
    displayname = inverse_catalog.get(account, -1)
43
    if displayname != -1:
44
        return displayname
54
    if account in inverse_catalog:
55
        return inverse_catalog[account]
45 56
    try:
46 57
        inverse_catalog[account] = get_user_displayname(
47 58
            SERVICE_TOKEN, account, USER_CATALOG_URL, AUTHENTICATION_USERS)
48
        print account, inverse_catalog[account]
59
        print '\n', account, '-->', inverse_catalog[account]
49 60
    except:
50 61
        raise
51 62
    else:
......
89 100
    column('member', sa.String(256))
90 101
)
91 102

  
92
def upgrade():
103
def migrate(callback):
93 104
    connection = op.get_bind()
94 105

  
95 106
    s = sa.select([n.c.node, n.c.path])
96 107
    nodes = connection.execute(s).fetchall()
108
    bar = IncrementalBar('Migrating node paths...', max=len(nodes))
97 109
    for node, path in nodes:
98 110
        account, sep, rest = path.partition('/')
99
        uuid = get_uuid(account)
100
        if not uuid:
111
        match = callback(account)
112
        if not match:
113
            bar.next()
101 114
            continue
102
        path = sep.join([uuid, rest])
115
        path = sep.join([match, rest])
103 116
        u = n.update().where(n.c.node == node).values({'path':path})
104 117
        connection.execute(u)
118
        bar.next()
119
    bar.finish()
105 120

  
106 121
    s = sa.select([v.c.node, v.c.muser])
107 122
    versions = connection.execute(s).fetchall()
123
    bar = IncrementalBar('Migrating version modification users...',
124
                         max=len(versions)
125
    )
108 126
    for node, muser in versions:
109
        uuid = get_uuid(muser)
110
        if not uuid:
127
        match = callback(muser)
128
        if not match:
129
            bar.next()
111 130
            continue
112
        u = v.update().where(v.c.node == node).values({'muser':uuid})
131
        u = v.update().where(v.c.node == node).values({'muser':match})
113 132
        connection.execute(u)
133
        bar.next()
134
    bar.finish()
114 135

  
115 136
    s = sa.select([p.c.public_id, p.c.path])
116 137
    public = connection.execute(s).fetchall()
138
    bar = IncrementalBar('Migrating public paths...', max=len(public))
117 139
    for id, path in public:
118 140
        account, sep, rest = path.partition('/')
119
        uuid = get_uuid(account)
120
        if not uuid:
141
        match = callback(account)
142
        if not match:
143
            bar.next()
121 144
            continue
122
        path = sep.join([uuid, rest])
145
        path = sep.join([match, rest])
123 146
        u = p.update().where(p.c.public_id == id).values({'path':path})
124 147
        connection.execute(u)
148
        bar.next()
149
    bar.finish()
125 150

  
126 151
    s = sa.select([x.c.feature_id, x.c.path])
127 152
    xfeatures = connection.execute(s).fetchall()
153
    bar = IncrementalBar('Migrating permission paths...', max=len(xfeatures))
128 154
    for id, path in xfeatures:
129 155
        account, sep, rest = path.partition('/')
130
        uuid = get_uuid(account)
131
        if not uuid:
156
        match = callback(account)
157
        if not match:
158
            bar.next()
132 159
            continue
133
        path = sep.join([uuid, rest])
160
        path = sep.join([match, rest])
134 161
        u = x.update().where(x.c.feature_id == id).values({'path':path})
135 162
        connection.execute(u)
163
        bar.next()
164
    bar.finish()
136 165

  
137 166
    s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value])
138 167
    s = s.where(xvals.c.value != '*')
139 168
    xfeaturevals = connection.execute(s).fetchall()
169
    bar = IncrementalBar('Migrating permission holders...',
170
                         max=len(xfeaturevals))
140 171
    for feature_id, key, value in xfeaturevals:
141 172
        account, sep, group = value.partition(':')
142
        uuid = get_uuid(account)
143
        if not uuid:
173
        match = callback(account)
174
        if not match:
175
            bar.next()
144 176
            continue
145
        new_value = sep.join([uuid, group])
177
        new_value = sep.join([match, group])
146 178
        u = xvals.update()
147 179
        u = u.where(and_(
148 180
                xvals.c.feature_id == feature_id,
......
150 182
                xvals.c.value == value))
151 183
        u = u.values({'value':new_value})
152 184
        connection.execute(u)
185
        bar.next()
186
    bar.finish()
153 187

  
154 188
    s = sa.select([g.c.owner, g.c.name, g.c.member])
155 189
    groups = connection.execute(s).fetchall()
190
    bar = IncrementalBar('Migrating group owners & members...',
191
                         max=len(groups))
156 192
    for owner, name, member in groups:
157
        owner_uuid = get_uuid(owner)
158
        member_uuid = get_uuid(member)
159
        if owner_uuid or member_uuid:
193
        owner_match = callback(owner)
194
        member_match = callback(member)
195
        if owner_match or member_match:
160 196
            u = g.update()
161 197
            u = u.where(and_(
162 198
                g.c.owner == owner,
163 199
                g.c.name == name,
164 200
                g.c.member == member))
165 201
            values = {}
166
            if owner_uuid:
167
                values['owner'] = owner_uuid
168
            if member_uuid:
169
                values['member'] = member_uuid
202
            if owner_match:
203
                values['owner'] = owner_match
204
            if member_match:
205
                values['member'] = member_match
170 206
            u = u.values(values)
171 207
            connection.execute(u)
208
            bar.next()
209
    bar.finish()
172 210

  
173
def downgrade():
174
    connection = op.get_bind()
175

  
176
    s = sa.select([n.c.node, n.c.path])
177
    nodes = connection.execute(s).fetchall()
178
    for node, path in nodes:
179
        account, sep, rest = path.partition('/')
180
        displayname = get_displayname(account)
181
        if not displayname:
182
            continue
183
        path = sep.join([displayname, rest])
184
        u = n.update().where(n.c.node == node).values({'path':path})
185
        connection.execute(u)
186

  
187
    s = sa.select([v.c.node, v.c.muser])
188
    versions = connection.execute(s).fetchall()
189
    for node, muser in versions:
190
        displayname = get_displayname(muser)
191
        if not displayname:
192
            continue
193
        u = v.update().where(v.c.node == node).values({'muser':displayname})
194
        connection.execute(u)
195

  
196
    s = sa.select([p.c.public_id, p.c.path])
197
    public = connection.execute(s).fetchall()
198
    for id, path in public:
199
        account, sep, rest = path.partition('/')
200
        displayname = get_displayname(account)
201
        if not displayname:
202
            continue
203
        path = sep.join([displayname, rest])
204
        u = p.update().where(p.c.public_id == id).values({'path':path})
205
        connection.execute(u)
206

  
207
    s = sa.select([x.c.feature_id, x.c.path])
208
    xfeatures = connection.execute(s).fetchall()
209
    for id, path in xfeatures:
210
        account, sep, rest = path.partition('/')
211
        displayname = get_displayname(account)
212
        if not displayname:
213
            continue
214
        path = sep.join([displayname, rest])
215
        u = x.update().where(x.c.feature_id == id).values({'path':path})
216
        connection.execute(u)
217

  
218
    s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value])
219
    s = s.where(xvals.c.value != '*')
220
    xfeaturevals = connection.execute(s).fetchall()
221
    for feature_id, key, value in xfeaturevals:
222
        account, sep, group = value.partition(':')
223
        displayname = get_displayname(account)
224
        if not displayname:
225
            continue
226
        new_value = sep.join([displayname, group])
227
        u = xvals.update()
228
        u = u.where(and_(
229
                xvals.c.feature_id == feature_id,
230
                xvals.c.key == key,
231
                xvals.c.value ==value))
232
        u = u.values({'value':new_value})
233
        connection.execute(u)
211
def upgrade():
212
    migrate(get_uuid)
234 213

  
235
    s = sa.select([g.c.owner, g.c.name, g.c.member])
236
    groups = connection.execute(s).fetchall()
237
    for owner, name, member in groups:
238
        owner_displayname = get_displayname(owner)
239
        member_displayname = get_displayname(member)
240
        if owner_displayname or member_displayname:
241
            u = g.update()
242
            u = u.where(and_(
243
                g.c.owner == owner,
244
                g.c.name == name,
245
                g.c.member == member))
246
            values = {}
247
            if owner_displayname:
248
                values['owner'] = owner_displayname
249
            if member_displayname:
250
                values['member'] = member_displayname
251
            u = u.values(values)
252
            connection.execute(u)
214
def downgrade():
215
    migrate(get_displayname)

Also available in: Unified diff