Commit 6a8e169b authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

pithos: Fix migrations

Some data migrations require to import pithos app settings.
These migrations used to fail in nodes where snf-pithos-app
is not installed.
Disable these migrations if they fail to import the necessary settings.
parent fcc62e5d
......@@ -13,11 +13,10 @@ down_revision = '3dd56e750a3'
from alembic import op
from sqlalchemy.sql import table, column, and_
from pithos.api.settings import (SERVICE_TOKEN, ASTAKOS_BASE_URL)
from astakosclient import AstakosClient
from astakosclient.errors import NoUserName, NoUUID
astakos_client = AstakosClient(ASTAKOS_BASE_URL, retry=3, use_pool=True)
import functools
try:
from progress.bar import IncrementalBar
......@@ -37,14 +36,13 @@ import sqlalchemy as sa
catalog = {}
def get_uuid(account):
def _get_uuid(account, service_token, astakos_client):
global catalog
if account in catalog:
return catalog[account]
try:
catalog[account] = astakos_client.service_get_uuid(
SERVICE_TOKEN, account
)
catalog[account] = astakos_client.service_get_uuid(service_token,
account)
print '\n', account, '-->', catalog[account]
except NoUUID:
return None
......@@ -56,14 +54,13 @@ def get_uuid(account):
inverse_catalog = {}
def get_displayname(account):
def _get_displayname(account, service_token, astakos_client):
global inverse_catalog
if account in inverse_catalog:
return inverse_catalog[account]
try:
inverse_catalog[account] = astakos_client.service_get_username(
SERVICE_TOKEN, account
)
service_token, account)
print '\n', account, '-->', inverse_catalog[account]
except NoUserName:
return None
......@@ -219,8 +216,31 @@ def migrate(callback):
def upgrade():
migrate(get_uuid)
try:
from pithos.api import settings
except ImportError:
return
else:
astakos_client = AstakosClient(settings.ASTAKOS_BASE_URL,
retry=3,
use_pool=True)
get_uuid = functools.partial(_get_uuid,
service_token=settings.SERVICE_TOKEN,
astakos_client=astakos_client)
migrate(get_uuid)
def downgrade():
migrate(get_displayname)
try:
from pithos.api import settings
except ImportError:
return
else:
astakos_client = AstakosClient(settings.ASTAKOS_BASE_URL,
retry=3,
use_pool=True)
get_displayname = functools.partial(
_get_displayname,
service_token=settings.SERVICE_TOKEN,
astakos_client=astakos_client)
migrate(get_displayname)
......@@ -14,7 +14,6 @@ from alembic import op
import sqlalchemy as sa
from pithos.backends.modular import ULTIMATE_ANSWER
from pithos.api.short_url import encode_url
def upgrade():
......@@ -27,13 +26,20 @@ def upgrade():
sa.sql.column('public_id', sa.Integer),
sa.sql.column('url', sa.String),
)
get_url = lambda x: encode_url(x + ULTIMATE_ANSWER)
conn = op.get_bind()
s = sa.select([p.c.public_id])
rows = conn.execute(s).fetchall()
for r in rows:
s = p.update().values(url=get_url(r[0])).where(p.c.public_id == r[0])
op.execute(s)
try:
from pithos.api.short_url import encode_url
except ImportError:
return
else:
get_url = lambda x: encode_url(x + ULTIMATE_ANSWER)
conn = op.get_bind()
s = sa.select([p.c.public_id])
rows = conn.execute(s).fetchall()
for r in rows:
s = p.update().values(url=get_url(r[0])).where(
p.c.public_id == r[0])
op.execute(s)
def downgrade():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment