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