root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / versions / 165ba3fbfe53_update_path_account.py @ 860c5d9c
History | View | Annotate | Download (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, literal, and_ |
15 |
|
16 |
from pithos.api.settings import (SERVICE_TOKEN, ASTAKOS_URL) |
17 |
|
18 |
from astakosclient import AstakosClient |
19 |
from astakosclient.errors import NoUserName, NoUUID |
20 |
astakos_client = AstakosClient(ASTAKOS_URL, retry=3, use_pool=True) |
21 |
|
22 |
try:
|
23 |
from progress.bar import IncrementalBar |
24 |
except ImportError: |
25 |
class IncrementalBar(): |
26 |
def __init__(self, label, max=100): |
27 |
print label
|
28 |
|
29 |
def next(self): |
30 |
return
|
31 |
|
32 |
def finish(self): |
33 |
return
|
34 |
|
35 |
import sqlalchemy as sa |
36 |
|
37 |
catalog = {} |
38 |
def get_uuid(account): |
39 |
global catalog
|
40 |
if account in catalog: |
41 |
return catalog[account]
|
42 |
try:
|
43 |
catalog[account] = astakos_client.service_get_uuid( |
44 |
SERVICE_TOKEN, account |
45 |
) |
46 |
print '\n', account, '-->', catalog[account] |
47 |
except NoUUID:
|
48 |
return None |
49 |
except:
|
50 |
raise
|
51 |
else:
|
52 |
return catalog[account]
|
53 |
|
54 |
inverse_catalog = {} |
55 |
def get_displayname(account): |
56 |
global inverse_catalog
|
57 |
if account in inverse_catalog: |
58 |
return inverse_catalog[account]
|
59 |
try:
|
60 |
inverse_catalog[account] = astakos_client.service_get_username( |
61 |
SERVICE_TOKEN, account |
62 |
) |
63 |
print '\n', account, '-->', inverse_catalog[account] |
64 |
except NoUserName:
|
65 |
return None |
66 |
except:
|
67 |
raise
|
68 |
else:
|
69 |
return inverse_catalog[account]
|
70 |
|
71 |
n = table( |
72 |
'nodes',
|
73 |
column('node', sa.Integer),
|
74 |
column('path', sa.String(2048)) |
75 |
) |
76 |
|
77 |
v = table( |
78 |
'versions',
|
79 |
column('node', sa.Integer),
|
80 |
column('muser', sa.String(2048)) |
81 |
) |
82 |
|
83 |
p = table( |
84 |
'public',
|
85 |
column('public_id', sa.Integer),
|
86 |
column('path', sa.String(2048)) |
87 |
) |
88 |
|
89 |
x = table( |
90 |
'xfeatures',
|
91 |
column('feature_id', sa.Integer),
|
92 |
column('path', sa.String(2048)) |
93 |
) |
94 |
|
95 |
xvals = table( |
96 |
'xfeaturevals',
|
97 |
column('feature_id', sa.Integer),
|
98 |
column('key', sa.Integer),
|
99 |
column('value', sa.String(256)) |
100 |
) |
101 |
|
102 |
g = table( |
103 |
'groups',
|
104 |
column('owner', sa.String(256)), |
105 |
column('name', sa.String(256)), |
106 |
column('member', sa.String(256)) |
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 |
) |
132 |
for muser, in musers: |
133 |
match = callback(muser) |
134 |
if not match: |
135 |
bar.next() |
136 |
continue
|
137 |
u = v.update().where(v.c.muser == muser).values({'muser': match})
|
138 |
connection.execute(u) |
139 |
bar.next() |
140 |
bar.finish() |
141 |
|
142 |
s = sa.select([p.c.public_id, p.c.path]) |
143 |
public = connection.execute(s).fetchall() |
144 |
bar = IncrementalBar('Migrating public paths...', max=len(public)) |
145 |
for id, path in public: |
146 |
account, sep, rest = path.partition('/')
|
147 |
match = callback(account) |
148 |
if not match: |
149 |
bar.next() |
150 |
continue
|
151 |
path = sep.join([match, rest]) |
152 |
u = p.update().where(p.c.public_id == id).values({'path':path}) |
153 |
connection.execute(u) |
154 |
bar.next() |
155 |
bar.finish() |
156 |
|
157 |
s = sa.select([x.c.feature_id, x.c.path]) |
158 |
xfeatures = connection.execute(s).fetchall() |
159 |
bar = IncrementalBar('Migrating permission paths...', max=len(xfeatures)) |
160 |
for id, path in xfeatures: |
161 |
account, sep, rest = path.partition('/')
|
162 |
match = callback(account) |
163 |
if not match: |
164 |
bar.next() |
165 |
continue
|
166 |
path = sep.join([match, rest]) |
167 |
u = x.update().where(x.c.feature_id == id).values({'path':path}) |
168 |
connection.execute(u) |
169 |
bar.next() |
170 |
bar.finish() |
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 |
bar = IncrementalBar('Migrating permission holders...',
|
176 |
max=len(xfeaturevals))
|
177 |
for feature_id, key, value in xfeaturevals: |
178 |
account, sep, group = value.partition(':')
|
179 |
match = callback(account) |
180 |
if not match: |
181 |
bar.next() |
182 |
continue
|
183 |
new_value = sep.join([match, group]) |
184 |
u = xvals.update() |
185 |
u = u.where(and_( |
186 |
xvals.c.feature_id == feature_id, |
187 |
xvals.c.key == key, |
188 |
xvals.c.value == value)) |
189 |
u = u.values({'value':new_value})
|
190 |
connection.execute(u) |
191 |
bar.next() |
192 |
bar.finish() |
193 |
|
194 |
s = sa.select([g.c.owner, g.c.name, g.c.member]) |
195 |
groups = connection.execute(s).fetchall() |
196 |
bar = IncrementalBar('Migrating group owners & members...',
|
197 |
max=len(groups))
|
198 |
for owner, name, member in groups: |
199 |
owner_match = callback(owner) |
200 |
member_match = callback(member) |
201 |
if owner_match or member_match: |
202 |
u = g.update() |
203 |
u = u.where(and_( |
204 |
g.c.owner == owner, |
205 |
g.c.name == name, |
206 |
g.c.member == member)) |
207 |
values = {} |
208 |
if owner_match:
|
209 |
values['owner'] = owner_match
|
210 |
if member_match:
|
211 |
values['member'] = member_match
|
212 |
u = u.values(values) |
213 |
connection.execute(u) |
214 |
bar.next() |
215 |
bar.finish() |
216 |
|
217 |
def upgrade(): |
218 |
migrate(get_uuid) |
219 |
|
220 |
def downgrade(): |
221 |
migrate(get_displayname) |