server/schemaserial.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 09 Sep 2010 18:23:43 +0200
branchstable
changeset 6199 a88902af3813
parent 6191 ece6996e6ac8
child 6208 07b176640a8c
permissions -rw-r--r--
remove debug print
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
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: 4835
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5896
67683b7e591a cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5558
diff changeset
    18
"""functions for schema / permissions (de)serialization using RQL"""
0
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
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
3854
8633cd05b6b5 no progress bar in apycot environment
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
    22
import os
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from itertools import chain
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
from logilab.common.shellutils import ProgressBar
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
    27
from yams import BadSchemaDefinition, schema as schemamod, buildobjs as ybo
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
5970
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    29
from cubicweb import CW_SOFTWARE_ROOT, typed_eid
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    30
from cubicweb.schema import (CONSTRAINTS, ETYPE_NAME_MAP,
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    31
                             VIRTUAL_RTYPES, PURE_VIRTUAL_RTYPES)
1869
642a1a120a92 should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1630
diff changeset
    32
from cubicweb.server import sqlutils
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
def group_mapping(cursor, interactive=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    """create a group mapping from an rql cursor
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    A group mapping has standard group names as key (managers, owners at least)
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    38
    and the actual CWGroup entity's eid as associated value.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    In interactive mode (the default), missing groups'eid will be prompted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    from the user.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    res = {}
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    43
    for eid, name in cursor.execute('Any G, N WHERE G is CWGroup, G name N',
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    44
                                    build_descr=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        res[name] = eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    if not interactive:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        return res
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    missing = [g for g in ('owners', 'managers', 'users', 'guests') if not g in res]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    if missing:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        print 'some native groups are missing but the following groups have been found:'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        print '\n'.join('* %s (%s)' % (n, eid) for n, eid in res.items())
1630
41aadba8b29f delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    52
        print
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        print 'enter the eid of a to group to map to each missing native group'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        print 'or just type enter to skip permissions granted to a group'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        for group in missing:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
            while True:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
                value = raw_input('eid for group %s: ' % group).strip()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
                if not value:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
                try:
5970
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    61
                    eid = typed_eid(value)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
                except ValueError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
                    print 'eid should be an integer'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
                    continue
5970
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    65
                for eid_ in res.values():
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    66
                    if eid == eid_:
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    67
                        break
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    68
                else:
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    69
                    print 'eid is not a group eid'
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    70
                    continue
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    71
                res[name] = eid
fb8acdab4e12 [broken schema] fix the group_mapping function in case where some standard group isn't found: we currently loop for ever, boring the user by asking ever and ever the same question...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5896
diff changeset
    72
                break
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    return res
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
    75
def cstrtype_mapping(cursor):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
    76
    """cached constraint types mapping"""
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    77
    map = dict(cursor.execute('Any T, X WHERE X is CWConstraintType, X name T'))
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    78
    if not 'BoundConstraint' in map:
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    79
        map['BoundConstraint'] = map['BoundaryConstraint']
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    80
    return map
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
    81
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
# schema / perms deserialization ##############################################
4791
b9a1b16a8d26 fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4789
diff changeset
    83
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
def deserialize_schema(schema, session):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    """return a schema according to information stored in an rql database
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    86
    as CWRType and CWEType entities
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    """
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    88
    repo = session.repo
2044
d5589c5bc4e6 alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    89
    dbhelper = repo.system_source.dbhelper
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
    90
    # XXX bw compat (3.6 migration)
4475
37c413a07216 kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    91
    sqlcu = session.pool['system']
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
    92
    sqlcu.execute("SELECT * FROM cw_CWRType WHERE cw_name='symetric'")
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
    93
    if sqlcu.fetchall():
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
    94
        sql = dbhelper.sql_rename_col('cw_CWRType', 'cw_symetric', 'cw_symmetric',
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
    95
                                      dbhelper.TYPE_MAPPING['Boolean'], True)
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
    96
        sqlcu.execute(sql)
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
    97
        sqlcu.execute("UPDATE cw_CWRType SET cw_name='symmetric' WHERE cw_name='symetric'")
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
    98
    ertidx = {}
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
    99
    copiedeids = set()
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   100
    permsidx = deserialize_ertype_permissions(session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    schema.reading_from_database = True
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   102
    for eid, etype, desc in session.execute(
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   103
        'Any X, N, D WHERE X is CWEType, X name N, X description D',
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   104
        build_descr=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        # base types are already in the schema, skip them
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        if etype in schemamod.BASE_TYPES:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
            # just set the eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
            eschema = schema.eschema(etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
            eschema.eid = eid
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   110
            ertidx[eid] = etype
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
            continue
2044
d5589c5bc4e6 alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   112
        if etype in ETYPE_NAME_MAP:
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   113
            needcopy = False
2044
d5589c5bc4e6 alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   114
            netype = ETYPE_NAME_MAP[etype]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
            # can't use write rql queries at this point, use raw sql
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   116
            sqlexec = session.system_sql
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   117
            if sqlexec('SELECT 1 FROM %(p)sCWEType WHERE %(p)sname=%%(n)s'
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   118
                       % {'p': sqlutils.SQL_PREFIX}, {'n': netype}).fetchone():
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   119
                # the new type already exists, we should copy (eg make existing
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   120
                # instances of the old type instances of the new type)
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   121
                assert etype.lower() != netype.lower()
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   122
                needcopy = True
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   123
            else:
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   124
                # the new type doesn't exist, we should rename
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   125
                sqlexec('UPDATE %(p)sCWEType SET %(p)sname=%%(n)s WHERE %(p)seid=%%(x)s'
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   126
                        % {'p': sqlutils.SQL_PREFIX}, {'x': eid, 'n': netype})
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   127
                if etype.lower() != netype.lower():
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   128
                    sqlexec('ALTER TABLE %s%s RENAME TO %s%s' % (
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   129
                        sqlutils.SQL_PREFIX, etype, sqlutils.SQL_PREFIX, netype))
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   130
            sqlexec('UPDATE entities SET type=%(n)s WHERE type=%(x)s',
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   131
                    {'x': etype, 'n': netype})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
            session.commit(False)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
            try:
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   134
                sqlexec('UPDATE deleted_entities SET type=%(n)s WHERE type=%(x)s',
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   135
                        {'x': etype, 'n': netype})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
            except:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
                pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
            tocleanup = [eid]
1630
41aadba8b29f delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   139
            tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
                          if etype == eidetype)
1630
41aadba8b29f delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   141
            repo.clear_caches(tocleanup)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
            session.commit(False)
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   143
            if needcopy:
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   144
                ertidx[eid] = netype
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   145
                copiedeids.add(eid)
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   146
                # copy / CWEType entity removal expected to be done through
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   147
                # rename_entity_type in a migration script
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   148
                continue
2044
d5589c5bc4e6 alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   149
            etype = netype
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   150
        ertidx[eid] = etype
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   151
        eschema = schema.add_entity_type(
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   152
            ybo.EntityType(name=etype, description=desc, eid=eid))
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   153
        set_perms(eschema, permsidx)
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   154
    for etype, stype in session.execute(
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   155
        'Any XN, ETN WHERE X is CWEType, X name XN, X specializes ET, ET name ETN',
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   156
        build_descr=False):
6080
d0cb8fde4957 [schema] consider ETYPE_NAME_MAP when deserializing specialization relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5970
diff changeset
   157
        etype = ETYPE_NAME_MAP.get(etype, etype)
d0cb8fde4957 [schema] consider ETYPE_NAME_MAP when deserializing specialization relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5970
diff changeset
   158
        stype = ETYPE_NAME_MAP.get(stype, stype)
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   159
        schema.eschema(etype)._specialized_type = stype
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   160
        schema.eschema(stype)._specialized_by.append(etype)
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   161
    for eid, rtype, desc, sym, il, ftc in session.execute(
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   162
        'Any X,N,D,S,I,FTC WHERE X is CWRType, X name N, X description D, '
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
   163
        'X symmetric S, X inlined I, X fulltext_container FTC', build_descr=False):
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   164
        ertidx[eid] = rtype
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   165
        rschema = schema.add_relation_type(
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   166
            ybo.RelationType(name=rtype, description=desc,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   167
                             symmetric=bool(sym), inlined=bool(il),
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   168
                             fulltext_container=ftc, eid=eid))
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   169
    cstrsidx = deserialize_rdef_constraints(session)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   170
    pendingrdefs = []
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   171
    # closure to factorize common code of attribute/relation rdef addition
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   172
    def _add_rdef(rdefeid, seid, reid, oeid, **kwargs):
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   173
        rdef = ybo.RelationDefinition(ertidx[seid], ertidx[reid], ertidx[oeid],
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   174
                                      constraints=cstrsidx.get(rdefeid, ()),
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   175
                                      eid=rdefeid, **kwargs)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   176
        if seid in copiedeids or oeid in copiedeids:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   177
            # delay addition of this rdef. We'll insert them later if needed. We
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   178
            # have to do this because:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   179
            #
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   180
            # * on etype renaming, we want relation of the old entity type being
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   181
            #   redirected to the new type during migration
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   182
            #
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   183
            # * in the case of a copy, we've to take care that rdef already
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   184
            #   existing in the schema are not overwritten by a redirected one,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   185
            #   since we want correct eid on them (redirected rdef will be
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   186
            #   removed in rename_entity_type)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   187
            pendingrdefs.append(rdef)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   188
        else:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   189
            # add_relation_def return a RelationDefinitionSchema if it has been
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   190
            # actually added (can be None on duplicated relation definitions,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   191
            # e.g. if the relation type is marked as beeing symmetric)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   192
            rdefs = schema.add_relation_def(rdef)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   193
            if rdefs is not None:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   194
                set_perms(rdefs, permsidx)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   195
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    for values in session.execute(
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   197
        'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        'X description DESC, X internationalizable I18N, X defaultval DFLT,'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
        'X fulltextindexed FTIDX, X from_entity SE, X to_entity OE',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        build_descr=False):
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   202
        rdefeid, seid, reid, oeid, card, ord, desc, idx, ftidx, i18n, default = values
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   203
        _add_rdef(rdefeid, seid, reid, oeid,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   204
                  cardinality=card, description=desc, order=ord,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   205
                  indexed=idx, fulltextindexed=ftidx, internationalizable=i18n,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   206
                  default=default)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
    for values in session.execute(
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   208
        'Any X,SE,RT,OE,CARD,ORD,DESC,C WHERE X is CWRelation, X relation_type RT,'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        'X cardinality CARD, X ordernum ORD, X description DESC, '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        'X from_entity SE, X to_entity OE, X composite C', build_descr=False):
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   211
        rdefeid, seid, reid, oeid, card, ord, desc, comp = values
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   212
        _add_rdef(rdefeid, seid, reid, oeid,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   213
                  cardinality=card, description=desc, order=ord,
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   214
                  composite=comp)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   215
    for rdef in pendingrdefs:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   216
        try:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   217
            rdefs = schema.add_relation_def(rdef)
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   218
        except BadSchemaDefinition:
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   219
            continue
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   220
        if rdefs is not None:
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   221
            set_perms(rdefs, permsidx)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
    schema.infer_specialization_rules()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    session.commit()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    schema.reading_from_database = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
def deserialize_ertype_permissions(session):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
    """return sect action:groups associations for the given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    entity or relation schema with its eid, according to schema's
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
    permissions stored in the database as [read|add|delete|update]_permission
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   231
    relations between CWEType/CWRType and CWGroup entities
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
    res = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
    for action in ('read', 'add', 'update', 'delete'):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   235
        rql = 'Any E,N WHERE G is CWGroup, G name N, E %s_permission G' % action
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        for eid, gname in session.execute(rql, build_descr=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
            res.setdefault(eid, {}).setdefault(action, []).append(gname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
        rql = ('Any E,X,EXPR,V WHERE X is RQLExpression, X expression EXPR, '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
               'E %s_permission X, X mainvars V' % action)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
        for eid, expreid, expr, mainvars in session.execute(rql, build_descr=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
            # we don't know yet if it's a rql expr for an entity or a relation,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
            # so append a tuple to differentiate from groups and so we'll be
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
            # able to instantiate it later
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
            res.setdefault(eid, {}).setdefault(action, []).append( (expr, mainvars, expreid) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
    return res
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
4760
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   247
def deserialize_rdef_constraints(session):
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   248
    """return the list of relation definition's constraints as instances"""
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   249
    res = {}
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   250
    for rdefeid, ceid, ct, val in session.execute(
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   251
        'Any E, X,TN,V WHERE E constrained_by X, X is CWConstraint, '
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   252
        'X cstrtype T, T name TN, X value V', build_descr=False):
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   253
        cstr = CONSTRAINTS[ct].deserialize(val)
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   254
        cstr.eid = ceid
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   255
        res.setdefault(rdefeid, []).append(cstr)
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   256
    return res
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   257
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   258
def set_perms(erschema, permsidx):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
    """set permissions on the given erschema according to the permission
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
    definition dictionary as built by deserialize_ertype_permissions for a
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
    given erschema's eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
    """
4570
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   263
    # reset erschema permissions here to avoid getting yams default anyway
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   264
    erschema.permissions = dict((action, ()) for action in erschema.ACTIONS)
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   265
    try:
6191
ece6996e6ac8 [schema deserial] fix loading of schema when mapping an entity type to an existing one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6189
diff changeset
   266
        thispermsdict = permsidx[erschema.eid]
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   267
    except KeyError:
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   268
        return
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   269
    for action, somethings in thispermsdict.iteritems():
4570
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   270
        # XXX cw < 3.6.1 bw compat
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   271
        if isinstance(erschema, schemamod.RelationDefinitionSchema) and erschema.final and action == 'add':
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   272
            action = 'update'
ede247bbbf62 follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4475
diff changeset
   273
        erschema.permissions[action] = tuple(
4312
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   274
            isinstance(p, tuple) and erschema.rql_expression(*p) or p
740397c24ec3 minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   275
            for p in somethings)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
# schema / perms serialization ################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
4760
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   280
def serialize_schema(cursor, schema):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
    """synchronize schema and permissions in the database according to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
    current schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
    """
3737
e8c669e33689 finally
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3732
diff changeset
   284
    quiet = os.environ.get('APYCOT_ROOT')
3723
a961c7cb2644 be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   285
    if not quiet:
a961c7cb2644 be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   286
        _title = '-> storing the schema in the database '
a961c7cb2644 be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   287
        print _title,
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: 4808
diff changeset
   288
    execute = cursor.execute
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
    eschemas = schema.entities()
4760
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   290
    if not quiet:
4791
b9a1b16a8d26 fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4789
diff changeset
   291
        pb_size = (len(eschemas + schema.relations())
b9a1b16a8d26 fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4789
diff changeset
   292
                   + len(CONSTRAINTS)
b9a1b16a8d26 fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4789
diff changeset
   293
                   + len([x for x in eschemas if x.specializes()]))
2396
8bfb99d7bbcc [cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2395
diff changeset
   294
        pb = ProgressBar(pb_size, title=_title)
3857
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   295
    else:
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   296
        pb = None
4800
686419cd2dff nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4799
diff changeset
   297
    groupmap = group_mapping(cursor, interactive=False)
686419cd2dff nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4799
diff changeset
   298
    # serialize all entity types, assuring CWEType is serialized first for proper
686419cd2dff nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4799
diff changeset
   299
    # is / is_instance_of insertion
4760
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   300
    eschemas.remove(schema.eschema('CWEType'))
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   301
    eschemas.insert(0, schema.eschema('CWEType'))
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   302
    for eschema in eschemas:
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   303
        execschemarql(execute, eschema, eschema2rql(eschema, groupmap))
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   304
        if pb is not None:
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   305
            pb.update()
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   306
    # serialize constraint types
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   307
    cstrtypemap = {}
2597
d9c5a7e0563c [C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2596
diff changeset
   308
    rql = 'INSERT CWConstraintType X: X name %(ct)s'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
    for cstrtype in CONSTRAINTS:
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   310
        if cstrtype == 'BoundConstraint':
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   311
            continue # XXX deprecated in yams 0.29 / cw 3.8.1
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   312
        cstrtypemap[cstrtype] = execute(rql, {'ct': unicode(cstrtype)},
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   313
                                        build_descr=False)[0][0]
3857
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   314
        if pb is not None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
            pb.update()
5379
c082dea0731b [schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   316
    cstrtypemap['BoundConstraint'] = cstrtypemap['BoundaryConstraint']
4760
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   317
    # serialize relations
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   318
    for rschema in schema.relations():
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   319
        # skip virtual relations such as eid, has_text and identity
fe0e307b9b70 minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4722
diff changeset
   320
        if rschema in VIRTUAL_RTYPES:
3857
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   321
            if pb is not None:
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   322
                pb.update()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
            continue
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   324
        execschemarql(execute, rschema, rschema2rql(rschema, addrdef=False))
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   325
        if rschema.symmetric:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   326
            rdefs = [rdef for k, rdef in rschema.rdefs.iteritems()
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   327
                     if (rdef.subject, rdef.object) == k]
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   328
        else:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   329
            rdefs = rschema.rdefs.itervalues()
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   330
        for rdef in rdefs:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   331
            execschemarql(execute, rdef,
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   332
                          rdef2rql(rdef, cstrtypemap, groupmap))
3857
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   333
        if pb is not None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
            pb.update()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
    for rql, kwargs in specialize2rql(schema):
4791
b9a1b16a8d26 fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4789
diff changeset
   336
        execute(rql, kwargs, build_descr=False)
3857
6676a839dc97 ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3854
diff changeset
   337
        if pb is not None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
            pb.update()
3723
a961c7cb2644 be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   339
    if not quiet:
a961c7cb2644 be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   340
        print
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   343
# high level serialization functions
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   344
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   345
def execschemarql(execute, schema, rqls):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   346
    for rql, kwargs in rqls:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   347
        kwargs['x'] = schema.eid
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   348
        rset = execute(rql, kwargs, build_descr=False)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   349
        if schema.eid is None:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   350
            schema.eid = rset[0][0]
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   351
        else:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   352
            assert rset
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   353
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   354
def erschema2rql(erschema, groupmap):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   355
    if isinstance(erschema, schemamod.EntitySchema):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   356
        return eschema2rql(erschema, groupmap=groupmap)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   357
    return rschema2rql(erschema, groupmap=groupmap)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   358
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   359
def specialize2rql(schema):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   360
    for eschema in schema.entities():
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   361
        if eschema.final:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   362
            continue
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   363
        for rql, kwargs in eschemaspecialize2rql(eschema):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   364
            yield rql, kwargs
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   365
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   366
# etype serialization
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   367
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   368
def eschema2rql(eschema, groupmap=None):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   369
    """return a list of rql insert statements to enter an entity schema
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   370
    in the database as an CWEType entity
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   371
    """
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   372
    relations, values = eschema_relations_values(eschema)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   373
    # NOTE: 'specializes' relation can't be inserted here since there's no
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   374
    # way to make sure the parent type is inserted before the child type
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   375
    yield 'INSERT CWEType X: %s' % ','.join(relations) , values
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   376
    # entity permissions
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   377
    if groupmap is not None:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   378
        for rql, args in _erperms2rql(eschema, groupmap):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   379
            yield rql, args
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   380
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   381
def eschema_relations_values(eschema):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   382
    values = _ervalues(eschema)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   383
    relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)]
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   384
    return relations, values
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   385
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   386
def eschemaspecialize2rql(eschema):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   387
    specialized_type = eschema.specializes()
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   388
    if specialized_type:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   389
        values = {'x': eschema.eid, 'et': specialized_type.eid}
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   390
        yield 'SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s', values
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   391
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
def _ervalues(erschema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
        type_ = unicode(erschema.type)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
    except UnicodeDecodeError, e:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
        raise Exception("can't decode %s [was %s]" % (erschema.type, e))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
        desc = unicode(erschema.description) or u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
    except UnicodeDecodeError, e:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        raise Exception("can't decode %s [was %s]" % (erschema.description, e))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
    return {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
        'name': type_,
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   403
        'final': erschema.final,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
        'description': desc,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
        }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   407
# rtype serialization
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   408
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   409
def rschema2rql(rschema, cstrtypemap=None, addrdef=True, groupmap=None):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   410
    """return a list of rql insert statements to enter a relation schema
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   411
    in the database as an CWRType entity
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   412
    """
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   413
    if rschema.type == 'has_text':
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   414
        return
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   415
    relations, values = rschema_relations_values(rschema)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   416
    yield 'INSERT CWRType X: %s' % ','.join(relations), values
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   417
    if addrdef:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   418
        assert cstrtypemap
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   419
        # sort for testing purpose
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   420
        for rdef in sorted(rschema.rdefs.itervalues(),
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   421
                           key=lambda x: (x.subject, x.object)):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   422
            for rql, values in rdef2rql(rdef, cstrtypemap, groupmap):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   423
                yield rql, values
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
def rschema_relations_values(rschema):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
    values = _ervalues(rschema)
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2955
diff changeset
   427
    values['final'] = rschema.final
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4312
diff changeset
   428
    values['symmetric'] = rschema.symmetric
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
    values['inlined'] = rschema.inlined
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4631
diff changeset
   430
    if isinstance(rschema.fulltext_container, str):
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4631
diff changeset
   431
        values['fulltext_container'] = unicode(rschema.fulltext_container)
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4631
diff changeset
   432
    else:
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4631
diff changeset
   433
        values['fulltext_container'] = rschema.fulltext_container
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   434
    relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
    return relations, values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   437
# rdef serialization
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   438
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   439
def rdef2rql(rdef, cstrtypemap, groupmap=None):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   440
    # don't serialize infered relations
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   441
    if rdef.infered:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   442
        return
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   443
    relations, values = _rdef_values(rdef)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   444
    relations.append('X relation_type ER,X from_entity SE,X to_entity OE')
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   445
    values.update({'se': rdef.subject.eid, 'rt': rdef.rtype.eid, 'oe': rdef.object.eid})
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   446
    if rdef.final:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   447
        etype = 'CWAttribute'
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   448
    else:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   449
        etype = 'CWRelation'
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   450
    yield 'INSERT %s X: %s WHERE SE eid %%(se)s,ER eid %%(rt)s,OE eid %%(oe)s' % (
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   451
        etype, ','.join(relations), ), values
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   452
    for rql, values in constraints2rql(cstrtypemap, rdef.constraints):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   453
        yield rql, values
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   454
    # no groupmap means "no security insertion"
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   455
    if groupmap:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   456
        for rql, args in _erperms2rql(rdef, groupmap):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   457
            yield rql, args
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   458
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   459
def _rdef_values(rdef):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   460
    amap = {'order': 'ordernum', 'default': 'defaultval'}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
    values = {}
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   462
    for prop, default in rdef.rproperty_defs(rdef.object).iteritems():
4003
b9436fe77c9e fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
   463
        if prop in ('eid', 'constraints', 'uid', 'infered', 'permissions'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
            continue
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   465
        value = getattr(rdef, prop)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   466
        # XXX type cast really necessary?
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
        if prop in ('indexed', 'fulltextindexed', 'internationalizable'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
            value = bool(value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
        elif prop == 'ordernum':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
            value = int(value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
        elif isinstance(value, str):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   472
            value = unicode(value)
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   473
        if value is not None and prop == 'default':
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   474
            if value is False:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   475
                value = u''
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   476
            if not isinstance(value, unicode):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   477
                value = unicode(value)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
        values[amap.get(prop, prop)] = value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
    relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
    return relations, values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   481
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   482
def constraints2rql(cstrtypemap, constraints, rdefeid=None):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   483
    for constraint in constraints:
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   484
        values = {'ct': cstrtypemap[constraint.type()],
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   485
                  'value': unicode(constraint.serialize()),
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   486
                  'x': rdefeid} # when not specified, will have to be set by the caller
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   487
        yield 'INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE \
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   488
CT eid %(ct)s, EDEF eid %(x)s', values
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4010
diff changeset
   491
def _erperms2rql(erschema, groupmap):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
    """return rql insert statements to enter the entity or relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   493
    schema's permissions in the database as
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   494
    [read|add|delete|update]_permission relations between CWEType/CWRType
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   495
    and CWGroup entities
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   497
    for action in erschema.ACTIONS:
4631
457160bc8cfe when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   498
        try:
457160bc8cfe when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   499
            grantedto = erschema.action_permissions(action)
457160bc8cfe when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   500
        except KeyError:
457160bc8cfe when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   501
            # may occurs when modifying persistent schema
457160bc8cfe when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   502
            continue
457160bc8cfe when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   503
        for group_or_rqlexpr in grantedto:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   504
            if isinstance(group_or_rqlexpr, basestring):
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   505
                # group
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   506
                try:
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   507
                    yield ('SET X %s_permission Y WHERE Y eid %%(g)s, X eid %%(x)s' % action,
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4010
diff changeset
   508
                           {'g': groupmap[group_or_rqlexpr]})
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   509
                except KeyError:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   510
                    continue
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   511
            else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   512
                # rqlexpr
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   513
                rqlexpr = group_or_rqlexpr
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   514
                yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, '
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   515
                       'E mainvars %%(v)s, X %s_permission E WHERE X eid %%(x)s' % action,
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   516
                       {'e': unicode(rqlexpr.expression),
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   517
                        'v': unicode(rqlexpr.mainvars),
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3876
diff changeset
   518
                        't': unicode(rqlexpr.__class__.__name__)})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   520
# update functions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   521
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   522
def updateeschema2rql(eschema, eid):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
    relations, values = eschema_relations_values(eschema)
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   524
    values['x'] = eid
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   525
    yield 'SET %s WHERE X eid %%(x)s' % ','.join(relations), values
1630
41aadba8b29f delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   526
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   527
def updaterschema2rql(rschema, eid):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   528
    relations, values = rschema_relations_values(rschema)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   529
    values['x'] = eid
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   530
    yield 'SET %s WHERE X eid %%(x)s' % ','.join(relations), values
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   531
4763
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   532
def updaterdef2rql(rdef, eid):
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   533
    relations, values = _rdef_values(rdef)
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   534
    values['x'] = eid
81b0df087375 schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4760
diff changeset
   535
    yield 'SET %s WHERE X eid %%(x)s' % ','.join(relations), values