cubicweb/misc/scripts/repair_file_1-9_migration.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10589 misc/scripts/repair_file_1-9_migration.py@7c23b7de2b8d
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile

"""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 print_function

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

defaultadmin = repo.config.default_admin_config
backupcfg = cwconfig.instance_configuration(backupinstance)
backupcfg.repairing = True
backuprepo, backupcnx = dbapi.in_memory_repo_cnx(backupcfg, defaultadmin['login'],
                                                 password=defaultadmin['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), end=' ')
            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), end=' ')
            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()