cubicweb/hooks/syncschema.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 22 Mar 2019 17:42:36 +0100
changeset 12542 85194bd49119
parent 12512 661dd0436c01
permissions -rw-r--r--
Drop more deprecated code This follows up on changeset a8c1ea390400, in which code deprecated using logilab.common.deprecation got dropped. Now we also drop code deprecated using stdlib's warn(<msg>, DeprecationWarning). Notice that, as a consequence of dropping old/new etypes aliases in cubicweb/schema.py, we drop the import ETYPE_NAME_MAP (no longer needed); but since other modules imported that name from cubicweb.schema, we need to update the import statement to use "cubicweb" directly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11903
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
     1
# copyright 2003-2017 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 _
12542
85194bd49119 Drop more deprecated code
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12512
diff changeset
    36
from cubicweb import validation_error, ETYPE_NAME_MAP
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,
12542
85194bd49119 Drop more deprecated code
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12512
diff changeset
    39
                             CONSTRAINTS, UNIQUE_CONSTRAINTS)
12512
661dd0436c01 [schema] drop monkeypatch "name_for" on BaseConstraint
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12025
diff changeset
    40
from cubicweb.schema import constraint_name_for
10200
cceb2c7c02f4 Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10074
diff changeset
    41
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
    42
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
    43
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
    44
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
# 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
    46
