root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / versions / 165ba3fbfe53_update_path_account.py @ 72e191e5
History | View | Annotate | Download (6.8 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, service_token, astakos_client): |
40 |
global catalog
|
41 |
if account in catalog: |
42 |
return catalog[account]
|
43 |
try:
|
44 |
catalog[account] = astakos_client.service_get_uuid(service_token, |
45 |
account) |
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 |
|
56 |
|
57 |
def _get_displayname(account, service_token, astakos_client): |
58 |
global inverse_catalog
|
59 |
if account in inverse_catalog: |
60 |
return inverse_catalog[account]
|
61 |
try:
|
62 |
inverse_catalog[account] = astakos_client.service_get_username( |
63 |
service_token, account) |
64 |
print '\n', account, '-->', inverse_catalog[account] |
65 |
except NoUserName:
|
66 |
return None |
67 |
except:
|
68 |
raise
|
69 |
else:
|
70 |
return inverse_catalog[account]
|
71 |
|
72 |
n = table( |
73 |
'nodes',
|
74 |
column('node', sa.Integer),
|
75 |
column('path', sa.String(2048)) |
76 |
) |
77 |
|
78 |
v = table( |
79 |
'versions',
|
80 |
column('node', sa.Integer),
|
81 |
column('muser', sa.String(2048)) |
82 |
) |
83 |
|
84 |
p = table( |
85 |
'public',
|
86 |
column('public_id', sa.Integer),
|
87 |
column('path', sa.String(2048)) |
88 |
) |
89 |
|
90 |
x = table( |
91 |
'xfeatures',
|
92 |
column('feature_id', sa.Integer),
|
93 |
column('path', sa.String(2048)) |
94 |
) |
95 |
|
96 |
xvals = table( |
97 |
'xfeaturevals',
|
98 |
column('feature_id', sa.Integer),
|
99 |
column('key', sa.Integer),
|
100 |
column('value', sa.String(256)) |
101 |
) |
102 |
|
103 |
g = table( |
104 |
'groups',
|
105 |
column('owner', sa.String(256)), |
106 |
column('name', sa.String(256)), |
107 |
column('member', sa.String(256)) |
108 |
) |
109 |
|
110 |
|
111 |
def migrate(callback): |
112 |
connection = op.get_bind() |
113 |
|
114 |
s = sa.select([n.c.node, n.c.path]) |
115 |
nodes = connection.execute(s).fetchall() |
116 |
bar = IncrementalBar('Migrating node paths...', max=len(nodes)) |
117 |
for node, path in nodes: |
118 |
account, sep, rest = path.partition('/')
|
119 |
match = callback(account) |
120 |
if not match: |
121 |
bar.next() |
122 |
continue
|
123 |
path = sep.join([match, rest]) |
124 |
u = n.update().where(n.c.node == node).values({'path': path})
|
125 |
connection.execute(u) |
126 |
bar.next() |
127 |
bar.finish() |
128 |
|
129 |
s = sa.select([v.c.muser]).distinct() |
130 |
musers = connection.execute(s).fetchall() |
131 |
bar = IncrementalBar('Migrating version modification users...',
|
132 |
max=len(musers))
|
133 |
for muser, in musers: |
134 |
match = callback(muser) |
135 |
if not match: |
136 |
bar.next() |
137 |
continue
|
138 |
u = v.update().where(v.c.muser == muser).values({'muser': match})
|
139 |
connection.execute(u) |
140 |
bar.next() |
141 |
bar.finish() |
142 |
|
143 |
s = sa.select([p.c.public_id, p.c.path]) |
144 |
public = connection.execute(s).fetchall() |
145 |
bar = IncrementalBar('Migrating public paths...', max=len(public)) |
146 |
for id, path in public: |
147 |
account, sep, rest = path.partition('/')
|
148 |
match = callback(account) |
149 |
if not match: |
150 |
bar.next() |
151 |
continue
|
152 |
path = sep.join([match, rest]) |
153 |
u = p.update().where(p.c.public_id == id).values({'path': path}) |
154 |
connection.execute(u) |
155 |
bar.next() |
156 |
bar.finish() |
157 |
|
158 |
s = sa.select([x.c.feature_id, x.c.path]) |
159 |
xfeatures = connection.execute(s).fetchall() |
160 |
bar = IncrementalBar('Migrating permission paths...', max=len(xfeatures)) |
161 |
for id, path in xfeatures: |
162 |
account, sep, rest = path.partition('/')
|
163 |
match = callback(account) |
164 |
if not match: |
165 |
bar.next() |
166 |
continue
|
167 |
path = sep.join([match, rest]) |
168 |
u = x.update().where(x.c.feature_id == id).values({'path': path}) |
169 |
connection.execute(u) |
170 |
bar.next() |
171 |
bar.finish() |
172 |
|
173 |
s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value]) |
174 |
s = s.where(xvals.c.value != '*')
|
175 |
xfeaturevals = connection.execute(s).fetchall() |
176 |
bar = IncrementalBar('Migrating permission holders...',
|
177 |
max=len(xfeaturevals))
|
178 |
for feature_id, key, value in xfeaturevals: |
179 |
account, sep, group = value.partition(':')
|
180 |
match = callback(account) |
181 |
if not match: |
182 |
bar.next() |
183 |
continue
|
184 |
new_value = sep.join([match, group]) |
185 |
u = xvals.update() |
186 |
u = u.where(and_(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 |
|
218 |
def upgrade(): |
219 |
try:
|
220 |
from pithos.api import settings |
221 |
except ImportError: |
222 |
return
|
223 |
else:
|
224 |
astakos_client = AstakosClient(settings.ASTAKOS_BASE_URL, |
225 |
retry=3,
|
226 |
use_pool=True)
|
227 |
get_uuid = functools.partial(_get_uuid, |
228 |
service_token=settings.SERVICE_TOKEN, |
229 |
astakos_client=astakos_client) |
230 |
migrate(get_uuid) |
231 |
|
232 |
|
233 |
def downgrade(): |
234 |
try:
|
235 |
from pithos.api import settings |
236 |
except ImportError: |
237 |
return
|
238 |
else:
|
239 |
astakos_client = AstakosClient(settings.ASTAKOS_BASE_URL, |
240 |
retry=3,
|
241 |
use_pool=True)
|
242 |
get_displayname = functools.partial( |
243 |
_get_displayname, |
244 |
service_token=settings.SERVICE_TOKEN, |
245 |
astakos_client=astakos_client) |
246 |
migrate(get_displayname) |