misc/migration/bootstrapmigration_repository.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 17 Mar 2011 13:43:07 +0100
brancholdstable
changeset 7095 1831c3154581
parent 6896 784d5df35af1
child 7398 26695dd703d8
child 7549 465009397d91
permissions -rw-r--r--
[devtools] missing py2.5 compat import
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4944
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""allways executed before all others in server migration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
it should only include low level schema changes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
"""
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    22
from __future__ import with_statement
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    23
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    24
from cubicweb.server.session import hooks_control
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    25
from cubicweb.server import schemaserial as ss
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
2275
bc0bed0616a3 fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    27
applcubicwebversion, cubicwebversion = versions_map['cubicweb']
bc0bed0616a3 fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    28
4570
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    29
def _add_relation_definition_no_perms(subjtype, rtype, objtype):
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    30
    rschema = fsschema.rschema(rtype)
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    31
    rdef = rschema.rdefs[(subjtype, objtype)]
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    32
    rdef.rtype = schema.rschema(rtype)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    33
    rdef.subject = schema.eschema(subjtype)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    34
    rdef.object = schema.eschema(objtype)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    35
    ss.execschemarql(rql, rdef, ss.rdef2rql(rdef, CSTRMAP, groupmap=None))
4570
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    36
    commit(ask_confirm=False)
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    37
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    38
if applcubicwebversion == (3, 6, 0) and cubicwebversion >= (3, 6, 0):
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    39
    CSTRMAP = dict(rql('Any T, X WHERE X is CWConstraintType, X name T',
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    40
                       ask_confirm=False))
4570
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    41
    _add_relation_definition_no_perms('CWAttribute', 'update_permission', 'CWGroup')
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    42
    _add_relation_definition_no_perms('CWAttribute', 'update_permission', 'RQLExpression')
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4834
diff changeset
    43
    rql('SET X update_permission Y WHERE X is CWAttribute, X add_permission Y')
4570
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    44
    drop_relation_definition('CWAttribute', 'add_permission', 'CWGroup')
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    45
    drop_relation_definition('CWAttribute', 'add_permission', 'RQLExpression')
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    46
    drop_relation_definition('CWAttribute', 'delete_permission', 'CWGroup')
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    47
    drop_relation_definition('CWAttribute', 'delete_permission', 'RQLExpression')
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    48
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    49
elif applcubicwebversion < (3, 6, 0) and cubicwebversion >= (3, 6, 0):
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    50
    CSTRMAP = dict(rql('Any T, X WHERE X is CWConstraintType, X name T',
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    51
                       ask_confirm=False))
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3890
diff changeset
    52
    session.set_pool()
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3890
diff changeset
    53
    permsdict = ss.deserialize_ertype_permissions(session)
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3890
diff changeset
    54
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    55
    with hooks_control(session, session.HOOKS_ALLOW_ALL, 'integrity'):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    56
        for rschema in repo.schema.relations():
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    57
            rpermsdict = permsdict.get(rschema.eid, {})
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    58
            for rdef in rschema.rdefs.values():
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    59
                for action in rdef.ACTIONS:
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    60
                    actperms = []
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    61
                    for something in rpermsdict.get(action == 'update' and 'add' or action, ()):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    62
                        if isinstance(something, tuple):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    63
                            actperms.append(rdef.rql_expression(*something))
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    64
                        else: # group name
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    65
                            actperms.append(something)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    66
                    rdef.set_action_permissions(action, actperms)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    67
        for action in ('read', 'add', 'delete'):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    68
            _add_relation_definition_no_perms('CWRelation', '%s_permission' % action, 'CWGroup')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    69
            _add_relation_definition_no_perms('CWRelation', '%s_permission' % action, 'RQLExpression')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    70
        for action in ('read', 'update'):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    71
            _add_relation_definition_no_perms('CWAttribute', '%s_permission' % action, 'CWGroup')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    72
            _add_relation_definition_no_perms('CWAttribute', '%s_permission' % action, 'RQLExpression')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    73
        for action in ('read', 'add', 'delete'):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    74
            rql('SET X %s_permission Y WHERE X is CWRelation, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    75
                'RT %s_permission Y, X relation_type RT, Y is CWGroup' % (action, action))
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    76
            rql('INSERT RQLExpression Y: Y exprtype YET, Y mainvars YMV, Y expression YEX, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    77
                'X %s_permission Y WHERE X is CWRelation, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    78
                'X relation_type RT, RT %s_permission Y2, Y2 exprtype YET, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    79
                'Y2 mainvars YMV, Y2 expression YEX' % (action, action))
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    80
        rql('SET X read_permission Y WHERE X is CWAttribute, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    81
            'RT read_permission Y, X relation_type RT, Y is CWGroup')
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3890
diff changeset
    82
        rql('INSERT RQLExpression Y: Y exprtype YET, Y mainvars YMV, Y expression YEX, '
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    83
            'X read_permission Y WHERE X is CWAttribute, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    84
            'X relation_type RT, RT read_permission Y2, Y2 exprtype YET, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    85
            'Y2 mainvars YMV, Y2 expression YEX')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    86
        rql('SET X update_permission Y WHERE X is CWAttribute, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    87
            'RT add_permission Y, X relation_type RT, Y is CWGroup')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    88
        rql('INSERT RQLExpression Y: Y exprtype YET, Y mainvars YMV, Y expression YEX, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    89
            'X update_permission Y WHERE X is CWAttribute, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    90
            'X relation_type RT, RT add_permission Y2, Y2 exprtype YET, '
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    91
            'Y2 mainvars YMV, Y2 expression YEX')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    92
        for action in ('read', 'add', 'delete'):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    93
            drop_relation_definition('CWRType', '%s_permission' % action, 'CWGroup', commit=False)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
    94
            drop_relation_definition('CWRType', '%s_permission' % action, 'RQLExpression')
6222
21772f4e41d9 [migration] attempt to fix pre 3.6 migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    95
    sync_schema_props_perms('read_permission', syncperms=False) # fix read_permission cardinality
21772f4e41d9 [migration] attempt to fix pre 3.6 migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    96
21772f4e41d9 [migration] attempt to fix pre 3.6 migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    97
if applcubicwebversion < (3, 9, 6) and cubicwebversion >= (3, 9, 6):
21772f4e41d9 [migration] attempt to fix pre 3.6 migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    98
    add_entity_type('CWUniqueTogetherConstraint')
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3890
diff changeset
    99
6896
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   100
if not ('CWUniqueTogetherConstraint', 'CWRType') in schema['relations'].rdefs:
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   101
    add_relation_definition('CWUniqueTogetherConstraint', 'relations', 'CWRType')
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   102
    rql('SET C relations RT WHERE C relations RDEF, RDEF relation_type RT')
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   103
    commit()
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   104
    drop_relation_definition('CWUniqueTogetherConstraint', 'relations', 'CWAttribute')
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   105
    drop_relation_definition('CWUniqueTogetherConstraint', 'relations', 'CWRelation')
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   106
784d5df35af1 [migration] cwunique constraint schema fix must be done in bootstrap migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6222
diff changeset
   107
2463
5200c0f7d2d5 add migration script for the new cwuri metadata attribute
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2462
diff changeset
   108
if applcubicwebversion < (3, 4, 0) and cubicwebversion >= (3, 4, 0):
2892
7ae3d62a8328 hooks have been refactored, call [re|de]activate_verification_hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2696
diff changeset
   109
4944
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   110
    with hooks_control(session, session.HOOKS_ALLOW_ALL, 'integrity'):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   111
        session.set_shared_data('do-not-insert-cwuri', True)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   112
        add_relation_type('cwuri')
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   113
        base_url = session.base_url()
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   114
        for eid, in rql('Any X', ask_confirm=False):
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   115
            type, source, extid = session.describe(eid)
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   116
            if source == 'system':
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   117
                rql('SET X cwuri %(u)s WHERE X eid %(x)s',
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   118
                    {'x': eid, 'u': base_url + u'eid/%s' % eid})
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   119
        isession.commit()
65533aee8d0a fix migration when upgrading from cw < 3.6 to cw 3.7
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   120
        session.set_shared_data('do-not-insert-cwuri', False)
2463
5200c0f7d2d5 add migration script for the new cwuri metadata attribute
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2462
diff changeset
   121
2965
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   122
if applcubicwebversion < (3, 5, 0) and cubicwebversion >= (3, 5, 0):
3788
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   123
    # check that migration is not doomed
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   124
    rset = rql('Any X,Y WHERE X transition_of E, Y transition_of E, '
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   125
               'X name N, Y name N, NOT X identity Y',
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   126
               ask_confirm=False)
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   127
    if rset:
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   128
        from logilab.common.shellutils import ASK
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   129
        if not ASK.confirm('Migration will fail because of transitions with the same name. '
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   130
                           'Continue anyway ?'):
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   131
            import sys
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   132
            sys.exit(1)
3d5abcd7c852 [migration] warn if 3.5 workflow migration is going to fail
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 3305
diff changeset
   133
    # proceed with migration
2965
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   134
    add_entity_type('Workflow')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   135
    add_entity_type('BaseTransition')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   136
    add_entity_type('WorkflowTransition')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   137
    add_entity_type('SubWorkflowExitPoint')
3218
2a4bbe3fa4f3 [3.5 migration] avoid removing defined transitions...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2965
diff changeset
   138
    # drop explicit 'State allowed_transition Transition' since it should be
2a4bbe3fa4f3 [3.5 migration] avoid removing defined transitions...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2965
diff changeset
   139
    # infered due to yams inheritance.  However we've to disable the schema
2a4bbe3fa4f3 [3.5 migration] avoid removing defined transitions...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2965
diff changeset
   140
    # sync hook first to avoid to destroy existing data...
2a4bbe3fa4f3 [3.5 migration] avoid removing defined transitions...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2965
diff changeset
   141
    try:
3395
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   142
        from cubicweb.hooks import syncschema
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   143
        repo.vreg.unregister(syncschema.AfterDelRelationTypeHook)
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   144
        try:
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   145
            drop_relation_definition('State', 'allowed_transition', 'Transition')
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   146
        finally:
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   147
            repo.vreg.register(syncschema.AfterDelRelationTypeHook)
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   148
    except ImportError: # syncschema is in CW >= 3.6 only
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   149
        from cubicweb.server.schemahooks import after_del_relation_type
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   150
        repo.hm.unregister_hook(after_del_relation_type,
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   151
                                'after_delete_relation', 'relation_type')
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   152
        try:
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   153
            drop_relation_definition('State', 'allowed_transition', 'Transition')
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   154
        finally:
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   155
            repo.hm.register_hook(after_del_relation_type,
405f393bcac0 [migration] fix bootstrap migration of 3.5 : need to support old-style and new-style hooks to migrate from 3.4 to 3.6
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   156
                                  'after_delete_relation', 'relation_type')
2965
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   157
    schema.rebuild_infered_relations() # need to be explicitly called once everything is in place
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   158
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   159
    for et in rql('DISTINCT Any ET,ETN WHERE S state_of ET, ET name ETN',
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   160
                  ask_confirm=False).entities():
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   161
        wf = add_workflow(u'default %s workflow' % et.name, et.name,
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   162
                          ask_confirm=False)
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   163
        rql('SET S state_of WF WHERE S state_of ET, ET eid %(et)s, WF eid %(wf)s',
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   164
            {'et': et.eid, 'wf': wf.eid}, 'et', ask_confirm=False)
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   165
        rql('SET T transition_of WF WHERE T transition_of ET, ET eid %(et)s, WF eid %(wf)s',
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   166
            {'et': et.eid, 'wf': wf.eid}, 'et', ask_confirm=False)
3305
abae9045d2e4 [migration] fix initial_state migration: at this point, S state_of ET is no more true (but we don't need it...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3218
diff changeset
   167
        rql('SET WF initial_state S WHERE ET initial_state S, ET eid %(et)s, WF eid %(wf)s',
2965
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   168
            {'et': et.eid, 'wf': wf.eid}, 'et', ask_confirm=False)
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   169
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   170
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   171
    rql('DELETE TrInfo TI WHERE NOT TI from_state S')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   172
    rql('SET TI by_transition T WHERE TI from_state FS, TI to_state TS, '
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   173
        'FS allowed_transition T, T destination_state TS')
4042
f85a4c846aad 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4011
diff changeset
   174
    commit()
2965
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   175
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   176
    drop_relation_definition('State', 'state_of', 'CWEType')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   177
    drop_relation_definition('Transition', 'transition_of', 'CWEType')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   178
    drop_relation_definition('CWEType', 'initial_state', 'State')
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   179
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   180
    sync_schema_props_perms()
75ad5cb00b88 3.5 migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   181
1952
8e19c813750d fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1501
diff changeset
   182
if applcubicwebversion < (3, 2, 2) and cubicwebversion >= (3, 2, 1):
2462
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   183
    from base64 import b64encode
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   184
    for table in ('entities', 'deleted_entities'):
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   185
        for eid, extid in sql('SELECT eid, extid FROM %s WHERE extid is NOT NULL'
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   186
                              % table, ask_confirm=False):
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   187
            sql('UPDATE %s SET extid=%%(extid)s WHERE eid=%%(eid)s' % table,
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   188
                {'extid': b64encode(extid), 'eid': eid}, ask_confirm=False)
4042
f85a4c846aad 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4011
diff changeset
   189
    commit()
1952
8e19c813750d fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1501
diff changeset
   190
1501
c80ca83a382f migrate card cube in bootstrap migration
sylvain.thenault@logilab.fr
parents: 1399
diff changeset
   191
if applcubicwebversion < (3, 2, 0) and cubicwebversion >= (3, 2, 0):
2462
9e670072884d fix indentation problems in bootstrap_migration (use 4 spaces instead of 3)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2275
diff changeset
   192
    add_cube('card', update_database=False)