cubicweb/hooks/syncschema.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 30 Sep 2016 18:25:08 +0200
changeset 11767 432f87a63057
parent 11765 9cb215e833b0
child 11774 51c160677afe
permissions -rw-r--r--
flake8 and all * update some copyright * drop most __docformat__ declaration * fix some flake8 warnings / errors
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
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: 4951
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""schema hooks:
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
- synchronize the living schema object with the persistent schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
- perform physical update on the source when necessary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
checking for schema consistency is done in hooks.py
5803
589e2e3fb997 [schema migration] don't try to drop column if backend doesn't support column alteration (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5801
diff changeset
    24
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
11004
14ba505fb652 [migration] consider extra_props in rdef created during attribute creation hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10921
diff changeset
    26
import json
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: 4722
diff changeset
    27
from copy import copy
11004
14ba505fb652 [migration] consider extra_props in rdef created during attribute creation hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10921
diff changeset
    28
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    29
from yams.schema import BASE_TYPES, BadSchemaDefinition, RelationDefinitionSchema
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
    30
from yams.constraints import UniqueConstraint
10200
cceb2c7c02f4 Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10074
diff changeset
    31
from yams import buildobjs as ybo, convert_default_value
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
3774
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
    33
from logilab.common.decorators import clear_cache
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
    34
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11765
diff changeset
    35
from cubicweb import _
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8534
diff changeset
    36
from cubicweb import validation_error
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8178
diff changeset
    37
from cubicweb.predicates import is_instance
6261
82d3ac90e47c [syncschema hooks] simplify core types definition be reusing schema sets (remove typos on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    38
from cubicweb.schema import (SCHEMA_TYPES, META_RTYPES, VIRTUAL_RTYPES,
10920
4b0443afbb3d [hooks/syncschema] drop unused import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10919
diff changeset
    39
                             CONSTRAINTS, UNIQUE_CONSTRAINTS, ETYPE_NAME_MAP)
10200
cceb2c7c02f4 Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10074
diff changeset
    40
from cubicweb.server import hook, schemaserial as ss, schema2sql as y2sql
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    41
from cubicweb.server.sqlutils import SQL_PREFIX
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    42
from cubicweb.hooks.synccomputed import RecomputeAttributeOperation
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    43
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
# core entity and relation types which can't be removed
6261
82d3ac90e47c [syncschema hooks] simplify core types definition be reusing schema sets (remove typos on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    45
CORE_TYPES = BASE_TYPES | SCHEMA_TYPES | META_RTYPES | set(
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
    46
    ('CWUser', 'CWGroup', 'login', 'upassword', 'name', 'in_group'))
6261
82d3ac90e47c [syncschema hooks] simplify core types definition be reusing schema sets (remove typos on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
    47
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    49
def get_constraints(cnx, entity):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    constraints = []
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    51
    for cstreid in cnx.transaction_data.get(entity.eid, ()):
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    52
        cstrent = cnx.entity_from_eid(cstreid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        cstr = CONSTRAINTS[cstrent.type].deserialize(cstrent.value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        cstr.eid = cstreid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        constraints.append(cstr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    return constraints
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    58
4043
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    59
def group_mapping(cw):
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    60
    try:
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    61
        return cw.transaction_data['groupmap']
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    62
    except KeyError:
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    63
        cw.transaction_data['groupmap'] = gmap = ss.group_mapping(cw)
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    64
        return gmap
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    65
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    66
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    67
def add_inline_relation_column(cnx, etype, rtype):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
    """add necessary column and index for an inlined relation"""
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
    69
    attrkey = '%s.%s' % (etype, rtype)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    70
    createdattrs = cnx.transaction_data.setdefault('createdattrs', set())
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
    71
    if attrkey in createdattrs:
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
    72
        return
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
    73
    createdattrs.add(attrkey)
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    74
    table = SQL_PREFIX + etype
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    75
    column = SQL_PREFIX + rtype
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    try:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
    77
        cnx.system_sql(str('ALTER TABLE %s ADD %s integer REFERENCES entities (eid)'
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
    78
                           % (table, column)),
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    79
                       rollback_on_failure=False)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    80
        cnx.info('added column %s to table %s', column, table)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7562
diff changeset
    81
    except Exception:
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    82
        # silent exception here, if this error has not been raised because the
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        # column already exists, index creation will fail anyway
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    84
        cnx.exception('error while adding column %s to table %s',
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    85
                      table, column)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    # create index before alter table which may expectingly fail during test
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    # (sqlite) while index creation should never fail (test for index existence
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    # is done by the dbhelper)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    89
    cnx.repo.system_source.create_index(cnx, table, column)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    90
    cnx.info('added index on %s(%s)', table, column)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    92
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    93
def insert_rdef_on_subclasses(cnx, eschema, rschema, rdefdef, props):
6076
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
    94
    # XXX 'infered': True/False, not clear actually
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
    95
    props.update({'constraints': rdefdef.constraints,
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
    96
                  'description': rdefdef.description,
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
    97
                  'cardinality': rdefdef.cardinality,
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
    98
                  'permissions': rdefdef.get_permissions(),
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
    99
                  'order': rdefdef.order,
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   100
                  'infered': False, 'eid': None
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   101
                  })
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   102
    cstrtypemap = ss.cstrtype_mapping(cnx)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   103
    groupmap = group_mapping(cnx)
6076
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   104
    object = rschema.schema.eschema(rdefdef.object)
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   105
    for specialization in eschema.specialized_by(False):
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   106
        if (specialization, rdefdef.object) in rschema.rdefs:
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   107
            continue
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   108
        sperdef = RelationDefinitionSchema(specialization, rschema,
9365
71c12e778162 [yams] follow yams 0.39 api change introduced by ad95fd2c46de (package attribute on etype/rtype/rdef)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9299
diff changeset
   109
                                           object, None, values=props)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   110
        ss.execschemarql(cnx.execute, sperdef,
6076
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   111
                         ss.rdef2rql(sperdef, cstrtypemap, groupmap))
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   112
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   113
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   114
def check_valid_changes(cnx, entity, ro_attrs=('name', 'final')):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   115
    errors = {}
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   116
    # don't use getattr(entity, attr), we would get the modified value if any
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
   117
    for attr in entity.cw_edited:
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   118
        if attr in ro_attrs:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
   119
            origval, newval = entity.cw_edited.oldnewvalue(attr)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   120
            if newval != origval:
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8534
diff changeset
   121
                errors[attr] = _("can't change this attribute")
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   122
    if errors:
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8534
diff changeset
   123
        raise validation_error(entity, errors)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   124
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   126
class _MockEntity(object):  # XXX use a named tuple with python 2.6
6789
f38963f7c91d [schema hook] don't depends on lgc.testlib, hence unittest2 for a dumb mock
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   127
    def __init__(self, eid):
f38963f7c91d [schema hook] don't depends on lgc.testlib, hence unittest2 for a dumb mock
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   128
        self.eid = eid
f38963f7c91d [schema hook] don't depends on lgc.testlib, hence unittest2 for a dumb mock
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   129
f38963f7c91d [schema hook] don't depends on lgc.testlib, hence unittest2 for a dumb mock
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   130
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   131
class SyncSchemaHook(hook.Hook):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   132
    """abstract class for schema synchronization hooks (in the `syncschema`
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   133
    category)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   134
    """
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   135
    __abstract__ = True
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   136
    category = 'syncschema'
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   137
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   138
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   139
# operations for low-level database alteration  ################################
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   140
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   141
class DropTable(hook.Operation):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2462
diff changeset
   142
    """actually remove a database from the instance's schema"""
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   143
    table = None  # make pylint happy
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   144
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    def precommit_event(self):
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   146
        dropped = self.cnx.transaction_data.setdefault('droppedtables', set())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        if self.table in dropped:
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   148
            return  # already processed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        dropped.add(self.table)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   150
        self.cnx.system_sql('DROP TABLE %s' % self.table)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        self.info('dropped table %s', self.table)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   152
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   153
    # XXX revertprecommit_event
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   154
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   155
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   156
class DropRelationTable(DropTable):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   157
    def __init__(self, cnx, rtype):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   158
        super(DropRelationTable, self).__init__(
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   159
            cnx, table='%s_relation' % rtype)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   160
        cnx.transaction_data.setdefault('pendingrtypes', set()).add(rtype)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   161
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   162
10916
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   163
class DropColumn(hook.DataOperationMixIn, hook.Operation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    """actually remove the attribut's column from entity table in the system
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
    database
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    def precommit_event(self):
10916
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   168
        cnx = self.cnx
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   169
        for etype, attr in self.get_data():
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   170
            table = SQL_PREFIX + etype
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   171
            column = SQL_PREFIX + attr
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   172
            source = cnx.repo.system_source
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   173
            # drop index if any
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   174
            source.drop_index(cnx, table, column)
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   175
            if source.dbhelper.alter_column_support:
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   176
                cnx.system_sql('ALTER TABLE %s DROP COLUMN %s' % (table, column),
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   177
                               rollback_on_failure=False)
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   178
                self.info('dropped column %s from table %s', column, table)
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   179
            else:
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   180
                # not supported by sqlite for instance
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   181
                self.error('dropping column not supported by the backend, handle '
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   182
                           'it yourself (%s.%s)', table, column)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   183
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   184
    # XXX revertprecommit_event
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   185
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   187
# base operations for in-memory schema synchronization  ########################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   189
class MemSchemaNotifyChanges(hook.SingleLastOperation):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   190
    """the update schema operation:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   192
    special operation which should be called once and after all other schema
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   193
    operations. It will trigger internal structures rebuilding to consider
2902
dd9f2dd02f85 remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2899
diff changeset
   194
    schema changes.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   197
    def __init__(self, cnx):
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   198
        hook.SingleLastOperation.__init__(self, cnx)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   199
3774
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   200
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   201
        for eschema in self.cnx.repo.schema.entities():
3774
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   202
            if not eschema.final:
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   203
                clear_cache(eschema, 'ordered_relations')
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   204
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   205
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   206
        repo = self.cnx.repo
4591
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   207
        # commit event should not raise error, while set_schema has chances to
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   208
        # do so because it triggers full vreg reloading
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   209
        try:
9588
fe267b7336f3 [migration] always rebuild infered relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9375
diff changeset
   210
            repo.schema.rebuild_infered_relations()
8943
58b3b2d9c965 [repo] kill rebuildinfered feature from Repository.set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8821
diff changeset
   211
            # trigger vreg reload
58b3b2d9c965 [repo] kill rebuildinfered feature from Repository.set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8821
diff changeset
   212
            repo.set_schema(repo.schema)
4591
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   213
            # CWUser class might have changed, update current session users
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   214
            cwuser_cls = self.cnx.vreg['etypes'].etype_class('CWUser')
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10646
diff changeset
   215
            for session in repo._sessions.values():
4591
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   216
                session.user.__class__ = cwuser_cls
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7562
diff changeset
   217
        except Exception:
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7562
diff changeset
   218
            self.critical('error while setting schema', exc_info=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
3774
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   220
    def rollback_event(self):
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   221
        self.precommit_event()
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   222
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   223
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   224
class MemSchemaOperation(hook.Operation):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   225
    """base class for schema operations"""
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   226
    def __init__(self, cnx, **kwargs):
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   227
        hook.Operation.__init__(self, cnx, **kwargs)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   228
        # every schema operation is triggering a schema update
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   229
        MemSchemaNotifyChanges(cnx)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   231
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   232
# operations for high-level source database alteration  ########################
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   233
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   234
class CWETypeAddOp(MemSchemaOperation):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   235
    """after adding a CWEType entity:
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   236
    * add it to the instance's schema
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   237
    * create the necessary table
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   238
    * set creation_date and modification_date by creating the necessary
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   239
      CWAttribute entities
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   240
    * add <meta rtype> relation by creating the necessary CWRelation entity
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   241
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   242
    entity = None  # make pylint happy
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   243
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   244
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   245
        cnx = self.cnx
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   246
        entity = self.entity
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   247
        schema = cnx.vreg.schema
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   248
        etype = ybo.EntityType(eid=entity.eid, name=entity.name,
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   249
                               description=entity.description)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   250
        eschema = schema.add_entity_type(etype)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   251
        # create the necessary table
11413
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   252
        for sql in y2sql.eschema2sql(cnx.repo.system_source.dbhelper,
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   253
                                     eschema, prefix=SQL_PREFIX):
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   254
            cnx.system_sql(sql)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   255
        # add meta relations
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   256
        gmap = group_mapping(cnx)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   257
        cmap = ss.cstrtype_mapping(cnx)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   258
        for rtype in (META_RTYPES - VIRTUAL_RTYPES):
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   259
            try:
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   260
                rschema = schema[rtype]
7551
2d4ba5b984dc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7504
diff changeset
   261
            except KeyError:
8192
0067653190c7 [hooks/syncschema] do not crash if the schema did not learn (yet) some new META rtype (closes #2171313)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8178
diff changeset
   262
                self.critical('rtype %s was not handled at cwetype creation time', rtype)
0067653190c7 [hooks/syncschema] do not crash if the schema did not learn (yet) some new META rtype (closes #2171313)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8178
diff changeset
   263
                continue
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   264
            if not rschema.rdefs:
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   265
                self.warning('rtype %s has no relation definition yet', rtype)
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   266
                continue
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   267
            sampletype = rschema.subjects()[0]
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   268
            desttype = rschema.objects()[0]
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   269
            try:
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   270
                rdef = copy(rschema.rdef(sampletype, desttype))
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   271
            except KeyError:
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   272
                # this combo does not exist because this is not a universal META_RTYPE
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8534
diff changeset
   273
                continue
6789
f38963f7c91d [schema hook] don't depends on lgc.testlib, hence unittest2 for a dumb mock
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   274
            rdef.subject = _MockEntity(eid=entity.eid)
f38963f7c91d [schema hook] don't depends on lgc.testlib, hence unittest2 for a dumb mock
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   275
            mock = _MockEntity(eid=None)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   276
            ss.execschemarql(cnx.execute, mock, ss.rdef2rql(rdef, cmap, gmap))
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   277
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   278
    def revertprecommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   279
        # revert changes on in memory schema
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   280
        self.cnx.vreg.schema.del_entity_type(self.entity.name)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   281
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   282
        self.cnx.system_sql('DROP TABLE %s%s' % (SQL_PREFIX, self.entity.name))
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   283
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   284
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   285
class CWETypeRenameOp(MemSchemaOperation):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   286
    """this operation updates physical storage accordingly"""
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   287
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   288
    oldname = newname = None  # make pylint happy
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   290
    def rename(self, oldname, newname):
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   291
        cnx = self.cnx
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   292
        source = cnx.repo.system_source
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   293
        dbhelper = source.dbhelper
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   294
        # we need sql to operate physical changes on the system database
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   295
        sqlexec = cnx.system_sql
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   296
        cnx.vreg.schema.rename_entity_type(oldname, newname)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   297
        old_table = SQL_PREFIX + oldname
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   298
        new_table = SQL_PREFIX + newname
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   299
        eschema = cnx.vreg.schema.eschema(newname)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   300
        # drop old indexes before the renaming
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   301
        for rschema in eschema.subject_relations():
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   302
            if rschema.inlined or (rschema.final and eschema.rdef(rschema.type).indexed):
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   303
                source.drop_index(cnx, old_table, SQL_PREFIX + rschema.type)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   304
            if rschema.final and any(isinstance(cstr, UniqueConstraint)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   305
                                     for cstr in eschema.rdef(rschema.type).constraints):
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   306
                source.drop_index(cnx, old_table, SQL_PREFIX + rschema.type, unique=True)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   307
        sql = dbhelper.sql_rename_table(old_table, new_table)
6734
ec9a5efdc451 make rename_table work with MS SQL (needs updated logilab.database)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6294
diff changeset
   308
        sqlexec(sql)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   309
        self.info('renamed table %s to %s', oldname, newname)
6734
ec9a5efdc451 make rename_table work with MS SQL (needs updated logilab.database)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6294
diff changeset
   310
        sqlexec('UPDATE entities SET type=%(newname)s WHERE type=%(oldname)s',
6735
7d37ba09f4e3 brown paperbag fix
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6734
diff changeset
   311
                {'newname': newname, 'oldname': oldname})
11763
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11417
diff changeset
   312
        for eid, (etype, extid) in cnx.repo._type_extid_cache.items():
8105
7980b36fb1aa [schema sync] Update repo._type_source_cache when renaming an entity (closes #2094470)
Julien Cristau <julien.cristau@logilab.fr>
parents: 8097
diff changeset
   313
            if etype == oldname:
11763
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11417
diff changeset
   314
                cnx.repo._type_extid_cache[eid] = (newname, extid)
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   315
        # recreate the indexes
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   316
        for rschema in eschema.subject_relations():
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   317
            if rschema.inlined or (rschema.final and eschema.rdef(rschema.type).indexed):
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   318
                source.create_index(cnx, new_table, SQL_PREFIX + rschema.type)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   319
            if rschema.final and any(isinstance(cstr, UniqueConstraint)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   320
                                     for cstr in eschema.rdef(rschema.type).constraints):
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   321
                source.create_index(cnx, new_table, SQL_PREFIX + rschema.type, unique=True)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   322
        for attrs in eschema._unique_together or ():
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   323
            columns = ['%s%s' % (SQL_PREFIX, attr) for attr in attrs]
11411
9a5a6ec5fc09 [hooks] Use already imported module alias
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11406
diff changeset
   324
            old_index_name = y2sql.unique_index_name(oldname, columns)
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   325
            for sql in dbhelper.sqls_drop_multicol_unique_index(
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   326
                    new_table, columns, old_index_name):
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   327
                sqlexec(sql)
11411
9a5a6ec5fc09 [hooks] Use already imported module alias
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11406
diff changeset
   328
            new_index_name = y2sql.unique_index_name(newname, columns)
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   329
            for sql in dbhelper.sqls_create_multicol_unique_index(
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   330
                    new_table, columns, new_index_name):
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   331
                sqlexec(sql)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   332
        # XXX transaction records
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   333
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   334
    def precommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   335
        self.rename(self.oldname, self.newname)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   336
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   337
    def revertprecommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   338
        self.rename(self.newname, self.oldname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   341
class CWRTypeUpdateOp(MemSchemaOperation):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   342
    """actually update some properties of a relation definition"""
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   343
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   344
    rschema = entity = values = None  # make pylint happy
9949
e0f1c5697221 fix typo in syncschema hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9888
diff changeset
   345
    oldvalues = None
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   346
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   347
    def precommit_event(self):
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
   348
        rschema = self.rschema
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
   349
        if rschema.final:
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   350
            return  # watched changes to final relation type are unexpected
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   351
        cnx = self.cnx
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
   352
        if 'fulltext_container' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   353
            op = UpdateFTIndexOp.get_instance(cnx)
4826
7eba168407c3 [schema hook] fulltext_container is a relation *type* property. Test and fix (+ more fti properties change testing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4807
diff changeset
   354
            for subjtype, objtype in rschema.rdefs:
10250
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   355
                if self.values['fulltext_container'] == 'subject':
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   356
                    op.add_data(subjtype)
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   357
                    op.add_data(objtype)
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   358
                else:
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   359
                    op.add_data(objtype)
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   360
                    op.add_data(subjtype)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   361
        # update the in-memory schema first
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   362
        self.oldvalues = dict((attr, getattr(rschema, attr)) for attr in self.values)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   363
        self.rschema.__dict__.update(self.values)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   364
        # then make necessary changes to the system source database
10921
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   365
        if 'inlined' not in self.values:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   366
            return  # nothing to do
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
   367
        inlined = self.values['inlined']
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   368
        # check in-lining is possible when inlined
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
   369
        if inlined:
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
   370
            self.entity.check_inlined_allowed()
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   371
        # inlined changed, make necessary physical changes!
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   372
        sqlexec = self.cnx.system_sql
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   373
        rtype = rschema.type
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   374
        eidcolumn = SQL_PREFIX + 'eid'
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   375
        if not inlined:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   376
            # need to create the relation if it has not been already done by
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   377
            # another event of the same transaction
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   378
            if rschema.type not in cnx.transaction_data.get('createdtables', ()):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   379
                # create the necessary table
11413
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   380
                for sql in y2sql.rschema2sql(rschema):
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   381
                    sqlexec(sql)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   382
                cnx.transaction_data.setdefault('createdtables', []).append(
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   383
                    rschema.type)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   384
            # copy existant data
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   385
            column = SQL_PREFIX + rtype
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   386
            for etype in rschema.subjects():
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   387
                table = SQL_PREFIX + str(etype)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   388
                sqlexec('INSERT INTO %s_relation SELECT %s, %s FROM %s WHERE NOT %s IS NULL'
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   389
                        % (rtype, eidcolumn, column, table, column))
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   390
            # drop existant columns
5805
560cde8a4f9f [schema migration] fix fix introduced in 5803:589e2e3fb997, test for backend alter column support should be done in the operation, else the associated index is not dropped while this is supported
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5804
diff changeset
   391
            for etype in rschema.subjects():
10916
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   392
                DropColumn.get_instance(cnx).add_data((str(etype), rtype))
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   393
        else:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   394
            for etype in rschema.subjects():
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   395
                try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   396
                    add_inline_relation_column(cnx, str(etype), rtype)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8556
diff changeset
   397
                except Exception as ex:
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   398
                    # the column probably already exists. this occurs when the
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   399
                    # entity's type has just been added or if the column has not
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   400
                    # been previously dropped (eg sqlite)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   401
                    self.error('error while altering table %s: %s', etype, ex)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   402
                # copy existant data.
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   403
                # XXX don't use, it's not supported by sqlite (at least at when i tried it)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   404
                #sqlexec('UPDATE %(etype)s SET %(rtype)s=eid_to '
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   405
                #        'FROM %(rtype)s_relation '
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   406
                #        'WHERE %(etype)s.eid=%(rtype)s_relation.eid_from'
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   407
                #        % locals())
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   408
                table = SQL_PREFIX + str(etype)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   409
                cursor = sqlexec('SELECT eid_from, eid_to FROM %(table)s, '
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   410
                                 '%(rtype)s_relation WHERE %(table)s.%(eidcolumn)s='
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   411
                                 '%(rtype)s_relation.eid_from' % locals())
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   412
                args = [{'val': eid_to, 'x': eid} for eid, eid_to in cursor.fetchall()]
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   413
                if args:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   414
                    column = SQL_PREFIX + rtype
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   415
                    cursor.executemany('UPDATE %s SET %s=%%(val)s WHERE %s=%%(x)s'
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   416
                                       % (table, column, eidcolumn), args)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   417
                # drop existant table
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   418
                DropRelationTable(cnx, rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   420
    def revertprecommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   421
        # revert changes on in memory schema
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   422
        self.rschema.__dict__.update(self.oldvalues)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   423
        # XXX revert changes on database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   425
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   426
class CWComputedRTypeUpdateOp(MemSchemaOperation):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   427
    """actually update some properties of a computed relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   428
    rschema = entity = rule = None  # make pylint happy
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   429
    old_rule = None
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   430
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   431
    def precommit_event(self):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   432
        # update the in-memory schema first
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   433
        self.old_rule = self.rschema.rule
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   434
        self.rschema.rule = self.rule
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   435
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   436
    def revertprecommit_event(self):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   437
        # revert changes on in memory schema
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   438
        self.rschema.rule = self.old_rule
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   439
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   440
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   441
class CWAttributeAddOp(MemSchemaOperation):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   442
    """an attribute relation (CWAttribute) has been added:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
    * add the necessary column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   444
    * set default on this column if any and possible
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   445
    * register an operation to add the relation definition to the
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2462
diff changeset
   446
      instance's schema on commit
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   447
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
    constraints are handled by specific hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   450
    entity = None  # make pylint happy
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   451
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   452
    def init_rdef(self, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
        entity = self.entity
2599
79bd12769c55 [R schema hooks] use new accessors on CWRelation and CWAttribute entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
   454
        fromentity = entity.stype
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   455
        rdefdef = self.rdefdef = ybo.RelationDefinition(
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   456
            str(fromentity.name), entity.rtype.name, str(entity.otype.name),
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   457
            description=entity.description, cardinality=entity.cardinality,
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   458
            constraints=get_constraints(self.cnx, entity),
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   459
            order=entity.ordernum, eid=entity.eid, **kwargs)
9950
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   460
        try:
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   461
            self.cnx.vreg.schema.add_relation_def(rdefdef)
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   462
        except BadSchemaDefinition:
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   463
            # rdef has been infered then explicitly added (current consensus is
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   464
            # not clear at all versus infered relation handling (and much
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   465
            # probably buggy)
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   466
            rdef = self.cnx.vreg.schema.rschema(rdefdef.name).rdefs[rdefdef.subject, rdefdef.object]
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   467
            assert rdef.infered
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   468
        else:
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   469
            rdef = self.cnx.vreg.schema.rschema(rdefdef.name).rdefs[rdefdef.subject, rdefdef.object]
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   470
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   471
        self.cnx.execute('SET X ordernum Y+1 '
9950
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   472
                         'WHERE X from_entity SE, SE eid %(se)s, X ordernum Y, '
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   473
                         'X ordernum >= %(order)s, NOT X eid %(x)s',
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   474
                         {'x': entity.eid, 'se': fromentity.eid,
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   475
                          'order': entity.ordernum or 0})
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   476
        return rdefdef, rdef
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   477
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   478
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   479
        cnx = self.cnx
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   480
        entity = self.entity
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   481
        # entity.defaultval is a Binary or None, but we need a correctly typed
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   482
        # value
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
        default = entity.defaultval
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
        if default is not None:
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   485
            default = default.unzpickle()
3526
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   486
        props = {'default': default,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   487
                 'indexed': entity.indexed,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   488
                 'fulltextindexed': entity.fulltextindexed,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   489
                 'internationalizable': entity.internationalizable}
11004
14ba505fb652 [migration] consider extra_props in rdef created during attribute creation hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10921
diff changeset
   490
        if entity.extra_props:
11038
7cb02ab4f321 [hooks] fix extra_props handling on python3
Julien Cristau <julien.cristau@logilab.fr>
parents: 11005
diff changeset
   491
            props.update(json.loads(entity.extra_props.getvalue().decode('ascii')))
10025
7b72ecc3f4d2 [migration] hackish black magic to bootstrap addition of formula attr during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9971
diff changeset
   492
        # entity.formula may not exist yet if we're migrating to 3.20
7b72ecc3f4d2 [migration] hackish black magic to bootstrap addition of formula attr during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9971
diff changeset
   493
        if hasattr(entity, 'formula'):
7b72ecc3f4d2 [migration] hackish black magic to bootstrap addition of formula attr during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9971
diff changeset
   494
            props['formula'] = entity.formula
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   495
        # update the in-memory schema first
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   496
        rdefdef, rdef = self.init_rdef(**props)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   497
        # then make necessary changes to the system source database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   498
        syssource = cnx.repo.system_source
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   499
        attrtype = y2sql.type_from_rdef(syssource.dbhelper, rdef)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
        # added some str() wrapping query since some backend (eg psycopg) don't
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   501
        # allow unicode queries
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   502
        table = SQL_PREFIX + rdefdef.subject
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   503
        column = SQL_PREFIX + rdefdef.name
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   505
            cnx.system_sql(str('ALTER TABLE %s ADD %s %s'
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   506
                               % (table, column, attrtype)),
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   507
                           rollback_on_failure=False)
10035
c5bcdece77fb [hooks] Fix precommit event logging message
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9888
diff changeset
   508
            self.info('added column %s to table %s', column, table)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8556
diff changeset
   509
        except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
            # the column probably already exists. this occurs when
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
            # the entity's type has just been added or if the column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
            # has not been previously dropped
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   513
            self.error('error while altering table %s: %s', table, ex)
11359
2da2dd60331c [sql gen] Explicitly name unique index
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11291
diff changeset
   514
        if entity.indexed:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
            try:
11359
2da2dd60331c [sql gen] Explicitly name unique index
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11291
diff changeset
   516
                syssource.create_index(cnx, table, column, unique=False)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8556
diff changeset
   517
            except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
                self.error('error while creating index for %s.%s: %s',
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   519
                           table, column, ex)
3526
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   520
        # final relations are not infered, propagate
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   521
        schema = cnx.vreg.schema
3531
c095f5f54873 [migration] take care to entity being added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3526
diff changeset
   522
        try:
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   523
            eschema = schema.eschema(rdefdef.subject)
3531
c095f5f54873 [migration] take care to entity being added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3526
diff changeset
   524
        except KeyError:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   525
            return  # entity type currently being added
3543
ed152fe5aa8b [migration] when adding a new attribute with a default value, set this value on existing entities (test and fix)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3531
diff changeset
   526
        # propagate attribute to children classes
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   527
        rschema = schema.rschema(rdefdef.name)
3550
f9bdcfeb12ee [migration] fix bug when propagating newly inserted relation type to children classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3546
diff changeset
   528
        # if relation type has been inserted in the same transaction, its final
f9bdcfeb12ee [migration] fix bug when propagating newly inserted relation type to children classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3546
diff changeset
   529
        # attribute is still set to False, so we've to ensure it's False
f9bdcfeb12ee [migration] fix bug when propagating newly inserted relation type to children classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3546
diff changeset
   530
        rschema.final = True
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   531
        insert_rdef_on_subclasses(cnx, eschema, rschema, rdefdef, props)
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   532
        # update existing entities with the default value of newly added attribute
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   533
        if default is not None:
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   534
            default = convert_default_value(self.rdefdef, default)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   535
            cnx.system_sql('UPDATE %s SET %s=%%(default)s' % (table, column),
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   536
                           {'default': default})
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   537
        # if attribute is computed, compute it
10025
7b72ecc3f4d2 [migration] hackish black magic to bootstrap addition of formula attr during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9971
diff changeset
   538
        if getattr(entity, 'formula', None):
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   539
            # add rtype attribute for RelationDefinitionSchema api compat, this
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   540
            # is what RecomputeAttributeOperation expect
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   541
            rdefdef.rtype = rdefdef.name
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
   542
            RecomputeAttributeOperation.get_instance(cnx).add_data(rdefdef)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   543
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   544
    def revertprecommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   545
        # revert changes on in memory schema
8818
d8b0984c923c [migration] fix bug in `CWAttributeAddOp.revertprecommit_event`
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8715
diff changeset
   546
        if getattr(self, 'rdefdef', None) is None:
d8b0984c923c [migration] fix bug in `CWAttributeAddOp.revertprecommit_event`
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8715
diff changeset
   547
            return
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   548
        self.cnx.vreg.schema.del_relation_def(
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   549
            self.rdefdef.subject, self.rdefdef.name, self.rdefdef.object)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   550
        # XXX revert changes on database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   552
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   553
class CWRelationAddOp(CWAttributeAddOp):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
    """an actual relation has been added:
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   555
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   556
    * add the relation definition to the instance's schema
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   557
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   558
    * if this is an inlined relation, add the necessary column else if it's the
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   559
      first instance of this relation type, add the necessary table and set
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   560
      default permissions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
    constraints are handled by specific hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   563
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   564
    entity = None  # make pylint happy
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   565
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   567
        cnx = self.cnx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
        entity = self.entity
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   569
        # update the in-memory schema first
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   570
        rdefdef, rdef = self.init_rdef(composite=entity.composite)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   571
        # then make necessary changes to the system source database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   572
        schema = cnx.vreg.schema
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   573
        rtype = rdefdef.name
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   574
        rschema = schema.rschema(rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   575
        # this have to be done before permissions setting
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
        if rschema.inlined:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   577
            # need to add a column if the relation is inlined and if this is the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
            # first occurence of "Subject relation Something" whatever Something
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   579
            if len(rschema.objects(rdefdef.subject)) == 1:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   580
                add_inline_relation_column(cnx, rdefdef.subject, rtype)
6076
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   581
            eschema = schema[rdefdef.subject]
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   582
            insert_rdef_on_subclasses(cnx, eschema, rschema, rdefdef,
6076
aa7402e998a1 [migration] when some inlined relation definition is added to an entity type which has some specialized types, it should be propagated to those types else they may miss the necessary table column to store the relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6002
diff changeset
   583
                                      {'composite': entity.composite})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
        else:
6163
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   585
            if rschema.symmetric:
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   586
                # for symmetric relations, rdefs will store relation definitions
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   587
                # in both ways (i.e. (subj -> obj) and (obj -> subj))
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   588
                relation_already_defined = len(rschema.rdefs) > 2
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   589
            else:
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   590
                relation_already_defined = len(rschema.rdefs) > 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   591
            # need to create the relation if no relation definition in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   592
            # schema and if it has not been added during other event of the same
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   593
            # transaction
6163
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   594
            if not (relation_already_defined or
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   595
                    rtype in cnx.transaction_data.get('createdtables', ())):
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   596
                rschema = schema.rschema(rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
                # create the necessary table
11413
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   598
                for sql in y2sql.rschema2sql(rschema):
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   599
                    cnx.system_sql(sql)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   600
                cnx.transaction_data.setdefault('createdtables', []).append(
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2056
diff changeset
   601
                    rtype)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   602
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   603
    # XXX revertprecommit_event
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   604
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   605
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   606
class RDefDelOp(MemSchemaOperation):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   607
    """an actual relation has been removed"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   608
    rdef = None  # make pylint happy
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   609
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   610
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   611
        cnx = self.cnx
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   612
        rdef = self.rdef
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   613
        rschema = rdef.rtype
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   614
        # make necessary changes to the system source database first
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   615
        rdeftype = rschema.final and 'CWAttribute' or 'CWRelation'
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   616
        execute = cnx.execute
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   617
        rset = execute('Any COUNT(X) WHERE X is %s, X relation_type R,'
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   618
                       'R eid %%(x)s' % rdeftype, {'x': rschema.eid})
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   619
        lastrel = rset[0][0] == 0
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   620
        # we have to update physical schema systematically for final and inlined
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   621
        # relations, but only if it's the last instance for this relation type
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   622
        # for other relations
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   623
        if (rschema.final or rschema.inlined):
11106
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   624
            if not cnx.deleted_in_transaction(rdef.subject.eid):
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   625
                rset = execute('Any COUNT(X) WHERE X is %s, X relation_type R, '
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   626
                               'R eid %%(r)s, X from_entity E, E eid %%(e)s'
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   627
                               % rdeftype,
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   628
                               {'r': rschema.eid, 'e': rdef.subject.eid})
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   629
                if rset[0][0] == 0:
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   630
                    ptypes = cnx.transaction_data.setdefault('pendingrtypes', set())
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   631
                    ptypes.add(rschema.type)
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   632
                    DropColumn.get_instance(cnx).add_data((str(rdef.subject), str(rschema)))
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   633
                elif rschema.inlined:
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   634
                    cnx.system_sql('UPDATE %s%s SET %s%s=NULL WHERE '
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   635
                                   'EXISTS(SELECT 1 FROM entities '
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   636
                                   '       WHERE eid=%s%s AND type=%%(to_etype)s)'
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   637
                                   % (SQL_PREFIX, rdef.subject, SQL_PREFIX, rdef.rtype,
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   638
                                      SQL_PREFIX, rdef.rtype),
012e6c7d02ef [migration] test if entity type has been in the deleted in the transaction should protect both clauses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11038
diff changeset
   639
                                   {'to_etype': rdef.object.type})
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   640
        elif lastrel:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   641
            DropRelationTable(cnx, str(rschema))
10921
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   642
        else:
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   643
            cnx.system_sql('DELETE FROM %s_relation WHERE '
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   644
                           'EXISTS(SELECT 1 FROM entities '
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   645
                           '       WHERE eid=eid_from AND type=%%(from_etype)s)'
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   646
                           ' AND EXISTS(SELECT 1 FROM entities '
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   647
                           '       WHERE eid=eid_to AND type=%%(to_etype)s)'
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   648
                           % rschema,
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   649
                           {'from_etype': rdef.subject.type, 'to_etype': rdef.object.type})
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   650
        # then update the in-memory schema
7504
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   651
        if rdef.subject not in ETYPE_NAME_MAP and rdef.object not in ETYPE_NAME_MAP:
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   652
            rschema.del_relation_def(rdef.subject, rdef.object)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   653
        # if this is the last relation definition of this type, drop associated
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   654
        # relation type
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   655
        if lastrel and not cnx.deleted_in_transaction(rschema.eid):
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   656
            execute('DELETE CWRType X WHERE X eid %(x)s', {'x': rschema.eid})
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   657
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   658
    def revertprecommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   659
        # revert changes on in memory schema
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   660
        #
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   661
        # Note: add_relation_def takes a RelationDefinition, not a
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   662
        # RelationDefinitionSchema, needs to fake it
7504
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   663
        rdef = self.rdef
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   664
        rdef.name = str(rdef.rtype)
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   665
        if rdef.subject not in ETYPE_NAME_MAP and rdef.object not in ETYPE_NAME_MAP:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   666
            self.cnx.vreg.schema.add_relation_def(rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   667
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   668
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   669
class RDefUpdateOp(MemSchemaOperation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   670
    """actually update some properties of a relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   671
    rschema = rdefkey = values = None  # make pylint happy
6002
0ce7052ce30b [schema sync] if something went wrong while getting rdef in precommit event, rdef adttribute won't be set in revertprecommit. We don't want to fail in such case
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5891
diff changeset
   672
    rdef = oldvalues = None
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   673
    indexed_changed = null_allowed_changed = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   674
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   675
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   676
        cnx = self.cnx
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   677
        rdef = self.rdef = self.rschema.rdefs[self.rdefkey]
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   678
        # update the in-memory schema first
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   679
        self.oldvalues = dict((attr, getattr(rdef, attr)) for attr in self.values)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   680
        rdef.update(self.values)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   681
        # then make necessary changes to the system source database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   682
        syssource = cnx.repo.system_source
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
        if 'indexed' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   684
            syssource.update_rdef_indexed(cnx, rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   685
            self.indexed_changed = True
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   686
        if ('cardinality' in self.values and rdef.rtype.final
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   687
                and self.values['cardinality'][0] != self.oldvalues['cardinality'][0]):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   688
            syssource.update_rdef_null_allowed(self.cnx, rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   689
            self.null_allowed_changed = True
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
   690
        if 'fulltextindexed' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   691
            UpdateFTIndexOp.get_instance(cnx).add_data(rdef.subject)
9971
5e44dd9dde6b [CWEP002 migration] support sync_schema_props_perms for computed attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9969
diff changeset
   692
        if 'formula' in self.values:
5e44dd9dde6b [CWEP002 migration] support sync_schema_props_perms for computed attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9969
diff changeset
   693
            RecomputeAttributeOperation.get_instance(cnx).add_data(rdef)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   694
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   695
    def revertprecommit_event(self):
6002
0ce7052ce30b [schema sync] if something went wrong while getting rdef in precommit event, rdef adttribute won't be set in revertprecommit. We don't want to fail in such case
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5891
diff changeset
   696
        if self.rdef is None:
0ce7052ce30b [schema sync] if something went wrong while getting rdef in precommit event, rdef adttribute won't be set in revertprecommit. We don't want to fail in such case
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5891
diff changeset
   697
            return
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   698
        # revert changes on in memory schema
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   699
        self.rdef.update(self.oldvalues)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   700
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   701
        syssource = self.cnx.repo.system_source
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   702
        if self.indexed_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   703
            syssource.update_rdef_indexed(self.cnx, self.rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   704
        if self.null_allowed_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   705
            syssource.update_rdef_null_allowed(self.cnx, self.rdef)
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   706
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   707
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   708
def _set_modifiable_constraints(rdef):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   709
    # for proper in-place modification of in-memory schema: if rdef.constraints
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   710
    # is already a list, reuse it (we're updating multiple constraints of the
9636
e35ae8617c03 Fix constraint sync during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   711
    # same rdef in the same transaction)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   712
    if not isinstance(rdef.constraints, list):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   713
        rdef.constraints = list(rdef.constraints)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   714
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   715
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   716
class CWConstraintDelOp(MemSchemaOperation):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   717
    """actually remove a constraint of a relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   718
    rdef = oldcstr = newcstr = None  # make pylint happy
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   719
    size_cstr_changed = unique_changed = False
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   720
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   721
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   722
        cnx = self.cnx
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   723
        rdef = self.rdef
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   724
        # in-place modification of in-memory schema first
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   725
        _set_modifiable_constraints(rdef)
9559
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   726
        if self.oldcstr in rdef.constraints:
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   727
            rdef.constraints.remove(self.oldcstr)
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   728
        else:
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   729
            self.critical('constraint %s for rdef %s was missing or already removed',
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   730
                          self.oldcstr, rdef)
9888
5b48dcc02be1 [hooks/syncschema] avoid altering a dropped table
Julien Cristau <julien.cristau@logilab.fr>
parents: 9636
diff changeset
   731
        if cnx.deleted_in_transaction(rdef.eid):
5b48dcc02be1 [hooks/syncschema] avoid altering a dropped table
Julien Cristau <julien.cristau@logilab.fr>
parents: 9636
diff changeset
   732
            # don't try to alter a table that's going away (or is already gone)
5b48dcc02be1 [hooks/syncschema] avoid altering a dropped table
Julien Cristau <julien.cristau@logilab.fr>
parents: 9636
diff changeset
   733
            return
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   734
        # then update database: alter the physical schema on size/unique
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   735
        # constraint changes
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   736
        syssource = cnx.repo.system_source
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   737
        cstrtype = self.oldcstr.type()
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   738
        if cstrtype == 'SizeConstraint':
10585
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   739
            # if the size constraint is being replaced with a new max size, we'll
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   740
            # call update_rdef_column in CWConstraintAddOp, skip it here
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   741
            for cstr in cnx.transaction_data.get('newsizecstr', ()):
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   742
                rdefentity = cstr.reverse_constrained_by[0]
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   743
                cstrrdef = cnx.vreg.schema.schema_by_eid(rdefentity.eid)
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   744
                if cstrrdef == rdef:
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   745
                    return
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   746
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   747
            # we found that the size constraint for this rdef is really gone,
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   748
            # not just replaced by another
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   749
            syssource.update_rdef_column(cnx, rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   750
            self.size_cstr_changed = True
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   751
        elif cstrtype == 'UniqueConstraint':
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   752
            syssource.update_rdef_unique(cnx, rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   753
            self.unique_changed = True
11416
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   754
        elif cstrtype in ('BoundaryConstraint',
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   755
                          'IntervalBoundConstraint',
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   756
                          'StaticVocabularyConstraint'):
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   757
            cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s'
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   758
                           % (SQL_PREFIX, rdef.subject, self.oldcstr.name_for(rdef)))
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   759
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   760
    def revertprecommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   761
        # revert changes on in memory schema
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   762
        if self.newcstr is not None:
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   763
            self.rdef.constraints.remove(self.newcstr)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   764
        if self.oldcstr is not None:
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   765
            self.rdef.constraints.append(self.oldcstr)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   766
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   767
        syssource = self.cnx.repo.system_source
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   768
        if self.size_cstr_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   769
            syssource.update_rdef_column(self.cnx, self.rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   770
        if self.unique_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   771
            syssource.update_rdef_unique(self.cnx, self.rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   772
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   773
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   774
class CWConstraintAddOp(CWConstraintDelOp):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   775
    """actually update constraint of a relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   776
    entity = None  # make pylint happy
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   777
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   778
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   779
        cnx = self.cnx
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   780
        rdefentity = self.entity.reverse_constrained_by[0]
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   781
        rdef = self.rdef = cnx.vreg.schema.schema_by_eid(rdefentity.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
        cstrtype = self.entity.type
10813
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10799
diff changeset
   783
        if cstrtype in UNIQUE_CONSTRAINTS:
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10799
diff changeset
   784
            oldcstr = self.oldcstr = rdef.constraint_by_type(cstrtype)
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10799
diff changeset
   785
        else:
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10799
diff changeset
   786
            oldcstr = None
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   787
        newcstr = self.newcstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   788
        # in-place modification of in-memory schema first
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   789
        _set_modifiable_constraints(rdef)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   790
        newcstr.eid = self.entity.eid
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   791
        if oldcstr is not None:
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   792
            rdef.constraints.remove(oldcstr)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   793
        rdef.constraints.append(newcstr)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   794
        # then update database: alter the physical schema on size/unique
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   795
        # constraint changes
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   796
        syssource = cnx.repo.system_source
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   797
        if cstrtype == 'SizeConstraint' and (oldcstr is None or
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   798
                                             oldcstr.max != newcstr.max):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   799
            syssource.update_rdef_column(cnx, rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   800
            self.size_cstr_changed = True
2697
a0c4bc933a1b [schema hooks] rename variable, check unique constraint actually changed before creating the index
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2680
diff changeset
   801
        elif cstrtype == 'UniqueConstraint' and oldcstr is None:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   802
            syssource.update_rdef_unique(cnx, rdef)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   803
            self.unique_changed = True
11415
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   804
        if cstrtype in ('BoundaryConstraint',
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   805
                        'IntervalBoundConstraint',
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   806
                        'StaticVocabularyConstraint'):
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   807
            cstrname, check = y2sql.check_constraint(rdef, newcstr, syssource.dbhelper,
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   808
                                                     prefix=SQL_PREFIX)
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   809
            # oldcstr is the new constraint when the attribute is being added in the same
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   810
            # transaction or when constraint value is updated. So we've to take care...
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   811
            if oldcstr is not None:
11416
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   812
                oldcstrname = self.oldcstr.name_for(rdef)
11415
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   813
                if oldcstrname != cstrname:
f87da59faea1 [migration] Test and fix case of addition of an attribute with some serialized constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11413
diff changeset
   814
                    cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s'
11416
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   815
                                   % (SQL_PREFIX, rdef.subject, oldcstrname))
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   816
            cnx.system_sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' %
11416
9c2fbb872e91 [schema] Add a method on yams constraints to compute its unique name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11415
diff changeset
   817
                           (SQL_PREFIX, rdef.subject, cstrname, check))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   818
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
   819
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   820
class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   821
    entity = None  # make pylint happy
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   822
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   823
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   824
        cnx = self.cnx
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   825
        prefix = SQL_PREFIX
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   826
        entity = self.entity
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   827
        table = '%s%s' % (prefix, entity.constraint_of[0].name)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   828
        cols = ['%s%s' % (prefix, r.name) for r in entity.relations]
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   829
        dbhelper = cnx.repo.system_source.dbhelper
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   830
        sqls = dbhelper.sqls_create_multicol_unique_index(table, cols, entity.name)
6294
a1535abe6ab2 use new logilab.database interface for multicol indexes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6261
diff changeset
   831
        for sql in sqls:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   832
            cnx.system_sql(sql)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   833
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   834
    def postcommit_event(self):
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   835
        entity = self.entity
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   836
        eschema = self.cnx.vreg.schema.schema_by_eid(entity.constraint_of[0].eid)
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   837
        attrs = [r.name for r in entity.relations]
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   838
        eschema._unique_together.append(attrs)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   839
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
   840
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   841
class CWUniqueTogetherConstraintDelOp(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   842
    entity = cstrname = None  # make pylint happy
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   843
    cols = ()  # make pylint happy
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   844
10586
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   845
    def insert_index(self):
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   846
        # We need to run before CWConstraintDelOp: if a size constraint is
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   847
        # removed and the column is part of a unique_together constraint, we
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   848
        # remove the unique_together index before changing the column's type.
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   849
        # SQL Server does not support unique indices on unlimited text columns.
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   850
        return 0
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   851
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   852
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   853
        cnx = self.cnx
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   854
        prefix = SQL_PREFIX
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   855
        table = '%s%s' % (prefix, self.entity.type)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   856
        dbhelper = cnx.repo.system_source.dbhelper
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   857
        cols = ['%s%s' % (prefix, c) for c in self.cols]
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   858
        sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols, self.cstrname)
6294
a1535abe6ab2 use new logilab.database interface for multicol indexes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6261
diff changeset
   859
        for sql in sqls:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   860
            cnx.system_sql(sql)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   861
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   862
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   863
        eschema = self.cnx.vreg.schema.schema_by_eid(self.entity.eid)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   864
        cols = set(self.cols)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   865
        unique_together = [ut for ut in eschema._unique_together
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   866
                           if set(ut) != cols]
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   867
        eschema._unique_together = unique_together
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   868
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
   869
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   870
# operations for in-memory schema synchronization  #############################
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   871
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   872
class MemSchemaCWETypeDel(MemSchemaOperation):
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   873
    """actually remove the entity type from the instance's schema"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   874
    etype = None  # make pylint happy
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   875
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   876
    def postcommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   877
        # del_entity_type also removes entity's relations
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   878
        self.cnx.vreg.schema.del_entity_type(self.etype)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   879
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   880
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   881
class MemSchemaCWRTypeAdd(MemSchemaOperation):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   882
    """actually add the relation type to the instance's schema"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   883
    rtypedef = None  # make pylint happy
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   884
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   885
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   886
        self.cnx.vreg.schema.add_relation_type(self.rtypedef)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   887
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   888
    def revertprecommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   889
        self.cnx.vreg.schema.del_relation_type(self.rtypedef.name)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   890
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   891
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   892
class MemSchemaCWRTypeDel(MemSchemaOperation):
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   893
    """actually remove the relation type from the instance's schema"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   894
    rtype = None  # make pylint happy
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   895
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   896
    def postcommit_event(self):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   897
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   898
            self.cnx.vreg.schema.del_relation_type(self.rtype)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   899
        except KeyError:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   900
            # s/o entity type have already been deleted
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   901
            pass
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   902
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   903
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   904
class MemSchemaPermissionAdd(MemSchemaOperation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   905
    """synchronize schema when a *_permission relation has been added on a group
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   906
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   907
    eid = action = group_eid = expr = None  # make pylint happy
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   908
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   909
    def precommit_event(self):
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   910
        """the observed connections.cnxset has been commited"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   911
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   912
            erschema = self.cnx.vreg.schema.schema_by_eid(self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   913
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   914
            # duh, schema not found, log error and skip operation
4624
1b46d5ece0d5 turn default logging threshold to warning (we usually want them), and log 'no schema for eid' pb using warning instead of error, so we see them in logs but not during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4591
diff changeset
   915
            self.warning('no schema for %s', self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   916
            return
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   917
        perms = list(erschema.action_permissions(self.action))
7880
f475a34d0ddc [sync schema hooks] pylint fixes have broken hasattr test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   918
        if self.group_eid is not None:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   919
            perm = self.cnx.entity_from_eid(self.group_eid).name
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   920
        else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   921
            perm = erschema.rql_expression(self.expr)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   922
        try:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   923
            perms.index(perm)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   924
            self.warning('%s already in permissions for %s on %s',
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   925
                         perm, self.action, erschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   926
        except ValueError:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   927
            perms.append(perm)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   928
            erschema.set_action_permissions(self.action, perms)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   929
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   930
    # XXX revertprecommit_event
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   931
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   932
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   933
class MemSchemaPermissionDel(MemSchemaPermissionAdd):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   934
    """synchronize schema when a *_permission relation has been deleted from a
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   935
    group
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   936
    """
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   937
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   938
    def precommit_event(self):
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
   939
        """the observed connections set has been commited"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   940
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   941
            erschema = self.cnx.vreg.schema.schema_by_eid(self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   942
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   943
            # duh, schema not found, log error and skip operation
4624
1b46d5ece0d5 turn default logging threshold to warning (we usually want them), and log 'no schema for eid' pb using warning instead of error, so we see them in logs but not during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4591
diff changeset
   944
            self.warning('no schema for %s', self.eid)
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
   945
            return
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   946
        perms = list(erschema.action_permissions(self.action))
7880
f475a34d0ddc [sync schema hooks] pylint fixes have broken hasattr test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   947
        if self.group_eid is not None:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   948
            perm = self.cnx.entity_from_eid(self.group_eid).name
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   949
        else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   950
            perm = erschema.rql_expression(self.expr)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   951
        try:
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
   952
            perms.remove(perm)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   953
            erschema.set_action_permissions(self.action, perms)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   954
        except ValueError:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   955
            self.error('can\'t remove permission %s for %s on %s',
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   956
                       perm, self.action, erschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   957
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   958
    # XXX revertprecommit_event
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   959
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   960
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   961
class MemSchemaSpecializesAdd(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   962
    etypeeid = parentetypeeid = None  # make pylint happy
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   963
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   964
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   965
        eschema = self.cnx.vreg.schema.schema_by_eid(self.etypeeid)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   966
        parenteschema = self.cnx.vreg.schema.schema_by_eid(self.parentetypeeid)
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   967
        eschema._specialized_type = parenteschema.type
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   968
        parenteschema._specialized_by.append(eschema.type)
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   969
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   970
    # XXX revertprecommit_event
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   971
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   972
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   973
class MemSchemaSpecializesDel(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   974
    etypeeid = parentetypeeid = None  # make pylint happy
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   975
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   976
    def precommit_event(self):
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   977
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   978
            eschema = self.cnx.vreg.schema.schema_by_eid(self.etypeeid)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   979
            parenteschema = self.cnx.vreg.schema.schema_by_eid(self.parentetypeeid)
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   980
        except KeyError:
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   981
            # etype removed, nothing to do
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   982
            return
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   983
        eschema._specialized_type = None
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   984
        parenteschema._specialized_by.remove(eschema.type)
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   985
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   986
    # XXX revertprecommit_event
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   987
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
   988
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   989
# CWEType hooks ################################################################
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   990
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   991
class DelCWETypeHook(SyncSchemaHook):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   992
    """before deleting a CWEType entity:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   993
    * check that we don't remove a core entity type
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   994
    * cascade to delete related CWAttribute and CWRelation entities
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   995
    * instantiate an operation to delete the entity type on commit
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   996
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   997
    __regid__ = 'syncdelcwetype'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5849
diff changeset
   998
    __select__ = SyncSchemaHook.__select__ & is_instance('CWEType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   999
    events = ('before_delete_entity',)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1000
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1001
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1002
        # final entities can't be deleted, don't care about that
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1003
        name = self.entity.name
6261
82d3ac90e47c [syncschema hooks] simplify core types definition be reusing schema sets (remove typos on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
  1004
        if name in CORE_TYPES:
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8534
diff changeset
  1005
            raise validation_error(self.entity, {None: _("can't be deleted")})
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1006
        # delete every entities of this type
7504
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
  1007
        if name not in ETYPE_NAME_MAP:
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1008
            MemSchemaCWETypeDel(self._cw, etype=name)
11231
b0dcbc7cf410 [hooks] deleting a final entity type shouldn't try dropping an sql table
Julien Cristau <julien.cristau@logilab.fr>
parents: 11129
diff changeset
  1009
        if not self.entity.final:
b0dcbc7cf410 [hooks] deleting a final entity type shouldn't try dropping an sql table
Julien Cristau <julien.cristau@logilab.fr>
parents: 11129
diff changeset
  1010
            DropTable(self._cw, table=SQL_PREFIX + name)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1011
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1012
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1013
class AfterDelCWETypeHook(DelCWETypeHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1014
    __regid__ = 'wfcleanup'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1015
    events = ('after_delete_entity',)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1016
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1017
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1018
        # workflow cleanup
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1019
        self._cw.execute('DELETE Workflow X WHERE NOT X workflow_of Y')
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1020
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1021
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1022
class AfterAddCWETypeHook(DelCWETypeHook):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1023
    """after adding a CWEType entity:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1024
    * create the necessary table
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1025
    * set creation_date and modification_date by creating the necessary
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1026
      CWAttribute entities
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1027
    * add owned_by relation by creating the necessary CWRelation entity
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1028
    * register an operation to add the entity type to the instance's
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1029
      schema on commit
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1030
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1031
    __regid__ = 'syncaddcwetype'
3402
434946bb5356 should be a after_add hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3400
diff changeset
  1032
    events = ('after_add_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1033
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1034
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1035
        entity = self.entity
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1036
        if entity.cw_edited.get('final'):
7181
e54ad6984e01 [schema synchronization] get back eid when adding a final entity type, else it may cause pb on later schema synchronization during the migration where the type is added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7021
diff changeset
  1037
            # final entity types don't need a table in the database and are
7185
6e42b595e5df rephrase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7181
diff changeset
  1038
            # systematically added by yams at schema initialization time so
6e42b595e5df rephrase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7181
diff changeset
  1039
            # there is no need to do further processing. Simply assign its eid.
7181
e54ad6984e01 [schema synchronization] get back eid when adding a final entity type, else it may cause pb on later schema synchronization during the migration where the type is added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7021
diff changeset
  1040
            self._cw.vreg.schema[entity.name].eid = entity.eid
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1041
            return
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1042
        CWETypeAddOp(self._cw, entity=entity)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1043
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1044
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1045
class BeforeUpdateCWETypeHook(DelCWETypeHook):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1046
    """check name change, handle final"""
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1047
    __regid__ = 'syncupdatecwetype'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1048
    events = ('before_update_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1049
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1050
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1051
        entity = self.entity
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1052
        check_valid_changes(self._cw, entity, ro_attrs=('final',))
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1053
        # don't use getattr(entity, attr), we would get the modified value if any
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1054
        if 'name' in entity.cw_edited:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1055
            oldname, newname = entity.cw_edited.oldnewvalue('name')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1056
            if newname.lower() != oldname.lower():
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1057
                CWETypeRenameOp(self._cw, oldname=oldname, newname=newname)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1058
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1059
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1060
# CWRType hooks ################################################################
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1061
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1062
class DelCWRTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1063
    """before deleting a CWRType entity:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1064
    * check that we don't remove a core relation type
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1065
    * cascade to delete related CWAttribute and CWRelation entities
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1066
    * instantiate an operation to delete the relation type on commit
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1067
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1068
    __regid__ = 'syncdelcwrtype'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5849
diff changeset
  1069
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1070
    events = ('before_delete_entity',)
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1071
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1072
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1073
        name = self.entity.name
6261
82d3ac90e47c [syncschema hooks] simplify core types definition be reusing schema sets (remove typos on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
  1074
        if name in CORE_TYPES:
8556
bbe0d6985e59 [validation error] refactor validation error handling so translation is done on the web side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8534
diff changeset
  1075
            raise validation_error(self.entity, {None: _("can't be deleted")})
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1076
        # delete relation definitions using this relation type
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1077
        self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1078
                         {'x': self.entity.eid})
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1079
        self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1080
                         {'x': self.entity.eid})
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1081
        MemSchemaCWRTypeDel(self._cw, rtype=name)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1082
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1083
9963
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1084
class AfterAddCWComputedRTypeHook(SyncSchemaHook):
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1085
    """after a CWComputedRType entity has been added:
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1086
    * register an operation to add the relation type to the instance's
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1087
      schema on commit
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1088
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1089
    We don't know yet this point if a table is necessary
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1090
    """
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1091
    __regid__ = 'syncaddcwcomputedrtype'
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1092
    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1093
    events = ('after_add_entity',)
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1094
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1095
    def __call__(self):
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1096
        entity = self.entity
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1097
        rtypedef = ybo.ComputedRelation(name=entity.name,
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1098
                                        eid=entity.eid,
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1099
                                        rule=entity.rule)
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1100
        MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1101
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1102
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1103
class AfterAddCWRTypeHook(SyncSchemaHook):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1104
    """after a CWRType entity has been added:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1105
    * register an operation to add the relation type to the instance's
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1106
      schema on commit
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1107
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1108
    We don't know yet this point if a table is necessary
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1109
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1110
    __regid__ = 'syncaddcwrtype'
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1111
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1112
    events = ('after_add_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1113
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1114
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1115
        entity = self.entity
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1116
        rtypedef = ybo.RelationType(name=entity.name,
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1117
                                    description=entity.description,
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1118
                                    inlined=entity.cw_edited.get('inlined', False),
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1119
                                    symmetric=entity.cw_edited.get('symmetric', False),
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1120
                                    eid=entity.eid)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1121
        MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1122
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1123
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1124
class BeforeUpdateCWRTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1125
    """check name change, handle final"""
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1126
    __regid__ = 'syncupdatecwrtype'
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1127
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1128
    events = ('before_update_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1129
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1130
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1131
        entity = self.entity
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1132
        check_valid_changes(self._cw, entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1133
        newvalues = {}
4827
8a505bf12193 [hook] use entity instead of values dict, check for edited_attributes on rtype change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4826
diff changeset
  1134
        for prop in ('symmetric', 'inlined', 'fulltext_container'):
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1135
            if prop in entity.cw_edited:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1136
                old, new = entity.cw_edited.oldnewvalue(prop)
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1137
                if old != new:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1138
                    newvalues[prop] = new
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1139
        if newvalues:
4827
8a505bf12193 [hook] use entity instead of values dict, check for edited_attributes on rtype change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4826
diff changeset
  1140
            rschema = self._cw.vreg.schema.rschema(entity.name)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1141
            CWRTypeUpdateOp(self._cw, rschema=rschema, entity=entity,
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1142
                            values=newvalues)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1143
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1144
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1145
class BeforeUpdateCWComputedRTypeHook(SyncSchemaHook):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1146
    """check name change, handle final"""
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1147
    __regid__ = 'syncupdatecwcomputedrtype'
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1148
    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1149
    events = ('before_update_entity',)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1150
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1151
    def __call__(self):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1152
        entity = self.entity
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1153
        check_valid_changes(self._cw, entity)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1154
        if 'rule' in entity.cw_edited:
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1155
            old, new = entity.cw_edited.oldnewvalue('rule')
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1156
            if old != new:
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1157
                rschema = self._cw.vreg.schema.rschema(entity.name)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1158
                CWComputedRTypeUpdateOp(self._cw, rschema=rschema,
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1159
                                        entity=entity, rule=new)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1160
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1161
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1162
class AfterDelRelationTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1163
    """before deleting a CWAttribute or CWRelation entity:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1164
    * if this is a final or inlined relation definition, instantiate an
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1165
      operation to drop necessary column, else if this is the last instance
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1166
      of a non final relation, instantiate an operation to drop necessary
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1167
      table
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1168
    * instantiate an operation to delete the relation definition on commit
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1169
    * delete the associated relation type when necessary
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1170
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1171
    __regid__ = 'syncdelrelationtype'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1172
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('relation_type')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1173
    events = ('after_delete_relation',)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1174
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1175
    def __call__(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1176
        cnx = self._cw
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: 5556
diff changeset
  1177
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1178
            rdef = cnx.vreg.schema.schema_by_eid(self.eidfrom)
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: 5556
diff changeset
  1179
        except KeyError:
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: 5556
diff changeset
  1180
            self.critical('cant get schema rdef associated to %s', self.eidfrom)
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: 5556
diff changeset
  1181
            return
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
  1182
        subjschema, rschema, objschema = rdef.as_triple()
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1183
        pendingrdefs = cnx.transaction_data.setdefault('pendingrdefs', set())
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1184
        # first delete existing relation if necessary
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689 3659
diff changeset
  1185
        if rschema.final:
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3550 3536
diff changeset
  1186
            pendingrdefs.add((subjschema, rschema))
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1187
        else:
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3550 3536
diff changeset
  1188
            pendingrdefs.add((subjschema, rschema, objschema))
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1189
        RDefDelOp(cnx, rdef=rdef)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1190
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1191
9962
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1192
# CWComputedRType hooks #######################################################
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1193
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1194
class DelCWComputedRTypeHook(SyncSchemaHook):
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1195
    """before deleting a CWComputedRType entity:
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1196
    * check that we don't remove a core relation type
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1197
    * instantiate an operation to delete the relation type on commit
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1198
    """
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1199
    __regid__ = 'syncdelcwcomputedrtype'
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1200
    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1201
    events = ('before_delete_entity',)
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1202
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1203
    def __call__(self):
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1204
        name = self.entity.name
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1205
        if name in CORE_TYPES:
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1206
            raise validation_error(self.entity, {None: _("can't be deleted")})
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1207
        MemSchemaCWRTypeDel(self._cw, rtype=name)
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1208
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1209
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1210
# CWAttribute / CWRelation hooks ###############################################
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1211
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1212
class AfterAddCWAttributeHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1213
    __regid__ = 'syncaddcwattribute'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5849
diff changeset
  1214
    __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1215
    events = ('after_add_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1216
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1217
    def __call__(self):
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1218
        CWAttributeAddOp(self._cw, entity=self.entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1219
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1220
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1221
class AfterAddCWRelationHook(AfterAddCWAttributeHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1222
    __regid__ = 'syncaddcwrelation'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5849
diff changeset
  1223
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRelation')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1224
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1225
    def __call__(self):
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1226
        CWRelationAddOp(self._cw, entity=self.entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1227
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1228
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1229
class AfterUpdateCWRDefHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1230
    __regid__ = 'syncaddcwattribute'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5849
diff changeset
  1231
    __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute',
5881
57387070f612 [selectors] use before-registry-reset event to init is_instance cache: cleaner code and avoid reloading bug (making hooks test fail for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
  1232
                                                         'CWRelation')
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1233
    events = ('before_update_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1234
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1235
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1236
        entity = self.entity
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1237
        if self._cw.deleted_in_transaction(entity.eid):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1238
            return
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1239
        subjtype = entity.stype.name
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1240
        objtype = entity.otype.name
7504
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
  1241
        if subjtype in ETYPE_NAME_MAP or objtype in ETYPE_NAME_MAP:
d95f9002edcc [sync schema hooks] consider etype name map before trying to update in-memory schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7185
diff changeset
  1242
            return
3400
93fc0070673a access to schema through vreg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
  1243
        rschema = self._cw.vreg.schema[entity.rtype.name]
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1244
        # note: do not access schema rdef here, it may be added later by an
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1245
        # operation
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1246
        newvalues = {}
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1247
        for prop in RelationDefinitionSchema.rproperty_defs(objtype):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1248
            if prop == 'constraints':
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1249
                continue
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1250
            if prop == 'order':
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1251
                attr = 'ordernum'
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1252
            else:
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1253
                attr = prop
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1254
            if attr in entity.cw_edited:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1255
                old, new = entity.cw_edited.oldnewvalue(attr)
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1256
                if old != new:
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1257
                    newvalues[prop] = new
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1258
        if newvalues:
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1259
            RDefUpdateOp(self._cw, rschema=rschema, rdefkey=(subjtype, objtype),
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1260
                         values=newvalues)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1261
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1262
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1263
# constraints synchronization hooks ############################################
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1264
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1265
class AfterAddCWConstraintHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1266
    __regid__ = 'syncaddcwconstraint'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5849
diff changeset
  1267
    __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1268
    events = ('after_add_entity', 'after_update_entity')
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1269
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1270
    def __call__(self):
10585
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
  1271
        if self.entity.cstrtype[0].name == 'SizeConstraint':
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
  1272
            txdata = self._cw.transaction_data
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
  1273
            if 'newsizecstr' not in txdata:
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
  1274
                txdata['newsizecstr'] = set()
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
  1275
            txdata['newsizecstr'].add(self.entity)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1276
        CWConstraintAddOp(self._cw, entity=self.entity)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1277
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1278
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1279
class AfterAddConstrainedByHook(SyncSchemaHook):
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1280
    __regid__ = 'syncaddconstrainedby'
3403
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3402
diff changeset
  1281
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('constrained_by')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1282
    events = ('after_add_relation',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1283
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1284
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1285
        if self._cw.added_in_transaction(self.eidfrom):
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1286
            # used by get_constraints() which is called in CWAttributeAddOp
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1287
            self._cw.transaction_data.setdefault(self.eidfrom, []).append(self.eidto)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1288
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1289
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1290
class BeforeDeleteCWConstraintHook(SyncSchemaHook):
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1291
    __regid__ = 'syncdelcwconstraint'
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1292
    __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint')
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1293
    events = ('before_delete_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1294
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1295
    def __call__(self):
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1296
        entity = self.entity
3400
93fc0070673a access to schema through vreg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
  1297
        schema = self._cw.vreg.schema
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1298
        try:
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1299
            # KeyError, e.g. composite chain deletion
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1300
            rdef = schema.schema_by_eid(entity.reverse_constrained_by[0].eid)
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1301
            # IndexError
9636
e35ae8617c03 Fix constraint sync during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
  1302
            cstr = rdef.constraint_by_eid(entity.eid)
9635
aaf099172bb9 merge 3.18.4 into default
Julien Cristau <julien.cristau@logilab.fr>
parents: 9621 9600
diff changeset
  1303
        except (KeyError, IndexError):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1304
            self._cw.critical('constraint type no more accessible')
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1305
        else:
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1306
            CWConstraintDelOp(self._cw, rdef=rdef, oldcstr=cstr)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1307
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1308
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1309
# unique_together constraints
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1310
# XXX: use setoperations and before_add_relation here (on constraint_of and relations)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1311
class AfterAddCWUniqueTogetherConstraintHook(SyncSchemaHook):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1312
    __regid__ = 'syncadd_cwuniquetogether_constraint'
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1313
    __select__ = SyncSchemaHook.__select__ & is_instance('CWUniqueTogetherConstraint')
7021
fc7ac3409b0c [schema sync] we don't want to react on after_update_entity, it may only causes bug. CWUniqueConstraint should be deleted then a fresh one readded on change (as we do in sync_schema_props_perms)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6954
diff changeset
  1314
    events = ('after_add_entity',)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1315
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1316
    def __call__(self):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1317
        CWUniqueTogetherConstraintAddOp(self._cw, entity=self.entity)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1318
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1319
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1320
class BeforeDeleteConstraintOfHook(SyncSchemaHook):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1321
    __regid__ = 'syncdelconstraintof'
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1322
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('constraint_of')
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1323
    events = ('before_delete_relation',)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1324
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1325
    def __call__(self):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1326
        if self._cw.deleted_in_transaction(self.eidto):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1327
            return
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1328
        schema = self._cw.vreg.schema
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1329
        cstr = self._cw.entity_from_eid(self.eidfrom)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1330
        entity = schema.schema_by_eid(self.eidto)
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
  1331
        cols = tuple(r.name for r in cstr.relations)
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
  1332
        CWUniqueTogetherConstraintDelOp(self._cw, entity=entity,
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
  1333
                                        cstrname=cstr.name, cols=cols)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1334
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1335
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1336
# permissions synchronization hooks ############################################
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1337
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1338
class AfterAddPermissionHook(SyncSchemaHook):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1339
    """added entity/relation *_permission, need to update schema"""
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1340
    __regid__ = 'syncaddperm'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1341
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype(
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1342
        'read_permission', 'add_permission', 'delete_permission',
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1343
        'update_permission')
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1344
    events = ('after_add_relation',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1345
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1346
    def __call__(self):
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1347
        action = self.rtype.split('_', 1)[0]
11765
9cb215e833b0 [cnx] Use entity_type instead of entity_metas()['type']
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
  1348
        if self._cw.entity_type(self.eidto) == 'CWGroup':
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1349
            MemSchemaPermissionAdd(self._cw, action=action, eid=self.eidfrom,
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1350
                                   group_eid=self.eidto)
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1351
        else:  # RQLExpression
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1352
            expr = self._cw.entity_from_eid(self.eidto).expression
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
  1353
            MemSchemaPermissionAdd(self._cw, action=action, eid=self.eidfrom,
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1354
                                   expr=expr)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1355
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1356
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1357
class BeforeDelPermissionHook(AfterAddPermissionHook):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1358
    """delete entity/relation *_permission, need to update schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1359
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1360
    skip the operation if the related type is being deleted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1361
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1362
    __regid__ = 'syncdelperm'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1363
    events = ('before_delete_relation',)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1364
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1365
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1366
        if self._cw.deleted_in_transaction(self.eidfrom):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1367
            return
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1368
        action = self.rtype.split('_', 1)[0]
11765
9cb215e833b0 [cnx] Use entity_type instead of entity_metas()['type']
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
  1369
        if self._cw.entity_type(self.eidto) == 'CWGroup':
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1370
            MemSchemaPermissionDel(self._cw, action=action, eid=self.eidfrom,
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1371
                                   group_eid=self.eidto)
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1372
        else:  # RQLExpression
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1373
            expr = self._cw.entity_from_eid(self.eidto).expression
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1374
            MemSchemaPermissionDel(self._cw, action=action, eid=self.eidfrom,
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1375
                                   expr=expr)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1376
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1377
6426
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1378
class UpdateFTIndexOp(hook.DataOperationMixIn, hook.SingleLastOperation):
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1379
    """operation to update full text indexation of entity whose schema change
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1380
6426
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1381
    We wait after the commit to as the schema in memory is only updated after
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1382
    the commit.
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1383
    """
10250
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
  1384
    containercls = list
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1385
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1386
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1387
        cnx = self.cnx
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1388
        source = cnx.repo.system_source
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1389
        schema = cnx.repo.vreg.schema
6426
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1390
        to_reindex = self.get_data()
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1391
        self.info('%i etypes need full text indexed reindexation',
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1392
                  len(to_reindex))
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1393
        for etype in to_reindex:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1394
            rset = cnx.execute('Any X WHERE X is %s' % etype)
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1395
            self.info('Reindexing full text index for %i entity of type %s',
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1396
                      len(rset), etype)
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1397
            still_fti = list(schema[etype].indexable_attributes())
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1398
            for entity in rset.entities():
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1399
                source.fti_unindex_entities(cnx, [entity])
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
  1400
                for container in entity.cw_adapt_to('IFTIndexable').fti_containers():
4828
3e173d598cad [cleanup] fti_* method already try/except exception, no need for additional catch here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4827
diff changeset
  1401
                    if still_fti or container is not entity:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1402
                        source.fti_unindex_entities(cnx, [container])
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1403
                        source.fti_index_entities(cnx, [container])
6426
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1404
        if to_reindex:
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1405
            # Transaction has already been committed
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1406
            cnx.cnxset.commit()
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1407
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1408
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1409
# specializes synchronization hooks ############################################
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1410
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1411
class AfterAddSpecializesHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1412
    __regid__ = 'syncaddspecializes'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1413
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1414
    events = ('after_add_relation',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1415
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1416
    def __call__(self):
3404
9cb6b0cc2a7c fix some name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3403
diff changeset
  1417
        MemSchemaSpecializesAdd(self._cw, etypeeid=self.eidfrom,
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1418
                                parentetypeeid=self.eidto)
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1419
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1420
3410
47832374cf14 fix class name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3404
diff changeset
  1421
class AfterDelSpecializesHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1422
    __regid__ = 'syncdelspecializes'
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1423
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1424
    events = ('after_delete_relation',)
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1425
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1426
    def __call__(self):
3404
9cb6b0cc2a7c fix some name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3403
diff changeset
  1427
        MemSchemaSpecializesDel(self._cw, etypeeid=self.eidfrom,
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1428
                                parentetypeeid=self.eidto)