misc/scripts/repair_file_1-9_migration.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 14 Jun 2012 15:21:12 +0200
changeset 8444 7a861620f64f
parent 7056 51f88f13d6f3
child 8694 d901c36bcfce
permissions -rw-r--r--
[login] redirect to real instance root if no postlogin_path is provided When not postlogin_path is provided, the login form issue a redirect to "/". The instance root may not be at "/" on the server. Then issuing a redirect to "/" send the user to the wrong location. We now redirect to "." which works fine because the "login" controller a direct children of instance root (http://babar.com/instance/login). All other redirection of the login controller use relative path too and then rely on this relative path from the login controleur to the instance root. This mechanism may be considered fragile and may deserve a proper fix. but this is to be discussed and implemented in another changeset.

"""execute this script if you've migration to file >= 1.9.0 with cubicweb <= 3.9.2

FYI, this migration occurred :
* on our intranet on July 07 2010
* on our extranet on July 16 2010
"""
from __future__ import with_statement

try:
    backupinstance, = __args__
except ValueError:
    print 'USAGE: cubicweb-ctl shell <instance> repair_file_1-9_migration.py -- <backup instance id>'
    print
    print 'you should restored the backup on a new instance, accessible through pyro'

from cubicweb import cwconfig, dbapi
from cubicweb.server.session import hooks_control

sourcescfg = repo.config.sources()
backupcfg = cwconfig.instance_configuration(backupinstance)
backupcfg.repairing = True
backuprepo, backupcnx = dbapi.in_memory_repo_cnx(backupcfg, sourcescfg['admin']['login'],
                                                 password=sourcescfg['admin']['password'],
                                                 host='localhost')
backupcu = backupcnx.cursor()

with hooks_control(session, session.HOOKS_DENY_ALL):
    rql('SET X is Y WHERE X is File, Y name "File", NOT X is Y')
    rql('SET X is_instance_of Y WHERE X is File, Y name "File", NOT X is_instance_of Y')
    for rtype, in backupcu.execute('DISTINCT Any RTN WHERE X relation_type RT, RT name RTN,'
                                   'X from_entity Y, Y name "Image", X is CWRelation, '
                                   'EXISTS(XX is CWRelation, XX relation_type RT, '
                                   'XX from_entity YY, YY name "File")'):
        if rtype in ('is', 'is_instance_of'):
            continue
        print rtype
        for feid, xeid in backupcu.execute('Any F,X WHERE F %s X, F is IN (File,Image)' % rtype):
            print 'restoring relation %s between file %s and %s' % (rtype, feid, xeid),
            print rql('SET F %s X WHERE F eid %%(f)s, X eid %%(x)s, NOT F %s X' % (rtype, rtype),
                      {'f': feid, 'x': xeid})

    for rtype, in backupcu.execute('DISTINCT Any RTN WHERE X relation_type RT, RT name RTN,'
                                   'X to_entity Y, Y name "Image", X is CWRelation, '
                                   'EXISTS(XX is CWRelation, XX relation_type RT, '
                                   'XX to_entity YY, YY name "File")'):
        print rtype
        for feid, xeid in backupcu.execute('Any F,X WHERE X %s F, F is IN (File,Image)' % rtype):
            print 'restoring relation %s between %s and file %s' % (rtype, xeid, feid),
            print rql('SET X %s F WHERE F eid %%(f)s, X eid %%(x)s, NOT X %s F' % (rtype, rtype),
                      {'f': feid, 'x': xeid})

commit()