CORE_TYPES = BASE_TYPES | SCHEMA_TYPES | META_RTYPES | set(
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
    47
    ('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
    48
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    50
def get_constraints(cnx, entity):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
    constraints = []
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    52
    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
    53
        cstrent = cnx.entity_from_eid(cstreid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        cstr = CONSTRAINTS[cstrent.type].deserialize(cstrent.value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        cstr.eid = cstreid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        constraints.append(cstr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    return constraints
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    59
4043
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    60
def group_mapping(cw):
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    61
    try:
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    62
        return cw.transaction_data['groupmap']
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    63
    except KeyError:
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    64
        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
    65
        return gmap
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    66
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    67
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    68
def add_inline_relation_column(cnx, etype, rtype):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    """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
    70
    attrkey = '%s.%s' % (etype, rtype)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    71
    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
    72
    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
    73
        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
    74
    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
    75
    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
    76
    column = SQL_PREFIX + rtype
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    try:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
    78
        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
    79
                           % (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
    80
                       rollback_on_failure=False)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    81
        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
    82
    except Exception:
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    83
        # 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
    84
        # 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
    85
        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
    86
                      table, column)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    # create index before alter table which may expectingly fail during test
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    # (sqlite) while index creation should never fail (test for index existence
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    # is done by the dbhelper)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    90
    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
    91
    cnx.info('added index on %s(%s)', table, column)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    93
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    94
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
    95
    # 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
    96
    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
    97
                  '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
    98
                  '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
    99
                  '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
   100
                  '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
   101
                  '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
   102
                  })
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   103
    cstrtypemap = ss.cstrtype_mapping(cnx)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   104
    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
   105
    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
   106
    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
   107
        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
   108
            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
   109
        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
   110
                                           object, None, values=props)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   111
        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
   112
                         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
   113
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
   114
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   115
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
   116
    errors = {}
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   117
    # 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
   118
    for attr in entity.cw_edited:
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   119
        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
   120
            origval, newval = entity.cw_edited.oldnewvalue(attr)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   121
            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
   122
                errors[attr] = _("can't change this attribute")
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   123
    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
   124
        raise validation_error(entity, errors)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   125
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   127
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
   128
    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
   129
        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
   130
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
   131
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
   132
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
   133
    """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
   134
    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
   135
    """
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
    __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
   137
    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
   138
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
   139
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
   140
# 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
   141
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   142
class DropTable(hook.Operation):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2462
diff changeset
   143
    """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
   144
    table = None  # make pylint happy
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   145
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
    def precommit_event(self):
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   147
        dropped = self.cnx.transaction_data.setdefault('droppedtables', set())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        if self.table in dropped:
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   149
            return  # already processed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        dropped.add(self.table)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   151
        self.cnx.system_sql('DROP TABLE %s' % self.table)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        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
   153
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
   154
    # 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
   155
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
   156
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
   157
class DropRelationTable(DropTable):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   158
    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
   159
        super(DropRelationTable, self).__init__(
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   160
            cnx, table='%s_relation' % rtype)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   161
        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
   162
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
   163
10916
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   164
class DropColumn(hook.DataOperationMixIn, hook.Operation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
    """actually remove the attribut's column from entity table in the system
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    database
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
    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
   169
        cnx = self.cnx
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   170
        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
   171
            table = SQL_PREFIX + etype
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   172
            column = SQL_PREFIX + attr
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   173
            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
   174
            # drop index if any
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   175
            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
   176
            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
   177
                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
   178
                               rollback_on_failure=False)
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   179
                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
   180
            else:
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   181
                # 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
   182
                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
   183
                           '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
   184
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
   185
    # 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
   186
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
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
   188
# base operations for in-memory schema synchronization  ########################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   190
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
   191
    """the update schema operation:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
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
   193
    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
   194
    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
   195
    schema changes.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   198
    def __init__(self, cnx):
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   199
        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
   200
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
   201
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   202
        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
   203
            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
   204
                clear_cache(eschema, 'ordered_relations')
11807
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   205
                # clear additional cached properties
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   206
                try:
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   207
                    # is_composite use composite_rdef_roles, hence one try except should be enough
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   208
                    del eschema.composite_rdef_roles
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   209
                    del eschema.is_composite
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   210
                except AttributeError:
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
   211
                    pass
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
   212
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
   213
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   214
        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
   215
        # 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
   216
        # 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
   217
        try:
9588
fe267b7336f3 [migration] always rebuild infered relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9375
diff changeset
   218
            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
   219
            # trigger vreg reload
58b3b2d9c965 [repo] kill rebuildinfered feature from Repository.set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8821
diff changeset
   220
            repo.set_schema(repo.schema)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7562
diff changeset
   221
        except Exception:
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7562
diff changeset
   222
            self.critical('error while setting schema', exc_info=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
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
   224
    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
   225
        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
   226
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   227
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   228
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
   229
    """base class for schema operations"""
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   230
    def __init__(self, cnx, **kwargs):
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   231
        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
   232
        # 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
   233
        MemSchemaNotifyChanges(cnx)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   235
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
   236
# 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
   237
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
   238
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
   239
    """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
   240
    * 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
   241
    * 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
   242
    * 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
   243
      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
   244
    * 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
   245
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   246
    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
   247
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
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   249
        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
   250
        entity = self.entity
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   251
        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
   252
        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
   253
                               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
   254
        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
   255
        # 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
   256
        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
   257
                                     eschema, prefix=SQL_PREFIX):
c172fa18565e [schema2sql] Avoid "parsing" SQL statements for database initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11411
diff changeset
   258
            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
   259
        # add meta relations
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   260
        gmap = group_mapping(cnx)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   261
        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
   262
        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
   263
            try:
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   264
                rschema = schema[rtype]
7551
2d4ba5b984dc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7504
diff changeset
   265
            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
   266
                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
   267
                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
   268
            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
   269
                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
   270
                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
   271
            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
   272
            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
   273
            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
   274
                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
   275
            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
   276
                # 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
   277
                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
   278
            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
   279
            mock = _MockEntity(eid=None)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   280
            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
   281
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
   282
    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
   283
        # 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
   284
        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
   285
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   286
        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
   287
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
   288
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
   289
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
   290
    """this operation updates physical storage accordingly"""
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   291
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   292
    oldname = newname = None  # make pylint happy
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
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
   294
    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
   295
        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
   296
        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
   297
        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
   298
        # 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
   299
        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
   300
        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
   301
        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
   302
        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
   303
        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
   304
        # 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
   305
        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
   306
            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
   307
                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
   308
            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
   309
                                     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
   310
                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
   311
        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
   312
        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
   313
        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
   314
        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
   315
                {'newname': newname, 'oldname': oldname})
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   316
        for eid, etype in cnx.repo._type_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
   317
            if etype == oldname:
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   318
                cnx.repo._type_cache[eid] = newname
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   319
        # 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
   320
        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
   321
            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
   322
                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
   323
            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
   324
                                     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
   325
                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
   326
        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
   327
            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
   328
            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
   329
            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
   330
                    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
   331
                sqlexec(sql)
11411
9a5a6ec5fc09 [hooks] Use already imported module alias
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11406
diff changeset
   332
            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
   333
            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
   334
                    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
   335
                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
   336
        # 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
   337
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
    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
   339
        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
   340
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
    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
   342
        self.rename(self.newname, self.oldname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
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
   345
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
   346
    """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
   347
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   348
    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
   349
    oldvalues = None
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   350
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
   351
    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
   352
        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
   353
        if rschema.final:
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   354
            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
   355
        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
   356
        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
   357
            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
   358
            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
   359
                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
   360
                    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
   361
                    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
   362
                else:
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   363
                    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
   364
                    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
   365
        # update the in-memory schema first
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   366
        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
   367
        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
   368
        # 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
   369
        if 'inlined' not in self.values:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   370
            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
   371
        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
   372
        # 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
   373
        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
   374
            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
   375
        # 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
   376
        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
   377
        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
   378
        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
   379
        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
   380
            # 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
   381
            # another event of the same transaction
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   382
            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
   383
                # 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
   384
                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
   385
                    sqlexec(sql)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   386
                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
   387
                    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
   388
            # 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
   389
            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
   390
            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
   391
                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
   392
                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
   393
                        % (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
   394
            # 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
   395
            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
   396
                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
   397
        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
   398
            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
   399
                try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   400
                    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
   401
                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
   402
                    # 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
   403
                    # 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
   404
                    # 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
   405
                    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
   406
                # 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
   407
                # 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
   408
                #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
   409
                #        '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
   410
                #        '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
   411
                #        % 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
                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
   413
                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
   414
                                 '%(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
   415
                                 '%(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
   416
                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
   417
                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
   418
                    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
   419
                    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
   420
                                       % (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
   421
                # drop existant table
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   422
                DropRelationTable(cnx, rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   423
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
   424
    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
   425
        # 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
   426
        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
   427
        # XXX revert changes on database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
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
   429
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   430
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
   431
    """actually update some properties of a computed relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   432
    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
   433
    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
   434
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   435
    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
   436
        # 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
   437
        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
   438
        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
   439
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   440
    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
   441
        # 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
   442
        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
   443
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   444
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
   445
class CWAttributeAddOp(MemSchemaOperation):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   446
    """an attribute relation (CWAttribute) has been added:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
    * add the necessary column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
    * set default on this column if any and possible
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
    * 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
   450
      instance's schema on commit
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   451
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
    constraints are handled by specific hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   454
    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
   455
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
   456
    def init_rdef(self, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
        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
   458
        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
   459
        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
   460
            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
   461
            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
   462
            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
   463
            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
   464
        try:
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   465
            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
   466
        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
   467
            # 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
   468
            # 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
   469
            # 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
   470
            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
   471
            assert rdef.infered
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   472
        else:
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   473
            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
   474
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   475
        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
   476
                         '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
   477
                         '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
   478
                         {'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
   479
                          '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
   480
        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
   481
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
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   483
        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
   484
        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
   485
        # 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
   486
        # value
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
        default = entity.defaultval
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
        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
   489
            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
   490
        props = {'default': default,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   491
                 'indexed': entity.indexed,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   492
                 'fulltextindexed': entity.fulltextindexed,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   493
                 '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
   494
        if entity.extra_props:
11038
7cb02ab4f321 [hooks] fix extra_props handling on python3
Julien Cristau <julien.cristau@logilab.fr>
parents: 11005
diff changeset
   495
            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
   496
        # 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
   497
        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
   498
            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
   499
        # 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
   500
        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
   501
        # 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
   502
        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
   503
        attrtype = y2sql.type_from_rdef(syssource.dbhelper, rdef)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
        # added some str() wrapping query since some backend (eg psycopg) don't
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
        # 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
   506
        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
   507
        column = SQL_PREFIX + rdefdef.name
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   509
            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
   510
                               % (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
   511
                           rollback_on_failure=False)
10035
c5bcdece77fb [hooks] Fix precommit event logging message
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9888
diff changeset
   512
            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
   513
        except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
            # the column probably already exists. this occurs when
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
            # the entity's type has just been added or if the column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
            # 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
   517
            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
   518
        if entity.indexed:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
            try:
11359
2da2dd60331c [sql gen] Explicitly name unique index
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11291
diff changeset
   520
                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
   521
            except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   522
                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
   523
                           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
   524
        # 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
   525
        schema = cnx.vreg.schema
3531
c095f5f54873 [migration] take care to entity being added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3526
diff changeset
   526
        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
   527
            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
   528
        except KeyError:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   529
            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
   530
        # 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
   531
        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
   532
        # 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
   533
        # 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
   534
        rschema.final = True
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   535
        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
   536
        # 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
   537
        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
   538
            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
   539
            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
   540
                           {'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
   541
        # 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
   542
        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
   543
            # 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
   544
            # 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
   545
            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
   546
            RecomputeAttributeOperation.get_instance(cnx).add_data(rdefdef)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
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
   548
    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
   549
        # 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
   550
        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
   551
            return
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   552
        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
   553
            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
   554
        # XXX revert changes on database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   555
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
   556
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
class CWRelationAddOp(CWAttributeAddOp):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   558
    """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
   559
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
    * 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
   561
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
   562
    * 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
   563
      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
   564
      default permissions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
    constraints are handled by specific hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   568
    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
   569
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   570
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   571
        cnx = self.cnx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   572
        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
   573
        # 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
   574
        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
   575
        # 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
   576
        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
   577
        rtype = rdefdef.name
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   578
        rschema = schema.rschema(rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
        # this have to be done before permissions setting
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   580
        if rschema.inlined:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   581
            # 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
   582
            # 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
   583
            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
   584
                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
   585
            eschema = schema[rdefdef.subject]
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   586
            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
   587
                                      {'composite': entity.composite})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   588
        else:
6163
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   589
            if rschema.symmetric:
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   590
                # 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
   591
                # 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
   592
                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
   593
            else:
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   594
                relation_already_defined = len(rschema.rdefs) > 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   595
            # need to create the relation if no relation definition in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
            # 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
   597
            # transaction
6163
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   598
            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
   599
                    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
   600
                rschema = schema.rschema(rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
                # 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
   602
                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
   603
                    cnx.system_sql(sql)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   604
                cnx.transaction_data.setdefault('createdtables', []).append(
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2056
diff changeset
   605
                    rtype)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   606
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
   607
    # XXX revertprecommit_event
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   608
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
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
   611
    """an actual relation has been removed"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   612
    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
   613
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
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   615
        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
   616
        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
   617
        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
   618
        # 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
   619
        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
   620
        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
   621
        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
   622
                       '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
   623
        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
   624
        # 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
   625
        # 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
   626
        # 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
   627
        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
   628
            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
   629
                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
   630
                               '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
   631
                               % 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
   632
                               {'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
   633
                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
   634
                    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
   635
                    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
   636
                    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
   637
                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
   638
                    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
   639
                                   '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
   640
                                   '       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
   641
                                   % (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
   642
                                      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
   643
                                   {'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
   644
        elif lastrel:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   645
            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
   646
        else:
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   647
            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
   648
                           '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
   649
                           '       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
   650
                           ' 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
   651
                           '       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
   652
                           % rschema,
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   653
                           {'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
   654
        # 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
   655
        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
   656
            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
   657
        # 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
   658
        # relation type
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   659
        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
   660
            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
   661
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
    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
   663
        # 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
   664
        #
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
   665
        # 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
   666
        # 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
   667
        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
   668
        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
   669
        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
   670
            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
   671
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
   672
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
class RDefUpdateOp(MemSchemaOperation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   674
    """actually update some properties of a relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   675
    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
   676
    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
   677
    indexed_changed = null_allowed_changed = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   678
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   679
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   680
        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
   681
        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
   682
        # update the in-memory schema first
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   683
        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
   684
        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
   685
        # 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
   686
        syssource = cnx.repo.system_source
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   687
        if 'indexed' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   688
            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
   689
            self.indexed_changed = True
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   690
        if ('cardinality' in self.values and rdef.rtype.final
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   691
                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
   692
            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
   693
            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
   694
        if 'fulltextindexed' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   695
            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
   696
        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
   697
            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
   698
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
   699
    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
   700
        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
   701
            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
   702
        # 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
   703
        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
   704
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   705
        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
   706
        if self.indexed_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   707
            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
   708
        if self.null_allowed_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   709
            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
   710
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
   711
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
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
   713
    # 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
   714
    # 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
   715
    # 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
   716
    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
   717
        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
   718
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
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
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
   721
    """actually remove a constraint of a relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   722
    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
   723
    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
   724
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
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   726
        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
   727
        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
   728
        # 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
   729
        _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
   730
        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
   731
            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
   732
        else:
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   733
            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
   734
                          self.oldcstr, rdef)
9888
5b48dcc02be1 [hooks/syncschema] avoid altering a dropped table
Julien Cristau <julien.cristau@logilab.fr>
parents: 9636
diff changeset
   735
        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
   736
            # 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
   737
            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
   738
        # 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
   739
        # constraint changes
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   740
        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
   741
        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
   742
        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
   743
            # 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
   744
            # 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
   745
            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
   746
                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
   747
                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
   748
                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
   749
                    return
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   750
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   751
            # 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
   752
            # not just replaced by another
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   753
            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
   754
            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
   755
        elif cstrtype == 'UniqueConstraint':
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   756
            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
   757
            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
   758
        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
   759
                          '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
   760
                          '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
   761
            cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s'
12512
661dd0436c01 [schema] drop monkeypatch "name_for" on BaseConstraint
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12025
diff changeset
   762
                           % (SQL_PREFIX, rdef.subject, constraint_name_for(self.oldcstr, 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
   763
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
    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
   765
        # 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
   766
        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
   767
            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
   768
        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
   769
            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
   770
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   771
        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
   772
        if self.size_cstr_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   773
            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
   774
        if self.unique_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   775
            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
   776
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
   777
11904
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
   778
class CWConstraintAddOp(hook.LateOperation, CWConstraintDelOp):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   779
    """actually update constraint of a relation definition"""
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   780
    entity = None  # make pylint happy
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   781
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   783
        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
   784
        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
   785
        rdef = self.rdef = cnx.vreg.schema.schema_by_eid(rdefentity.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   786
        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
   787
        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
   788
            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
   789
        else:
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10799
diff changeset
   790
            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
   791
        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
   792
        # 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
   793
        _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
   794
        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
   795
        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
   796
            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
   797
        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
   798
        # 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
   799
        # constraint changes
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   800
        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
   801
        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
   802
                                             oldcstr.max != newcstr.max):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   803
            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
   804
            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
   805
        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
   806
            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
   807
            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
   808
        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
   809
                        '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
   810
                        '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
   811
            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
   812
                                                     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
   813
            # 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
   814
            # 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
   815
            if oldcstr is not None:
12512
661dd0436c01 [schema] drop monkeypatch "name_for" on BaseConstraint
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12025
diff changeset
   816
                oldcstrname = constraint_name_for(self.oldcstr, 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
   817
                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
   818
                    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
   819
                                   % (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
   820
            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
   821
                           (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
   822
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
   823
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   824
class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   825
    entity = None  # make pylint happy
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   826
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   827
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   828
        cnx = self.cnx
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   829
        prefix = SQL_PREFIX
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   830
        entity = self.entity
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   831
        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
   832
        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
   833
        dbhelper = cnx.repo.system_source.dbhelper
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   834
        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
   835
        for sql in sqls:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   836
            cnx.system_sql(sql)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   837
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   838
    def postcommit_event(self):
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   839
        entity = self.entity
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   840
        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
   841
        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
   842
        eschema._unique_together.append(attrs)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   843
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
   844
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   845
class CWUniqueTogetherConstraintDelOp(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   846
    entity = cstrname = None  # make pylint happy
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   847
    cols = ()  # make pylint happy
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   848
10586
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   849
    def insert_index(self):
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   850
        # 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
   851
        # 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
   852
        # 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
   853
        # 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
   854
        return 0
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   855
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   856
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   857
        cnx = self.cnx
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   858
        prefix = SQL_PREFIX
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   859
        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
   860
        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
   861
        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
   862
        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
   863
        for sql in sqls:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   864
            cnx.system_sql(sql)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   865
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   866
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   867
        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
   868
        cols = set(self.cols)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   869
        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
   870
                           if set(ut) != cols]
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   871
        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
   872
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
   873
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
   874
# 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
   875
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
   876
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
   877
    """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
   878
    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
   879
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
   880
    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
   881
        # 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
   882
        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
   883
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
   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
class MemSchemaCWRTypeAdd(MemSchemaOperation):
11904
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
   886
    """Revert addition of the relation type from the instance's schema if something goes wrong.
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
   887
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   888
    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
   889
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
   890
    def revertprecommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   891
        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
   892
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
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
   894
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
   895
    """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
   896
    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
   897
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
   898
    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
   899
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   900
            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
   901
        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
   902
            # 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
   903
            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
   904
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
   905
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   906
class MemSchemaPermissionAdd(MemSchemaOperation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   907
    """synchronize schema when a *_permission relation has been added on a group
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   908
    """
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   909
    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
   910
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
   911
    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
   912
        """the observed connections.cnxset has been commited"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   913
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   914
            erschema = self.cnx.vreg.schema.schema_by_eid(self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   915
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   916
            # 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
   917
            self.warning('no schema for %s', self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   918
            return
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   919
        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
   920
        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
   921
            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
   922
        else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   923
            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
   924
        try:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   925
            perms.index(perm)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   926
            self.warning('%s already in permissions for %s on %s',
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   927
                         perm, self.action, erschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   928
        except ValueError:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   929
            perms.append(perm)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   930
            erschema.set_action_permissions(self.action, perms)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   931
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
   932
    # 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
   933
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
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   935
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
   936
    """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
   937
    group
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   938
    """
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   939
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
   940
    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
   941
        """the observed connections set has been commited"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   942
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   943
            erschema = self.cnx.vreg.schema.schema_by_eid(self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   944
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   945
            # 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
   946
            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
   947
            return
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   948
        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
   949
        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
   950
            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
   951
        else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   952
            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
   953
        try:
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
   954
            perms.remove(perm)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   955
            erschema.set_action_permissions(self.action, perms)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   956
        except ValueError:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   957
            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
   958
                       perm, self.action, erschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   959
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
   960
    # 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
   961
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   962
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
class MemSchemaSpecializesAdd(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   964
    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
   965
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
   966
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   967
        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
   968
        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
   969
        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
   970
        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
   971
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
   972
    # 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
   973
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
   974
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
class MemSchemaSpecializesDel(MemSchemaOperation):
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
   976
    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
   977
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
   978
    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
   979
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   980
            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
   981
            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
   982
        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
   983
            # 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
   984
            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
   985
        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
   986
        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
   987
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
   988
    # 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
   989
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
   990
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
   991
# 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
   992
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   993
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
   994
    """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
   995
    * 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
   996
    * 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
   997
    * 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
   998
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   999
    __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
  1000
    __select__ = SyncSchemaHook.__select__ & is_instance('CWEType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1001
    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
  1002
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1003
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1004
        # 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
  1005
        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
  1006
        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
  1007
            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
  1008
        # 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
  1009
        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
  1010
            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
  1011
        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
  1012
            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
  1013
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
  1014
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1015
class AfterDelCWETypeHook(DelCWETypeHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1016
    __regid__ = 'wfcleanup'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1017
    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
  1018
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1019
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1020
        # 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
  1021
        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
  1022
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
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1024
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
  1025
    """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
  1026
    * 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
  1027
    * 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
  1028
      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
  1029
    * 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
  1030
    * 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
  1031
      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
  1032
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1033
    __regid__ = 'syncaddcwetype'
3402
434946bb5356 should be a after_add hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3400
diff changeset
  1034
    events = ('after_add_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1035
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1036
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1037
        entity = self.entity
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1038
        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
  1039
            # 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
  1040
            # systematically added by yams at schema initialization time so
6e42b595e5df rephrase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7181
diff changeset
  1041
            # 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
  1042
            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
  1043
            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
  1044
        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
  1045
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
  1046
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1047
class BeforeUpdateCWETypeHook(DelCWETypeHook):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1048
    """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
  1049
    __regid__ = 'syncupdatecwetype'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1050
    events = ('before_update_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1051
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1052
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1053
        entity = self.entity
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1054
        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
  1055
        # 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
  1056
        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
  1057
            oldname, newname = entity.cw_edited.oldnewvalue('name')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1058
            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
  1059
                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
  1060
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
  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
# 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
  1063
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1064
class DelCWRTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1065
    """before deleting a CWRType entity:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1066
    * 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
  1067
    * cascade to delete related CWAttribute and CWRelation entities
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1068
    * 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
  1069
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1070
    __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
  1071
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1072
    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
  1073
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1074
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1075
        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
  1076
        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
  1077
            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
  1078
        # 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
  1079
        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
  1080
                         {'x': self.entity.eid})
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1081
        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
  1082
                         {'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
  1083
        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
  1084
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
  1085
11903
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1086
class AfterAddCWRTypeHook(SyncSchemaHook):
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1087
    """After a CWRType entity has been added, register an operation to add the
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1088
    relation type to the instance's schema on commit.
9963
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1089
11903
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1090
    We don't know yet at this point if a table is necessary, it will depend on
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1091
    further addition of relation definitions.
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
  1092
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1093
    __regid__ = 'syncaddcwrtype'
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1094
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1095
    events = ('after_add_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1096
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1097
    def __call__(self):
11903
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1098
        rtypedef = self.rtype_def()
11904
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
  1099
        # modify the instance's schema now since we'll usually need the type definition to do
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
  1100
        # further thing (e.g. add relation def of this type) but register and operation to revert
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
  1101
        # this if necessary
e760c54490b1 [migration] Fix addition of entity type including boundary constraints on its own attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11903
diff changeset
  1102
        self._cw.vreg.schema.add_relation_type(rtypedef)
11903
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1103
        MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1104
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1105
    def rtype_def(self):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1106
        entity = self.entity
11903
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1107
        return ybo.RelationType(name=entity.name,
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1108
                                description=entity.description,
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1109
                                inlined=entity.cw_edited.get('inlined', False),
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1110
                                symmetric=entity.cw_edited.get('symmetric', False),
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1111
                                eid=entity.eid)
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1112
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1113
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1114
class AfterAddCWComputedRTypeHook(AfterAddCWRTypeHook):
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1115
    """After a CWComputedRType entity has been added, register an operation to
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1116
    add the relation type to the instance's schema on commit.
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1117
    """
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1118
    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1119
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1120
    def rtype_def(self):
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1121
        entity = self.entity
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1122
        return ybo.ComputedRelation(name=entity.name,
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1123
                                    eid=entity.eid,
6f36275a6e74 [schema sync] Refactor AfterAddCWRTypeHook and AfterAddCWComputedRTypeHook so the latter inherit from the former
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11807
diff changeset
  1124
                                    rule=entity.rule)
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
  1125
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
  1126
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1127
class BeforeUpdateCWRTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1128
    """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
  1129
    __regid__ = 'syncupdatecwrtype'
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1130
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1131
    events = ('before_update_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1132
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1133
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1134
        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
  1135
        check_valid_changes(self._cw, entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1136
        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
  1137
        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
  1138
            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
  1139
                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
  1140
                if old != new:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1141
                    newvalues[prop] = new
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1142
        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
  1143
            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
  1144
            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
  1145
                            values=newvalues)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1146
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
  1147
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1148
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
  1149
    """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
  1150
    __regid__ = 'syncupdatecwcomputedrtype'
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1151
    __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
  1152
    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
  1153
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1154
    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
  1155
        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
  1156
        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
  1157
        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
  1158
            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
  1159
            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
  1160
                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
  1161
                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
  1162
                                        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
  1163
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1164
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
  1165
class AfterDelRelationTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1166
    """before deleting a CWAttribute or CWRelation entity:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1167
    * 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
  1168
      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
  1169
      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
  1170
      table
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1171
    * 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
  1172
    * delete the associated relation type when necessary
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1173
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1174
    __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
  1175
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('relation_type')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1176
    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
  1177
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1178
    def __call__(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1179
        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
  1180
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1181
            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
  1182
        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
  1183
            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
  1184
            return
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
  1185
        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
  1186
        pendingrdefs = cnx.transaction_data.setdefault('pendingrdefs', set())
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1187
        # first delete existing relation if necessary
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689 3659
diff changeset
  1188
        if rschema.final:
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3550 3536
diff changeset
  1189
            pendingrdefs.add((subjschema, rschema))
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1190
        else:
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3550 3536
diff changeset
  1191
            pendingrdefs.add((subjschema, rschema, objschema))
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1192
        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
  1193
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
  1194
9962
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1195
# CWComputedRType hooks #######################################################
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1196
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1197
class DelCWComputedRTypeHook(SyncSchemaHook):
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1198
    """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
  1199
    * 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
  1200
    * 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
  1201
    """
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1202
    __regid__ = 'syncdelcwcomputedrtype'
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1203
    __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
  1204
    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
  1205
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1206
    def __call__(self):
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1207
        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
  1208
        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
  1209
            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
  1210
        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
  1211
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1212
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
  1213
# 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
  1214
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1215
class AfterAddCWAttributeHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1216
    __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
  1217
    __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1218
    events = ('after_add_entity',)
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
    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
  1221
        CWAttributeAddOp(self._cw, entity=self.entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1222
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1223
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1224
class AfterAddCWRelationHook(AfterAddCWAttributeHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1225
    __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
  1226
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRelation')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1227
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1228
    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
  1229
        CWRelationAddOp(self._cw, entity=self.entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1230
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
  1231
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
  1232
class AfterUpdateCWRDefHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1233
    __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
  1234
    __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
  1235
                                                         '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
  1236
    events = ('before_update_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1237
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1238
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1239
        entity = self.entity
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1240
        if self._cw.deleted_in_transaction(entity.eid):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1241
            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
  1242
        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
  1243
        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
  1244
        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
  1245
            return
3400
93fc0070673a access to schema through vreg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
  1246
        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
  1247
        # 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
  1248
        # operation
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1249
        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
  1250
        for prop in RelationDefinitionSchema.rproperty_defs(objtype):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1251
            if prop == 'constraints':
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1252
                continue
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1253
            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
  1254
                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
  1255
            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
  1256
                attr = prop
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1257
            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
  1258
                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
  1259
                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
  1260
                    newvalues[prop] = new
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1261
        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
  1262
            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
  1263
                         values=newvalues)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1264
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
  1265
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
  1266
# 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
  1267
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
  1268
class AfterAddCWConstraintHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1269
    __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
  1270
    __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1271
    events = ('after_add_entity', 'after_update_entity')
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1272
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1273
    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
  1274
        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
  1275
            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
  1276
            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
  1277
                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
  1278
            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
  1279
        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
  1280
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
  1281
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
  1282
class AfterAddConstrainedByHook(SyncSchemaHook):
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1283
    __regid__ = 'syncaddconstrainedby'
3403
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3402
diff changeset
  1284
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('constrained_by')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1285
    events = ('after_add_relation',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1286
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1287
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1288
        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
  1289
            # 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
  1290
            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
  1291
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
  1292
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1293
class BeforeDeleteCWConstraintHook(SyncSchemaHook):
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1294
    __regid__ = 'syncdelcwconstraint'
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1295
    __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint')
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1296
    events = ('before_delete_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1297
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1298
    def __call__(self):
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1299
        entity = self.entity
3400
93fc0070673a access to schema through vreg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
  1300
        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
  1301
        try:
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1302
            # KeyError, e.g. composite chain deletion
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1303
            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
  1304
            # IndexError
9636
e35ae8617c03 Fix constraint sync during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
  1305
            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
  1306
        except (KeyError, IndexError):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1307
            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
  1308
        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
  1309
            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
  1310
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1311
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1312
# unique_together constraints
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1313
# 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
  1314
class AfterAddCWUniqueTogetherConstraintHook(SyncSchemaHook):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1315
    __regid__ = 'syncadd_cwuniquetogether_constraint'
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1316
    __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
  1317
    events = ('after_add_entity',)
6208
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
    def __call__(self):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1320
        CWUniqueTogetherConstraintAddOp(self._cw, entity=self.entity)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1321
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1322
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1323
class BeforeDeleteConstraintOfHook(SyncSchemaHook):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1324
    __regid__ = 'syncdelconstraintof'
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1325
    __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
  1326
    events = ('before_delete_relation',)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1327
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1328
    def __call__(self):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1329
        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
  1330
            return
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1331
        schema = self._cw.vreg.schema
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1332
        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
  1333
        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
  1334
        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
  1335
        CWUniqueTogetherConstraintDelOp(self._cw, entity=entity,
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
  1336
                                        cstrname=cstr.name, cols=cols)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1337
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
  1338
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
# 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
  1340
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
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
  1342
    """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
  1343
    __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
  1344
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype(
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1345
        'read_permission', 'add_permission', 'delete_permission',
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1346
        'update_permission')
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1347
    events = ('after_add_relation',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1348
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1349
    def __call__(self):
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1350
        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
  1351
        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
  1352
            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
  1353
                                   group_eid=self.eidto)
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1354
        else:  # RQLExpression
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1355
            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
  1356
            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
  1357
                                   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
  1358
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
  1359
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1360
class BeforeDelPermissionHook(AfterAddPermissionHook):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1361
    """delete entity/relation *_permission, need to update schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1362
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1363
    skip the operation if the related type is being deleted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1364
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1365
    __regid__ = 'syncdelperm'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1366
    events = ('before_delete_relation',)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1367
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1368
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1369
        if self._cw.deleted_in_transaction(self.eidfrom):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1370
            return
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1371
        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
  1372
        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
  1373
            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
  1374
                                   group_eid=self.eidto)
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11416
diff changeset
  1375
        else:  # RQLExpression
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1376
            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
  1377
            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
  1378
                                   expr=expr)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1379
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
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
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
  1382
    """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
  1383
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
  1384
    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
  1385
    the commit.
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1386
    """
10250
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
  1387
    containercls = list
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1388
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1389
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1390
        cnx = self.cnx
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1391
        source = cnx.repo.system_source
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1392
        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
  1393
        to_reindex = self.get_data()
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1394
        self.info('%i etypes need full text indexed reindexation',
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1395
                  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
  1396
        for etype in to_reindex:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1397
            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
  1398
            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
  1399
                      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
  1400
            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
  1401
            for entity in rset.entities():
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, [entity])
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
  1403
                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
  1404
                    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
  1405
                        source.fti_unindex_entities(cnx, [container])
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1406
                        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
  1407
        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
  1408
            # Transaction has already been committed
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1409
            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
  1410
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1411
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
  1412
# specializes synchronization hooks ############################################
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1413
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
class AfterAddSpecializesHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1415
    __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
  1416
    __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
  1417
    events = ('after_add_relation',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1418
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1419
    def __call__(self):
3404
9cb6b0cc2a7c fix some name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3403
diff changeset
  1420
        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
  1421
                                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
  1422
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
3410
47832374cf14 fix class name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3404
diff changeset
  1424
class AfterDelSpecializesHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1425
    __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
  1426
    __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
  1427
    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
  1428
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1429
    def __call__(self):
3404
9cb6b0cc2a7c fix some name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3403
diff changeset
  1430
        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
  1431
                                parentetypeeid=self.eidto)