Statistics
| Branch: | Tag: | Revision:

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

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

    
16
from astakosclient import AstakosClient
17
from astakosclient.errors import NoUserName, NoUUID
18

    
19
import functools
20

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

    
28
        def next(self):
29
            return
30

    
31
        def finish(self):
32
            return
33

    
34
import sqlalchemy as sa
35

    
36
catalog = {}
37

    
38

    
39
def _get_uuid(account, astakos_client):
40
    global catalog
41
    if account in catalog:
42
        return catalog[account]
43
    try:
44
        catalog[account] = astakos_client.service_get_uuid(account)
45
        print '\n', account, '-->', catalog[account]
46
    except NoUUID:
47
        return None
48
    except:
49
        raise
50
    else:
51
        return catalog[account]
52

    
53
inverse_catalog = {}
54

    
55

    
56
def _get_displayname(account, astakos_client):
57
    global inverse_catalog
58
    if account in inverse_catalog:
59
        return inverse_catalog[account]
60
    try:
61
        inverse_catalog[account] = astakos_client.service_get_username(account)
62
        print '\n', account, '-->', inverse_catalog[account]
63
    except NoUserName:
64
        return None
65
    except:
66
        raise
67
    else:
68
        return inverse_catalog[account]
69

    
70
n = table(
71
    'nodes',
72
    column('node', sa.Integer),
73
    column('path', sa.String(2048))
74
)
75

    
76
v = table(
77
    'versions',
78
    column('node', sa.Integer),
79
    column('muser', sa.String(2048))
80
)
81

    
82
p = table(
83
    'public',
84
    column('public_id', sa.Integer),
85
    column('path', sa.String(2048))
86
)
87

    
88
x = table(
89
    'xfeatures',
90
    column('feature_id', sa.Integer),
91
    column('path', sa.String(2048))
92
)
93

    
94
xvals = table(
95
    'xfeaturevals',
96
    column('feature_id', sa.Integer),
97
    column('key', sa.Integer),
98
    column('value', sa.String(256))
99
)
100

    
101
g = table(
102
    'groups',
103
    column('owner', sa.String(256)),
104
    column('name', sa.String(256)),
105
    column('member', sa.String(256))
106
)
107

    
108

    
109
def migrate(callback):
110
    connection = op.get_bind()
111

    
112
    s = sa.select([n.c.node, n.c.path])
113
    nodes = connection.execute(s).fetchall()
114
    bar = IncrementalBar('Migrating node paths...', max=len(nodes))
115
    for node, path in nodes:
116
        account, sep, rest = path.partition('/')
117
        match = callback(account)
118
        if not match:
119
            bar.next()
120
            continue
121
        path = sep.join([match, rest])
122
        u = n.update().where(n.c.node == node).values({'path': path})
123
        connection.execute(u)
124
        bar.next()
125
    bar.finish()
126

    
127
    s = sa.select([v.c.muser]).distinct()
128
    musers = connection.execute(s).fetchall()
129
    bar = IncrementalBar('Migrating version modification users...',
130
                         max=len(musers))
131
    for muser, in musers:
132
        match = callback(muser)
133
        if not match:
134
            bar.next()
135
            continue
136
        u = v.update().where(v.c.muser == muser).values({'muser': match})
137
        connection.execute(u)
138
        bar.next()
139
    bar.finish()
140

    
141
    s = sa.select([p.c.public_id, p.c.path])
142
    public = connection.execute(s).fetchall()
143
    bar = IncrementalBar('Migrating public paths...', max=len(public))
144
    for id, path in public:
145
        account, sep, rest = path.partition('/')
146
        match = callback(account)
147
        if not match:
148
            bar.next()
149
            continue
150
        path = sep.join([match, rest])
151
        u = p.update().where(p.c.public_id == id).values({'path': path})
152
        connection.execute(u)
153
        bar.next()
154
    bar.finish()
155

    
156
    s = sa.select([x.c.feature_id, x.c.path])
157
    xfeatures = connection.execute(s).fetchall()
158
    bar = IncrementalBar('Migrating permission paths...', max=len(xfeatures))
159
    for id, path in xfeatures:
160
        account, sep, rest = path.partition('/')
161
        match = callback(account)
162
        if not match:
163
            bar.next()
164
            continue
165
        path = sep.join([match, rest])
166
        u = x.update().where(x.c.feature_id == id).values({'path': path})
167
        connection.execute(u)
168
        bar.next()
169
    bar.finish()
170

    
171
    s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value])
172
    s = s.where(xvals.c.value != '*')
173
    xfeaturevals = connection.execute(s).fetchall()
174
    bar = IncrementalBar('Migrating permission holders...',
175
                         max=len(xfeaturevals))
176
    for feature_id, key, value in xfeaturevals:
177
        account, sep, group = value.partition(':')
178
        match = callback(account)
179
        if not match:
180
            bar.next()
181
            continue
182
        new_value = sep.join([match, group])
183
        u = xvals.update()
184
        u = u.where(and_(xvals.c.feature_id == feature_id,
185
                         xvals.c.key == key,
186
                         xvals.c.value == value))
187
        u = u.values({'value': new_value})
188
        connection.execute(u)
189
        bar.next()
190
    bar.finish()
191

    
192
    s = sa.select([g.c.owner, g.c.name, g.c.member])
193
    groups = connection.execute(s).fetchall()
194
    bar = IncrementalBar('Migrating group owners & members...',
195
                         max=len(groups))
196
    for owner, name, member in groups:
197
        owner_match = callback(owner)
198
        member_match = callback(member)
199
        if owner_match or member_match:
200
            u = g.update()
201
            u = u.where(and_(
202
                g.c.owner == owner,
203
                g.c.name == name,
204
                g.c.member == member))
205
            values = {}
206
            if owner_match:
207
                values['owner'] = owner_match
208
            if member_match:
209
                values['member'] = member_match
210
            u = u.values(values)
211
            connection.execute(u)
212
            bar.next()
213
    bar.finish()
214

    
215

    
216
def upgrade():
217
    try:
218
        from pithos.api import settings
219
    except ImportError:
220
        return
221
    else:
222
        astakos_client = AstakosClient(
223
            settings.SERVICE_TOKEN, settings.ASTAKOS_AUTH_URL,
224
            retry=3, use_pool=True)
225
        get_uuid = functools.partial(_get_uuid,
226
                                     service_token=settings.SERVICE_TOKEN,
227
                                     astakos_client=astakos_client)
228
        migrate(get_uuid)
229

    
230

    
231
def downgrade():
232
    try:
233
        from pithos.api import settings
234
    except ImportError:
235
        return
236
    else:
237
        astakos_client = AstakosClient(
238
            settings.SERVICE_TOKEN, settings.ASTAKOS_AUTH_URL,
239
            retry=3, use_pool=True)
240
        get_displayname = functools.partial(
241
            _get_displayname,
242
            astakos_client=astakos_client)
243
        migrate(get_displayname)