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