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