root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / versions / 165ba3fbfe53_update_path_account.py @ 29148653
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, and_ |
15 |
|
16 |
from pithos.api.settings import (SERVICE_TOKEN, ASTAKOS_BASE_URL) |
17 |
|
18 |
from astakosclient import AstakosClient |
19 |
from astakosclient.errors import NoUserName, NoUUID |
20 |
astakos_client = AstakosClient(ASTAKOS_BASE_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 |
|
39 |
|
40 |
def get_uuid(account): |
41 |
global catalog
|
42 |
if account in catalog: |
43 |
return catalog[account]
|
44 |
try:
|
45 |
catalog[account] = astakos_client.service_get_uuid( |
46 |
SERVICE_TOKEN, account |
47 |
) |
48 |
print '\n', account, '-->', catalog[account] |
49 |
except NoUUID:
|
50 |
return None |
51 |
except:
|
52 |
raise
|
53 |
else:
|
54 |
return catalog[account]
|
55 |
|
56 |
inverse_catalog = {} |
57 |
|
58 |
|
59 |
def get_displayname(account): |
60 |
global inverse_catalog
|
61 |
if account in inverse_catalog: |
62 |
return inverse_catalog[account]
|
63 |
try:
|
64 |
inverse_catalog[account] = astakos_client.service_get_username( |
65 |
SERVICE_TOKEN, account |
66 |
) |
67 |
print '\n', account, '-->', inverse_catalog[account] |
68 |
except NoUserName:
|
69 |
return None |
70 |
except:
|
71 |
raise
|
72 |
else:
|
73 |
return inverse_catalog[account]
|
74 |
|
75 |
n = table( |
76 |
'nodes',
|
77 |
column('node', sa.Integer),
|
78 |
column('path', sa.String(2048)) |
79 |
) |
80 |
|
81 |
v = table( |
82 |
'versions',
|
83 |
column('node', sa.Integer),
|
84 |
column('muser', sa.String(2048)) |
85 |
) |
86 |
|
87 |
p = table( |
88 |
'public',
|
89 |
column('public_id', sa.Integer),
|
90 |
column('path', sa.String(2048)) |
91 |
) |
92 |
|
93 |
x = table( |
94 |
'xfeatures',
|
95 |
column('feature_id', sa.Integer),
|
96 |
column('path', sa.String(2048)) |
97 |
) |
98 |
|
99 |
xvals = table( |
100 |
'xfeaturevals',
|
101 |
column('feature_id', sa.Integer),
|
102 |
column('key', sa.Integer),
|
103 |
column('value', sa.String(256)) |
104 |
) |
105 |
|
106 |
g = table( |
107 |
'groups',
|
108 |
column('owner', sa.String(256)), |
109 |
column('name', sa.String(256)), |
110 |
column('member', sa.String(256)) |
111 |
) |
112 |
|
113 |
|
114 |
def migrate(callback): |
115 |
connection = op.get_bind() |
116 |
|
117 |
s = sa.select([n.c.node, n.c.path]) |
118 |
nodes = connection.execute(s).fetchall() |
119 |
bar = IncrementalBar('Migrating node paths...', max=len(nodes)) |
120 |
for node, path in nodes: |
121 |
account, sep, rest = path.partition('/')
|
122 |
match = callback(account) |
123 |
if not match: |
124 |
bar.next() |
125 |
continue
|
126 |
path = sep.join([match, rest]) |
127 |
u = n.update().where(n.c.node == node).values({'path': path})
|
128 |
connection.execute(u) |
129 |
bar.next() |
130 |
bar.finish() |
131 |
|
132 |
s = sa.select([v.c.muser]).distinct() |
133 |
musers = connection.execute(s).fetchall() |
134 |
bar = IncrementalBar('Migrating version modification users...',
|
135 |
max=len(musers))
|
136 |
for muser, in musers: |
137 |
match = callback(muser) |
138 |
if not match: |
139 |
bar.next() |
140 |
continue
|
141 |
u = v.update().where(v.c.muser == muser).values({'muser': match})
|
142 |
connection.execute(u) |
143 |
bar.next() |
144 |
bar.finish() |
145 |
|
146 |
s = sa.select([p.c.public_id, p.c.path]) |
147 |
public = connection.execute(s).fetchall() |
148 |
bar = IncrementalBar('Migrating public paths...', max=len(public)) |
149 |
for id, path in public: |
150 |
account, sep, rest = path.partition('/')
|
151 |
match = callback(account) |
152 |
if not match: |
153 |
bar.next() |
154 |
continue
|
155 |
path = sep.join([match, rest]) |
156 |
u = p.update().where(p.c.public_id == id).values({'path': path}) |
157 |
connection.execute(u) |
158 |
bar.next() |
159 |
bar.finish() |
160 |
|
161 |
s = sa.select([x.c.feature_id, x.c.path]) |
162 |
xfeatures = connection.execute(s).fetchall() |
163 |
bar = IncrementalBar('Migrating permission paths...', max=len(xfeatures)) |
164 |
for id, path in xfeatures: |
165 |
account, sep, rest = path.partition('/')
|
166 |
match = callback(account) |
167 |
if not match: |
168 |
bar.next() |
169 |
continue
|
170 |
path = sep.join([match, rest]) |
171 |
u = x.update().where(x.c.feature_id == id).values({'path': path}) |
172 |
connection.execute(u) |
173 |
bar.next() |
174 |
bar.finish() |
175 |
|
176 |
s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value]) |
177 |
s = s.where(xvals.c.value != '*')
|
178 |
xfeaturevals = connection.execute(s).fetchall() |
179 |
bar = IncrementalBar('Migrating permission holders...',
|
180 |
max=len(xfeaturevals))
|
181 |
for feature_id, key, value in xfeaturevals: |
182 |
account, sep, group = value.partition(':')
|
183 |
match = callback(account) |
184 |
if not match: |
185 |
bar.next() |
186 |
continue
|
187 |
new_value = sep.join([match, group]) |
188 |
u = xvals.update() |
189 |
u = u.where(and_(xvals.c.feature_id == feature_id, |
190 |
xvals.c.key == key, |
191 |
xvals.c.value == value)) |
192 |
u = u.values({'value': new_value})
|
193 |
connection.execute(u) |
194 |
bar.next() |
195 |
bar.finish() |
196 |
|
197 |
s = sa.select([g.c.owner, g.c.name, g.c.member]) |
198 |
groups = connection.execute(s).fetchall() |
199 |
bar = IncrementalBar('Migrating group owners & members...',
|
200 |
max=len(groups))
|
201 |
for owner, name, member in groups: |
202 |
owner_match = callback(owner) |
203 |
member_match = callback(member) |
204 |
if owner_match or member_match: |
205 |
u = g.update() |
206 |
u = u.where(and_( |
207 |
g.c.owner == owner, |
208 |
g.c.name == name, |
209 |
g.c.member == member)) |
210 |
values = {} |
211 |
if owner_match:
|
212 |
values['owner'] = owner_match
|
213 |
if member_match:
|
214 |
values['member'] = member_match
|
215 |
u = u.values(values) |
216 |
connection.execute(u) |
217 |
bar.next() |
218 |
bar.finish() |
219 |
|
220 |
|
221 |
def upgrade(): |
222 |
migrate(get_uuid) |
223 |
|
224 |
|
225 |
def downgrade(): |
226 |
migrate(get_displayname) |