root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / versions / 165ba3fbfe53_update_path_account.py @ 50149cc8
History | View | Annotate | Download (5.5 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_username as get_user_username |
17 |
from pithos.api.settings import ( |
18 |
SERVICE_TOKEN, USER_INFO_URL, AUTHENTICATION_USERS) |
19 |
|
20 |
import sqlalchemy as sa |
21 |
|
22 |
catalog = {} |
23 |
def get_uuid(account): |
24 |
global catalog
|
25 |
uuid = catalog.get(account) |
26 |
if uuid:
|
27 |
return uuid
|
28 |
try:
|
29 |
uuid = get_user_uuid( |
30 |
SERVICE_TOKEN, account, USER_INFO_URL, AUTHENTICATION_USERS) |
31 |
except Exception, e: |
32 |
print 'Unable to retrieve uuid for %s: %s' % (account, e) |
33 |
return
|
34 |
else:
|
35 |
if uuid:
|
36 |
catalog[account] = uuid |
37 |
return uuid
|
38 |
|
39 |
inverse_catalog = {} |
40 |
def get_username(account): |
41 |
global inverse_catalog
|
42 |
username = inverse_catalog.get(account) |
43 |
if username:
|
44 |
return username
|
45 |
try:
|
46 |
username = get_user_username( |
47 |
SERVICE_TOKEN, account, USER_INFO_URL, AUTHENTICATION_USERS) |
48 |
except Exception, e: |
49 |
print 'Unable to retrieve username for %s: %s' % (account, e) |
50 |
return
|
51 |
else:
|
52 |
if username:
|
53 |
catalog[account] = username |
54 |
return username
|
55 |
|
56 |
n = table( |
57 |
'nodes',
|
58 |
column('node', sa.Integer),
|
59 |
column('path', sa.String(2048)) |
60 |
) |
61 |
|
62 |
p = table( |
63 |
'public',
|
64 |
column('public_id', sa.Integer),
|
65 |
column('path', sa.String(2048)) |
66 |
) |
67 |
|
68 |
x = table( |
69 |
'xfeatures',
|
70 |
column('feature_id', sa.Integer),
|
71 |
column('path', sa.String(2048)) |
72 |
) |
73 |
|
74 |
xvals = table( |
75 |
'xfeaturevals',
|
76 |
column('feature_id', sa.Integer),
|
77 |
column('key', sa.Integer),
|
78 |
column('value', sa.String(256)) |
79 |
) |
80 |
|
81 |
|
82 |
def upgrade(): |
83 |
connection = op.get_bind() |
84 |
|
85 |
s = sa.select([n.c.node, n.c.path]) |
86 |
nodes = connection.execute(s).fetchall() |
87 |
for node, path in nodes: |
88 |
account, sep, rest = path.partition('/')
|
89 |
uuid = get_uuid(account) |
90 |
if not uuid: |
91 |
continue
|
92 |
path = sep.join([uuid, rest]) |
93 |
u = n.update().where(n.c.node == node).values({'path':path})
|
94 |
connection.execute(u) |
95 |
|
96 |
s = sa.select([p.c.public_id, p.c.path]) |
97 |
public = connection.execute(s).fetchall() |
98 |
for id, path in public: |
99 |
account, sep, rest = path.partition('/')
|
100 |
uuid = get_uuid(account) |
101 |
if not uuid: |
102 |
continue
|
103 |
path = sep.join([uuid, rest]) |
104 |
u = p.update().where(p.c.public_id == id).values({'path':path}) |
105 |
connection.execute(u) |
106 |
|
107 |
s = sa.select([x.c.feature_id, x.c.path]) |
108 |
xfeatures = connection.execute(s).fetchall() |
109 |
for id, path in xfeatures: |
110 |
account, sep, rest = path.partition('/')
|
111 |
uuid = get_uuid(account) |
112 |
if not uuid: |
113 |
continue
|
114 |
path = sep.join([uuid, rest]) |
115 |
u = x.update().where(x.c.feature_id == id).values({'path':path}) |
116 |
connection.execute(u) |
117 |
|
118 |
s = sa.select([xvals.c.feature_id, xvals.c.key, xvals.c.value]) |
119 |
s = s.where(xvals.c.value != '*')
|
120 |
xfeaturevals = connection.execute(s).fetchall() |
121 |
for feature_id, key, value in xfeaturevals: |
122 |
account, sep, group = value.partition(':')
|
123 |
uuid = get_uuid(account) |
124 |
if not uuid: |
125 |
continue
|
126 |
new_value = sep.join([uuid, group]) |
127 |
u = xvals.update() |
128 |
u = u.where(and_( |
129 |
xvals.c.feature_id == feature_id, |
130 |
xvals.c.key == key, |
131 |
xvals.c.value == value)) |
132 |
u = u.values({'value':new_value})
|
133 |
connection.execute(u) |
134 |
|
135 |
|
136 |
def downgrade(): |
137 |
connection = op.get_bind() |
138 |
|
139 |
s = sa.select([n.c.node, n.c.path]) |
140 |
nodes = connection.execute(s).fetchall() |
141 |
for node, path in nodes: |
142 |
account, sep, rest = path.partition('/')
|
143 |
username = get_username(account) |
144 |
if not username: |
145 |
continue
|
146 |
path = sep.join([username, rest]) |
147 |
u = n.update().where(n.c.node == node).values({'path':path})
|
148 |
connection.execute(u) |
149 |
|
150 |
s = sa.select([p.c.public_id, p.c.path]) |
151 |
public = connection.execute(s).fetchall() |
152 |
for id, path in public: |
153 |
account, sep, rest = path.partition('/')
|
154 |
username = get_username(account) |
155 |
if not username: |
156 |
continue
|
157 |
path = sep.join([username, rest]) |
158 |
u = p.update().where(p.c.public_id == id).values({'path':path}) |
159 |
connection.execute(u) |
160 |
|
161 |
s = sa.select([x.c.feature_id, x.c.path]) |
162 |
xfeatures = connection.execute(s).fetchall() |
163 |
for id, path in xfeatures: |
164 |
account, sep, rest = path.partition('/')
|
165 |
username = get_username(account) |
166 |
if not username: |
167 |
continue
|
168 |
path = sep.join([username, rest]) |
169 |
u = x.update().where(x.c.feature_id == id).values({'path':path}) |
170 |
connection.execute(u) |
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 |
for feature_id, key, value in xfeaturevals: |
176 |
account, sep, group = value.partition(':')
|
177 |
username = get_username(account) |
178 |
if not username: |
179 |
continue
|
180 |
new_value = sep.join([username, group]) |
181 |
u = xvals.update() |
182 |
u = u.where(and_( |
183 |
xvals.c.feature_id == feature_id, |
184 |
xvals.c.key == key, |
185 |
xvals.c.value ==value)) |
186 |
u = u.values({'value':new_value})
|
187 |
connection.execute(u) |