cubicweb/hooks/syncschema.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 30 Jun 2016 13:25:43 +0200
changeset 11406 8ed625765a5c
parent 11362 ebe75d73acdd
child 11411 9a5a6ec5fc09
permissions -rw-r--r--
[schema2sql] Give a rdef to check_constraint This simplifies the API. Related to #14050899
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4951
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""schema hooks:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
- synchronize the living schema object with the persistent schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
- perform physical update on the source when necessary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
checking for schema consistency is done in hooks.py
5803
589e2e3fb997 [schema migration] don't try to drop column if backend doesn't support column alteration (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5801
diff changeset
    24
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
__docformat__ = "restructuredtext en"
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10663
diff changeset
    27
from cubicweb import _
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
11004
14ba505fb652 [migration] consider extra_props in rdef created during attribute creation hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10921
diff changeset
    29
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
    30
from copy import copy
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
    31
from hashlib import md5
11004
14ba505fb652 [migration] consider extra_props in rdef created during attribute creation hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10921
diff changeset
    32
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    33
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
    34
from yams.constraints import UniqueConstraint
10200
cceb2c7c02f4 Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10074
diff changeset
    35
from yams import buildobjs as ybo, convert_default_value
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
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
    37
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
    38
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
    39
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
    40
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
    41
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
    42
                             CONSTRAINTS, UNIQUE_CONSTRAINTS, ETYPE_NAME_MAP)
10200
cceb2c7c02f4 Use our version of schema2sql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10074
diff changeset
    43
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
    44
from cubicweb.server.sqlutils import SQL_PREFIX
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
    45
from cubicweb.server.schema2sql import unique_index_name
9969
0f64ef873f7a [CWEP002 migration] support add_relation_type/add_attribute for computed attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    46
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
    47
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
# 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
    49
CORE_TYPES = BASE_TYPES | SCHEMA_TYPES | META_RTYPES | set(
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
    50
    ('CWUser', 'CWGroup','login', 'upassword', 'name', 'in_group'))
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
    51
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    53
def get_constraints(cnx, entity):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    constraints = []
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    55
    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
    56
        cstrent = cnx.entity_from_eid(cstreid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        cstr = CONSTRAINTS[cstrent.type].deserialize(cstrent.value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        cstr.eid = cstreid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        constraints.append(cstr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    return constraints
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    62
4043
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    63
def group_mapping(cw):
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    64
    try:
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    65
        return cw.transaction_data['groupmap']
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    66
    except KeyError:
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    67
        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
    68
        return gmap
39ae94e0c8b8 give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4042
diff changeset
    69
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
    70
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    71
def add_inline_relation_column(cnx, etype, rtype):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    """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
    73
    attrkey = '%s.%s' % (etype, rtype)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    74
    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
    75
    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
    76
        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
    77
    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
    78
    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
    79
    column = SQL_PREFIX + rtype
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
    try:
10201
989bbadbcd8d Add foreign key for inline relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 10200
diff changeset
    81
        cnx.system_sql(str('ALTER TABLE %s ADD %s integer REFERENCES entities (eid)' % (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
    82
                       rollback_on_failure=False)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    83
        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
    84
    except Exception:
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    85
        # 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
    86
        # 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
    87
        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
    88
                      table, column)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    # create index before alter table which may expectingly fail during test
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    # (sqlite) while index creation should never fail (test for index existence
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    # is done by the dbhelper)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    92
    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
    93
    cnx.info('added index on %s(%s)', table, column)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    95
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
    96
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
    97
    # 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
    98
    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
    99
                  '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
   100
                  '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
   101
                  '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
   102
                  '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
   103
                  '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
   104
                  })
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   105
    cstrtypemap = ss.cstrtype_mapping(cnx)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   106
    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
   107
    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
   108
    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
   109
        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
   110
            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
   111
        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
   112
                                           object, None, values=props)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   113
        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
   114
                         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
   115
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
   116
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   117
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
   118
    errors = {}
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   119
    # 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
   120
    for attr in entity.cw_edited:
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   121
        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
   122
            origval, newval = entity.cw_edited.oldnewvalue(attr)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   123
            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
   124
                errors[attr] = _("can't change this attribute")
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   125
    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
   126
        raise validation_error(entity, errors)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   127
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   129
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
   130
    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
   131
        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
   132
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
   133
5891
99024ad59223 [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
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
   135
    """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
   136
    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
   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
    __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
   139
    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
   140
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   141
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   142
# 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
   143
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   144
class DropTable(hook.Operation):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2462
diff changeset
   145
    """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
   146
    table = None  # make pylint happy
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   147
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
    def precommit_event(self):
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   149
        dropped = self.cnx.transaction_data.setdefault('droppedtables', set())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        if self.table in dropped:
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   151
            return  # already processed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        dropped.add(self.table)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   153
        self.cnx.system_sql('DROP TABLE %s' % self.table)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        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
   155
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   156
    # 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
   157
2641
9c33d98a074e R [schema 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
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   159
class DropRelationTable(DropTable):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   160
    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
   161
        super(DropRelationTable, self).__init__(
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   162
            cnx, table='%s_relation' % rtype)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   163
        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
   164
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   165
10916
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   166
class DropColumn(hook.DataOperationMixIn, hook.Operation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    """actually remove the attribut's column from entity table in the system
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
    database
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
    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
   171
        cnx = self.cnx
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   172
        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
   173
            table = SQL_PREFIX + etype
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   174
            column = SQL_PREFIX + attr
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   175
            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
   176
            # drop index if any
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   177
            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
   178
            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
   179
                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
   180
                               rollback_on_failure=False)
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   181
                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
   182
            else:
8e504786df3c [hooks/syncschema] Turn DropColumnOp into a data operation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10816
diff changeset
   183
                # 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
   184
                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
   185
                           '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
   186
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   187
    # 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
   188
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
2641
9c33d98a074e R [schema 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
# base operations for in-memory schema synchronization  ########################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   192
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
   193
    """the update schema operation:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   195
    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
   196
    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
   197
    schema changes.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   200
    def __init__(self, cnx):
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   201
        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
   202
3774
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   203
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   204
        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
   205
            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
   206
                clear_cache(eschema, 'ordered_relations')
1eca47d59fd9 on precommit / rollback of schema update op, clear order_relations cache of entity schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   207
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   208
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   209
        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
   210
        # 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
   211
        # 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
   212
        try:
9588
fe267b7336f3 [migration] always rebuild infered relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9375
diff changeset
   213
            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
   214
            # trigger vreg reload
58b3b2d9c965 [repo] kill rebuildinfered feature from Repository.set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8821
diff changeset
   215
            repo.set_schema(repo.schema)
4591
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   216
            # CWUser class might have changed, update current session users
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   217
            cwuser_cls = self.cnx.vreg['etypes'].etype_class('CWUser')
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10646
diff changeset
   218
            for session in repo._sessions.values():
4591
47acae3cb778 don't let set_schema making commit_event fail
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4570
diff changeset
   219
                session.user.__class__ = cwuser_cls
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7562
diff changeset
   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
    """
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
   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
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   255
        tablesql = y2sql.eschema2sql(cnx.repo.system_source.dbhelper,
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   256
                                     eschema, prefix=SQL_PREFIX)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   257
        for sql in tablesql.split(';'):
99024ad59223 [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
            if sql.strip():
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   259
                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
   260
        # add meta relations
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   261
        gmap = group_mapping(cnx)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   262
        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
   263
        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
   264
            try:
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   265
                rschema = schema[rtype]
7551
2d4ba5b984dc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7504
diff changeset
   266
            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
   267
                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
   268
                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
   269
            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
   270
                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
   271
                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
   272
            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
   273
            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
   274
            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
   275
                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
   276
            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
   277
                # 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
   278
                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
   279
            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
   280
            mock = _MockEntity(eid=None)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   281
            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
   282
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   283
    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
   284
        # 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
   285
        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
   286
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   287
        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
   288
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   289
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   290
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
   291
    """this operation updates physical storage accordingly"""
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   292
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   293
    oldname = newname = None  # make pylint happy
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   295
    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
   296
        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
   297
        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
   298
        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
   299
        # 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
   300
        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
   301
        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
   302
        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
   303
        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
   304
        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
   305
        # 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
   306
        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
   307
            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
   308
                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
   309
            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
   310
                                     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
   311
                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
   312
        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
   313
        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
   314
        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
   315
        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
   316
                {'newname': newname, 'oldname': oldname})
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   317
        for eid, (etype, extid, auri) in cnx.repo._type_source_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
   318
            if etype == oldname:
11362
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   319
                cnx.repo._type_source_cache[eid] = (newname, extid, auri)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   320
        # 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
   321
        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
   322
            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
   323
                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
   324
            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
   325
                                     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
   326
                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
   327
        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
   328
            columns = ['%s%s' % (SQL_PREFIX, attr) for attr in attrs]
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   329
            old_index_name = unique_index_name(oldname, columns)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   330
            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
   331
                    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
   332
                sqlexec(sql)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   333
            new_index_name = unique_index_name(newname, columns)
ebe75d73acdd [schema sync] Rename index when an entity type is renamed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11359
diff changeset
   334
            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
   335
                    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
   336
                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
   337
        # 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
   338
99024ad59223 [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
    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
   340
        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
   341
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   342
    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
   343
        self.rename(self.newname, self.oldname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   346
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
   347
    """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
   348
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   349
    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
   350
    oldvalues = None
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   351
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   352
    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
   353
        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
   354
        if rschema.final:
11288
ab0a1863248e [server] some pep8 changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11106
diff changeset
   355
            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
   356
        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
   357
        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
   358
            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
   359
            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
   360
                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
   361
                    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
   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
                else:
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   364
                    op.add_data(objtype)
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
   365
                    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
   366
        # update the 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
   367
        self.oldvalues = dict( (attr, getattr(rschema, attr)) for attr in self.values)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   368
        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
   369
        # 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
   370
        if 'inlined' not in self.values:
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   371
            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
   372
        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
   373
        # 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
   374
        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
   375
            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
   376
        # 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
   377
        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
   378
        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
   379
        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
   380
        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
   381
            # 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
   382
            # another event of the same transaction
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   383
            if not rschema.type in cnx.transaction_data.get('createdtables', ()):
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
   384
                tablesql = y2sql.rschema2sql(rschema)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   385
                # 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
   386
                for sql in tablesql.split(';'):
9c33d98a074e R [schema 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
                    if sql.strip():
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   388
                        sqlexec(sql)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   389
                cnx.transaction_data.setdefault('createdtables', []).append(
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   390
                    rschema.type)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   391
            # copy existant data
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   392
            column = SQL_PREFIX + rtype
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   393
            for etype in rschema.subjects():
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   394
                table = SQL_PREFIX + str(etype)
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   395
                sqlexec('INSERT INTO %s_relation SELECT %s, %s FROM %s WHERE NOT %s IS NULL'
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   396
                        % (rtype, eidcolumn, column, table, column))
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   397
            # drop existant columns
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   398
            #if cnx.repo.system_source.dbhelper.alter_column_support:
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
   399
            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
   400
                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
   401
        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
   402
            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
   403
                try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   404
                    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
   405
                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
   406
                    # 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
   407
                    # 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
   408
                    # 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
   409
                    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
   410
                # 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
   411
                # 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
   412
                #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
   413
                #        '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
   414
                #        '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
   415
                #        % locals())
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   416
                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
   417
                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
   418
                                 '%(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
   419
                                 '%(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
   420
                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
   421
                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
   422
                    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
   423
                    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
   424
                                       % (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
   425
                # drop existant table
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   426
                DropRelationTable(cnx, rtype)
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
    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
   429
        # 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
   430
        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
   431
        # XXX revert changes on database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   433
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   434
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
   435
    """actually update some properties of a computed relation definition"""
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   436
    rschema = entity = rule = None # make pylint happy
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   437
    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
   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 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
   440
        # 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
   441
        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
   442
        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
   443
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   444
    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
   445
        # 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
   446
        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
   447
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
   448
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   449
class CWAttributeAddOp(MemSchemaOperation):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   450
    """an attribute relation (CWAttribute) has been added:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
    * add the necessary column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
    * set default on this column if any and possible
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
    * 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
   454
      instance's schema on commit
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   455
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
    constraints are handled by specific hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
    """
1138
22f634977c95 make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   458
    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
   459
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   460
    def init_rdef(self, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
        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
   462
        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
   463
        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
   464
            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
   465
            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
   466
            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
   467
            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
   468
        try:
3e5addc72575 Fix test migration crash waiting to happen due to inferred relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9949
diff changeset
   469
            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
   470
        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
   471
            # 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
   472
            # 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
   473
            # 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
   474
            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
   475
            assert rdef.infered
11005
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   476
        else:
f8417bd135ed [server, hooks] allow callable in dbh.TYPE_MAPPING
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11004
diff changeset
   477
            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
   478
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   479
        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
   480
                         '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
   481
                         '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
   482
                         {'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
   483
                          '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
   484
        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
   485
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   486
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   487
        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
   488
        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
   489
        # 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
   490
        # value
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
        default = entity.defaultval
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
        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
   493
            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
   494
        props = {'default': default,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   495
                 'indexed': entity.indexed,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   496
                 'fulltextindexed': entity.fulltextindexed,
dfb2ebb765e2 [migration] fix addition of attribute to a parent class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967
diff changeset
   497
                 '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
   498
        if entity.extra_props:
11038
7cb02ab4f321 [hooks] fix extra_props handling on python3
Julien Cristau <julien.cristau@logilab.fr>
parents: 11005
diff changeset
   499
            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
   500
        # 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
   501
        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
   502
            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
   503
        # 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
   504
        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
   505
        # 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
   506
        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
   507
        attrtype = y2sql.type_from_rdef(syssource.dbhelper, rdef)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
        # added some str() wrapping query since some backend (eg psycopg) don't
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
        # 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
   510
        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
   511
        column = SQL_PREFIX + rdefdef.name
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   513
            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
   514
                               % (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
   515
                           rollback_on_failure=False)
10035
c5bcdece77fb [hooks] Fix precommit event logging message
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9888
diff changeset
   516
            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
   517
        except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
            # the column probably already exists. this occurs when
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
            # the entity's type has just been added or if the column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
            # 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
   521
            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
   522
        if entity.indexed:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
            try:
11359
2da2dd60331c [sql gen] Explicitly name unique index
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11291
diff changeset
   524
                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
   525
            except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   526
                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
   527
                           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
   528
        # 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
   529
        schema = cnx.vreg.schema
3531
c095f5f54873 [migration] take care to entity being added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3526
diff changeset
   530
        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
   531
            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
   532
        except KeyError:
c095f5f54873 [migration] take care to entity being added
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3526
diff changeset
   533
            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
   534
        # 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
   535
        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
   536
        # 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
   537
        # 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
   538
        rschema.final = True
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   539
        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
   540
        # 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
   541
        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
   542
            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
   543
            cnx.system_sql('UPDATE %s SET %s=%%(default)s' % (table, column),
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
   544
                               {'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
   545
        # 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
   546
        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
   547
            # 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
   548
            # 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
   549
            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
   550
            RecomputeAttributeOperation.get_instance(cnx).add_data(rdefdef)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   552
    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
   553
        # 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
   554
        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
   555
            return
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   556
        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
   557
            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
   558
        # XXX revert changes on database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   559
5891
99024ad59223 [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
class CWRelationAddOp(CWAttributeAddOp):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   562
    """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
   563
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   564
    * 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
   565
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   566
    * 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
   567
      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
   568
      default permissions
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   569
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   570
    constraints are handled by specific hooks
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   571
    """
1138
22f634977c95 make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   572
    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
   573
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   575
        cnx = self.cnx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
        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
   577
        # 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
   578
        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
   579
        # 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
   580
        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
   581
        rtype = rdefdef.name
4722
9c13d5db03d9 pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   582
        rschema = schema.rschema(rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
        # this have to be done before permissions setting
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
        if rschema.inlined:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   585
            # 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
   586
            # 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
   587
            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
   588
                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
   589
            eschema = schema[rdefdef.subject]
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   590
            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
   591
                                      {'composite': entity.composite})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   592
        else:
6163
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   593
            if rschema.symmetric:
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   594
                # 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
   595
                # 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
   596
                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
   597
            else:
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   598
                relation_already_defined = len(rschema.rdefs) > 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   599
            # need to create the relation if no relation definition in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
            # 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
   601
            # transaction
6163
407f54345687 [migration] fix add_relation_type() for symmetric relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6097
diff changeset
   602
            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
   603
                    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
   604
                rschema = schema.rschema(rtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   605
                # create the necessary table
5891
99024ad59223 [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
                for sql in y2sql.rschema2sql(rschema).split(';'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   607
                    if sql.strip():
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   608
                        cnx.system_sql(sql)
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   609
                cnx.transaction_data.setdefault('createdtables', []).append(
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2056
diff changeset
   610
                    rtype)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   611
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   612
    # XXX revertprecommit_event
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   613
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   614
99024ad59223 [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
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
   616
    """an actual relation has been removed"""
99024ad59223 [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
    rdef = None # make pylint happy
99024ad59223 [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
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   619
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   620
        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
   621
        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
   622
        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
   623
        # 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
   624
        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
   625
        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
   626
        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
   627
                       '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
   628
        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
   629
        # 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
   630
        # 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
   631
        # 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
   632
        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
   633
            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
   634
                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
   635
                               '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
   636
                               % 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
   637
                               {'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
   638
                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
   639
                    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
   640
                    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
   641
                    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
   642
                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
   643
                    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
   644
                                   '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
   645
                                   '       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
   646
                                   % (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
   647
                                      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
   648
                                   {'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
   649
        elif lastrel:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   650
            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
   651
        else:
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   652
            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
   653
                           'EXISTS(SELECT 1 FROM entities '
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   654
                           '       WHERE eid=eid_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
   655
                           ' 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
   656
                           '       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
   657
                           % rschema,
977def81780a [migration] don't handle data deletion anymore on schema changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10920
diff changeset
   658
                           {'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
   659
        # 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
   660
        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
   661
            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
   662
        # 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
   663
        # relation type
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   664
        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
   665
            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
   666
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   667
    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
   668
        # 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
   669
        #
99024ad59223 [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
        # 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
   671
        # 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
   672
        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
   673
        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
   674
        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
   675
            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
   676
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   677
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   678
class RDefUpdateOp(MemSchemaOperation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   679
    """actually update some properties of a relation definition"""
5891
99024ad59223 [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
    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
   681
    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
   682
    indexed_changed = null_allowed_changed = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   685
        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
   686
        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
   687
        # update the 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
   688
        self.oldvalues = dict( (attr, getattr(rdef, attr)) for attr in 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
   689
        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
   690
        # 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
   691
        syssource = cnx.repo.system_source
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   692
        if 'indexed' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   693
            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
   694
            self.indexed_changed = True
10917
edea9c324396 [hooks/syncschema] don't set NOT NULL on inlined relation columns
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10916
diff changeset
   695
        if 'cardinality' in self.values and rdef.rtype.final \
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   696
              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
   697
            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
   698
            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
   699
        if 'fulltextindexed' in self.values:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   700
            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
   701
        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
   702
            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
   703
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   704
    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
   705
        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
   706
            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
   707
        # 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
   708
        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
   709
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   710
        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
   711
        if self.indexed_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   712
            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
   713
        if self.null_allowed_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   714
            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
   715
5891
99024ad59223 [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
99024ad59223 [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
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
   718
    # 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
   719
    # 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
   720
    # 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
   721
    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
   722
        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
   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
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   725
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
   726
    """actually remove a constraint of a relation definition"""
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   727
    rdef = oldcstr = newcstr = None # make pylint happy
99024ad59223 [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
    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
   729
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   730
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   731
        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
   732
        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
   733
        # 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
   734
        _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
   735
        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
   736
            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
   737
        else:
072429be2d95 [hooks/syncschema] do not crash on double removal of a constraint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8943
diff changeset
   738
            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
   739
                          self.oldcstr, rdef)
9888
5b48dcc02be1 [hooks/syncschema] avoid altering a dropped table
Julien Cristau <julien.cristau@logilab.fr>
parents: 9636
diff changeset
   740
        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
   741
            # 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
   742
            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
   743
        # 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
   744
        # constraint changes
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   745
        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
   746
        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
   747
        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
   748
            # 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
   749
            # 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
   750
            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
   751
                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
   752
                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
   753
                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
   754
                    return
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   755
80236876ee4d [hooks/syncschema] only call "ALTER TABLE" once when changing a size constraint
Aurelien Campeas <aurelien.campeas@pythonian.fr>
parents: 10035
diff changeset
   756
            # 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
   757
            # not just replaced by another
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   758
            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
   759
            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
   760
        elif cstrtype == 'UniqueConstraint':
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   761
            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
   762
            self.unique_changed = True
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   763
        if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
10816
d4a26e94f54a [syncschema] hashlib.md5() wants bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10813
diff changeset
   764
            cstrname = 'cstr' + md5((rdef.subject.type + rdef.rtype.type + cstrtype +
d4a26e94f54a [syncschema] hashlib.md5() wants bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10813
diff changeset
   765
                                     (self.oldcstr.serialize() or '')).encode('utf-8')).hexdigest()
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   766
            cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' % (SQL_PREFIX, rdef.subject.type, cstrname))
5891
99024ad59223 [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
99024ad59223 [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
    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
   769
        # 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
   770
        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
   771
            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
   772
        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
   773
            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
   774
        # revert changes on database
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   775
        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
   776
        if self.size_cstr_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   777
            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
   778
        if self.unique_changed:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   779
            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
   780
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   781
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   782
class CWConstraintAddOp(CWConstraintDelOp):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   783
    """actually update constraint of a relation definition"""
1138
22f634977c95 make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   784
    entity = None # make pylint happy
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   785
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   786
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   787
        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
   788
        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
   789
        rdef = self.rdef = cnx.vreg.schema.schema_by_eid(rdefentity.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   790
        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
   791
        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
   792
            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
   793
        else:
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10799
diff changeset
   794
            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
   795
        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
   796
        # 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
   797
        _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
   798
        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
   799
        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
   800
            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
   801
        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
   802
        # 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
   803
        # constraint changes
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   804
        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
   805
        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
   806
                                             oldcstr.max != newcstr.max):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   807
            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
   808
            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
   809
        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
   810
            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
   811
            self.unique_changed = True
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   812
        if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   813
            if oldcstr is not None:
10671
e08102f45237 [py3k] bytes required for hashlib.{md5,sha1}()
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
   814
                oldcstrname = 'cstr' + md5((rdef.subject.type + rdef.rtype.type + cstrtype +
e08102f45237 [py3k] bytes required for hashlib.{md5,sha1}()
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
   815
                                            (self.oldcstr.serialize() or '')).encode('ascii')).hexdigest()
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   816
                cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' %
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   817
                               (SQL_PREFIX, rdef.subject.type, oldcstrname))
11406
8ed625765a5c [schema2sql] Give a rdef to check_constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11362
diff changeset
   818
            cstrname, check = y2sql.check_constraint(rdef, newcstr, syssource.dbhelper,
8ed625765a5c [schema2sql] Give a rdef to check_constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11362
diff changeset
   819
                                                     prefix=SQL_PREFIX)
10445
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   820
            cnx.system_sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' %
f1773842077d [hooks/syncschema] drop/add check constraints as appropriate
Julien Cristau <julien.cristau@logilab.fr>
parents: 10250
diff changeset
   821
                           (SQL_PREFIX, rdef.subject.type, cstrname, check))
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   822
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
   823
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   824
class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   825
    entity = None # make pylint happy
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   826
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   827
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   828
        cnx = self.cnx
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   829
        prefix = SQL_PREFIX
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   830
        entity = self.entity
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   831
        table = '%s%s' % (prefix, entity.constraint_of[0].name)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   832
        cols = ['%s%s' % (prefix, r.name) for r in entity.relations]
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   833
        dbhelper = cnx.repo.system_source.dbhelper
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   834
        sqls = dbhelper.sqls_create_multicol_unique_index(table, cols, entity.name)
6294
a1535abe6ab2 use new logilab.database interface for multicol indexes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6261
diff changeset
   835
        for sql in sqls:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   836
            cnx.system_sql(sql)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   837
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   838
    def postcommit_event(self):
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   839
        entity = self.entity
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   840
        eschema = self.cnx.vreg.schema.schema_by_eid(entity.constraint_of[0].eid)
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   841
        attrs = [r.name for r in entity.relations]
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   842
        eschema._unique_together.append(attrs)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   843
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
   844
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   845
class CWUniqueTogetherConstraintDelOp(MemSchemaOperation):
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   846
    entity = cstrname = None # for pylint
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   847
    cols = () # for pylint
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   848
10586
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   849
    def insert_index(self):
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   850
        # We need to run before CWConstraintDelOp: if a size constraint is
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   851
        # removed and the column is part of a unique_together constraint, we
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   852
        # remove the unique_together index before changing the column's type.
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   853
        # SQL Server does not support unique indices on unlimited text columns.
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   854
        return 0
22f330f829ae [hooks/syncschema] make sure CWUniqueTogetherConstraintDelOp happens before CWConstraintDelOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10585
diff changeset
   855
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   856
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   857
        cnx = self.cnx
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   858
        prefix = SQL_PREFIX
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   859
        table = '%s%s' % (prefix, self.entity.type)
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   860
        dbhelper = cnx.repo.system_source.dbhelper
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   861
        cols = ['%s%s' % (prefix, c) for c in self.cols]
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
   862
        sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols, self.cstrname)
6294
a1535abe6ab2 use new logilab.database interface for multicol indexes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6261
diff changeset
   863
        for sql in sqls:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   864
            cnx.system_sql(sql)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   865
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   866
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   867
        eschema = self.cnx.vreg.schema.schema_by_eid(self.entity.eid)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   868
        cols = set(self.cols)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   869
        unique_together = [ut for ut in eschema._unique_together
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   870
                           if set(ut) != cols]
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
   871
        eschema._unique_together = unique_together
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   872
6815
a84190d4e78c [schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6789
diff changeset
   873
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   874
# operations for in-memory schema synchronization  #############################
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   875
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   876
class MemSchemaCWETypeDel(MemSchemaOperation):
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   877
    """actually remove the entity type from the instance's schema"""
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
    etype = None # make pylint happy
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   879
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   880
    def postcommit_event(self):
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   881
        # del_entity_type also removes entity's relations
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   882
        self.cnx.vreg.schema.del_entity_type(self.etype)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   883
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   884
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   885
class MemSchemaCWRTypeAdd(MemSchemaOperation):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   886
    """actually add the relation type to the instance's schema"""
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
   887
    rtypedef = None # make pylint happy
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 precommit_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.add_relation_type(self.rtypedef)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   891
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   892
    def revertprecommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   893
        self.cnx.vreg.schema.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
   894
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   895
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   896
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
   897
    """actually remove the relation type from the instance's schema"""
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
   898
    rtype = None # make pylint happy
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
   899
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   900
    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
   901
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   902
            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
   903
        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
   904
            # 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
   905
            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
   906
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
   907
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   908
class MemSchemaPermissionAdd(MemSchemaOperation):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   909
    """synchronize schema when a *_permission relation has been added on a group
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   910
    """
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
   911
    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
   912
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   913
    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
   914
        """the observed connections.cnxset has been commited"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   915
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   916
            erschema = self.cnx.vreg.schema.schema_by_eid(self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   917
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   918
            # 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
   919
            self.warning('no schema for %s', self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   920
            return
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   921
        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
   922
        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
   923
            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
   924
        else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   925
            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
   926
        try:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   927
            perms.index(perm)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   928
            self.warning('%s already in permissions for %s on %s',
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   929
                         perm, self.action, erschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   930
        except ValueError:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   931
            perms.append(perm)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   932
            erschema.set_action_permissions(self.action, perms)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   933
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   934
    # 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
   935
2641
9c33d98a074e R [schema 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
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   937
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
   938
    """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
   939
    group
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   940
    """
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   941
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   942
    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
   943
        """the observed connections set has been commited"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   944
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   945
            erschema = self.cnx.vreg.schema.schema_by_eid(self.eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   946
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   947
            # 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
   948
            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
   949
            return
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   950
        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
   951
        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
   952
            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
   953
        else:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   954
            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
   955
        try:
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
   956
            perms.remove(perm)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   957
            erschema.set_action_permissions(self.action, perms)
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   958
        except ValueError:
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3870
diff changeset
   959
            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
   960
                       perm, self.action, erschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   961
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   962
    # 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
   963
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
   964
2963
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   965
class MemSchemaSpecializesAdd(MemSchemaOperation):
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
   966
    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
   967
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   968
    def precommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   969
        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
   970
        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
   971
        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
   972
        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
   973
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   974
    # 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
   975
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
12ad88615a12 test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
   977
class MemSchemaSpecializesDel(MemSchemaOperation):
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
   978
    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
   979
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   980
    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
   981
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
   982
            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
   983
            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
   984
        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
   985
            # 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
   986
            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
   987
        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
   988
        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
   989
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
   990
    # 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
   991
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
   992
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
   993
# 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
   994
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   995
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
   996
    """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
   997
    * 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
   998
    * 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
   999
    * 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
  1000
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1001
    __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
  1002
    __select__ = SyncSchemaHook.__select__ & is_instance('CWEType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1003
    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
  1004
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1005
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1006
        # 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
  1007
        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
  1008
        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
  1009
            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
  1010
        # 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
  1011
        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
  1012
            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
  1013
        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
  1014
            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
  1015
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1016
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1017
class AfterDelCWETypeHook(DelCWETypeHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1018
    __regid__ = 'wfcleanup'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1019
    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
  1020
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1021
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1022
        # 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
  1023
        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
  1024
9c33d98a074e R [schema 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
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1026
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
  1027
    """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
  1028
    * 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
  1029
    * 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
  1030
      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
  1031
    * 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
  1032
    * 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
  1033
      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
  1034
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1035
    __regid__ = 'syncaddcwetype'
3402
434946bb5356 should be a after_add hook
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3400
diff changeset
  1036
    events = ('after_add_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1037
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1038
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1039
        entity = self.entity
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1040
        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
  1041
            # 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
  1042
            # systematically added by yams at schema initialization time so
6e42b595e5df rephrase
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7181
diff changeset
  1043
            # 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
  1044
            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
  1045
            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
  1046
        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
  1047
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1048
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1049
class BeforeUpdateCWETypeHook(DelCWETypeHook):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1050
    """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
  1051
    __regid__ = 'syncupdatecwetype'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1052
    events = ('before_update_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1053
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1054
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1055
        entity = self.entity
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1056
        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
  1057
        # 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
  1058
        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
  1059
            oldname, newname = entity.cw_edited.oldnewvalue('name')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1060
            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
  1061
                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
  1062
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
  1063
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1064
# 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
  1065
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1066
class DelCWRTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1067
    """before deleting a CWRType entity:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1068
    * 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
  1069
    * cascade to delete related CWAttribute and CWRelation entities
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1070
    * 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
  1071
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1072
    __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
  1073
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1074
    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
  1075
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1076
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1077
        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
  1078
        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
  1079
            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
  1080
        # 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
  1081
        self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1082
                        {'x': self.entity.eid})
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1083
        self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1084
                        {'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
  1085
        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
  1086
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1087
9963
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1088
class AfterAddCWComputedRTypeHook(SyncSchemaHook):
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1089
    """after a CWComputedRType entity has been added:
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1090
    * register an operation to add the relation type to the instance's
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1091
      schema on commit
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1092
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1093
    We don't know yet this point if a table is necessary
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1094
    """
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1095
    __regid__ = 'syncaddcwcomputedrtype'
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1096
    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1097
    events = ('after_add_entity',)
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1098
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1099
    def __call__(self):
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1100
        entity = self.entity
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1101
        rtypedef = ybo.ComputedRelation(name=entity.name,
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1102
                                        eid=entity.eid,
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1103
                                        rule=entity.rule)
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1104
        MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1105
5531f5577b50 [CWEP002 migration] support add_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9962
diff changeset
  1106
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1107
class AfterAddCWRTypeHook(SyncSchemaHook):
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1108
    """after a CWRType entity has been added:
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1109
    * register an operation to add the relation type to the instance's
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1110
      schema on commit
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1111
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1112
    We don't know yet this point if a table is necessary
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1113
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1114
    __regid__ = 'syncaddcwrtype'
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1115
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1116
    events = ('after_add_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1117
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1118
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1119
        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
  1120
        rtypedef = ybo.RelationType(name=entity.name,
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1121
                                    description=entity.description,
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1122
                                    inlined=entity.cw_edited.get('inlined', False),
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1123
                                    symmetric=entity.cw_edited.get('symmetric', False),
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1124
                                    eid=entity.eid)
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1125
        MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1126
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1127
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1128
class BeforeUpdateCWRTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1129
    """check name change, handle final"""
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1130
    __regid__ = 'syncupdatecwrtype'
9960
6359f3121f3f [hooks] do not abuse of inheritance for CWRType hooks
Laura Médioni <laura.medioni@logilab.fr>
parents: 9950
diff changeset
  1131
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1132
    events = ('before_update_entity',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1133
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1134
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1135
        entity = self.entity
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1136
        check_valid_changes(self._cw, entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1137
        newvalues = {}
4827
8a505bf12193 [hook] use entity instead of values dict, check for edited_attributes on rtype change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4826
diff changeset
  1138
        for prop in ('symmetric', 'inlined', 'fulltext_container'):
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1139
            if prop in entity.cw_edited:
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1140
                old, new = entity.cw_edited.oldnewvalue(prop)
4839
f482dbdf2f8c [schema hooks] properly check for changes of schema properties avoid useless costly operation (such as full-text reindexing all entities of a type). Refactor inlined handling and fix a bug trying to reindex a final entity type on the way.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4828
diff changeset
  1141
                if old != new:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1142
                    newvalues[prop] = new
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1143
        if newvalues:
4827
8a505bf12193 [hook] use entity instead of values dict, check for edited_attributes on rtype change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4826
diff changeset
  1144
            rschema = self._cw.vreg.schema.rschema(entity.name)
5891
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1145
            CWRTypeUpdateOp(self._cw, rschema=rschema, entity=entity,
99024ad59223 [schema migration] import refactoring to fix #1109558 and enhances things on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5881
diff changeset
  1146
                            values=newvalues)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1147
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1148
9964
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1149
class BeforeUpdateCWComputedRTypeHook(SyncSchemaHook):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1150
    """check name change, handle final"""
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1151
    __regid__ = 'syncupdatecwcomputedrtype'
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1152
    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1153
    events = ('before_update_entity',)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1154
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1155
    def __call__(self):
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1156
        entity = self.entity
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1157
        check_valid_changes(self._cw, entity)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1158
        if 'rule' in entity.cw_edited:
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1159
            old, new = entity.cw_edited.oldnewvalue('rule')
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1160
            if old != new:
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1161
                rschema = self._cw.vreg.schema.rschema(entity.name)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1162
                CWComputedRTypeUpdateOp(self._cw, rschema=rschema,
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1163
                                        entity=entity, rule=new)
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1164
f4a3ee05cf9d [CWEP002 migration] support sync_schema_props_perms for computed relations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9963
diff changeset
  1165
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1166
class AfterDelRelationTypeHook(SyncSchemaHook):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1167
    """before deleting a CWAttribute or CWRelation entity:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1168
    * if this is a final or inlined relation definition, instantiate an
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1169
      operation to drop necessary column, else if this is the last instance
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1170
      of a non final relation, instantiate an operation to drop necessary
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1171
      table
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1172
    * instantiate an operation to delete the relation definition on commit
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1173
    * delete the associated relation type when necessary
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1174
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1175
    __regid__ = 'syncdelrelationtype'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1176
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('relation_type')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1177
    events = ('after_delete_relation',)
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1178
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1179
    def __call__(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1180
        cnx = self._cw
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
  1181
        try:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1182
            rdef = cnx.vreg.schema.schema_by_eid(self.eidfrom)
5558
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
  1183
        except KeyError:
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
  1184
            self.critical('cant get schema rdef associated to %s', self.eidfrom)
afd1face1faf [schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
  1185
            return
4011
394f853bb653 [migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4003
diff changeset
  1186
        subjschema, rschema, objschema = rdef.as_triple()
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1187
        pendingrdefs = cnx.transaction_data.setdefault('pendingrdefs', set())
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1188
        # first delete existing relation if necessary
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689 3659
diff changeset
  1189
        if rschema.final:
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1190
            rdeftype = 'CWAttribute'
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3550 3536
diff changeset
  1191
            pendingrdefs.add((subjschema, rschema))
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1192
        else:
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1193
            rdeftype = 'CWRelation'
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3550 3536
diff changeset
  1194
            pendingrdefs.add((subjschema, rschema, objschema))
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1195
        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
  1196
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1197
9962
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1198
# CWComputedRType hooks #######################################################
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1199
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1200
class DelCWComputedRTypeHook(SyncSchemaHook):
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1201
    """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
  1202
    * 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
  1203
    * 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
  1204
    """
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1205
    __regid__ = 'syncdelcwcomputedrtype'
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1206
    __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
  1207
    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
  1208
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1209
    def __call__(self):
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1210
        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
  1211
        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
  1212
            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
  1213
        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
  1214
64b573d54133 [CWEP002 migration] support drop_relation_type for computed relations
Laura Médioni <laura.medioni@logilab.fr>
parents: 9960
diff changeset
  1215
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
  1216
# 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
  1217
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
  1218
class AfterAddCWAttributeHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1219
    __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
  1220
    __select__ = SyncSchemaHook.__select__ & is_instance('CWAttribute')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1221
    events = ('after_add_entity',)
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
    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
  1224
        CWAttributeAddOp(self._cw, entity=self.entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1225
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
class AfterAddCWRelationHook(AfterAddCWAttributeHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1228
    __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
  1229
    __select__ = SyncSchemaHook.__select__ & is_instance('CWRelation')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1230
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1231
    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
  1232
        CWRelationAddOp(self._cw, entity=self.entity)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1233
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1234
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
  1235
class AfterUpdateCWRDefHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1236
    __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
  1237
    __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
  1238
                                                         '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
  1239
    events = ('before_update_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1240
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1241
    def __call__(self):
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1242
        entity = self.entity
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1243
        if self._cw.deleted_in_transaction(entity.eid):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1244
            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
  1245
        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
  1246
        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
  1247
        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
  1248
            return
3400
93fc0070673a access to schema through vreg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
  1249
        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
  1250
        # 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
  1251
        # operation
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1252
        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
  1253
        for prop in RelationDefinitionSchema.rproperty_defs(objtype):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1254
            if prop == 'constraints':
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1255
                continue
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1256
            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
  1257
                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
  1258
            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
  1259
                attr = prop
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6097
diff changeset
  1260
            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
  1261
                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
  1262
                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
  1263
                    newvalues[prop] = new
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1264
        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
  1265
            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
  1266
                         values=newvalues)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1267
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1268
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1269
# 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
  1270
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
  1271
class AfterAddCWConstraintHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1272
    __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
  1273
    __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1274
    events = ('after_add_entity', 'after_update_entity')
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1275
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1276
    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
  1277
        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
  1278
            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
  1279
            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
  1280
                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
  1281
            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
  1282
        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
  1283
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1284
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
  1285
class AfterAddConstrainedByHook(SyncSchemaHook):
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1286
    __regid__ = 'syncaddconstrainedby'
3403
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3402
diff changeset
  1287
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype('constrained_by')
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1288
    events = ('after_add_relation',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1289
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1290
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1291
        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
  1292
            # 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
  1293
            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
  1294
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1295
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1296
class BeforeDeleteCWConstraintHook(SyncSchemaHook):
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1297
    __regid__ = 'syncdelcwconstraint'
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1298
    __select__ = SyncSchemaHook.__select__ & is_instance('CWConstraint')
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1299
    events = ('before_delete_entity',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1300
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1301
    def __call__(self):
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1302
        entity = self.entity
3400
93fc0070673a access to schema through vreg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
  1303
        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
  1304
        try:
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1305
            # KeyError, e.g. composite chain deletion
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
  1306
            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
  1307
            # IndexError
9636
e35ae8617c03 Fix constraint sync during migration
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
  1308
            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
  1309
        except (KeyError, IndexError):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1310
            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
  1311
        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
  1312
            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
  1313
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1314
# unique_together constraints
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1315
# 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
  1316
class AfterAddCWUniqueTogetherConstraintHook(SyncSchemaHook):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1317
    __regid__ = 'syncadd_cwuniquetogether_constraint'
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1318
    __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
  1319
    events = ('after_add_entity',)
6208
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
    def __call__(self):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1322
        CWUniqueTogetherConstraintAddOp(self._cw, entity=self.entity)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1323
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1324
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1325
class BeforeDeleteConstraintOfHook(SyncSchemaHook):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1326
    __regid__ = 'syncdelconstraintof'
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1327
    __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
  1328
    events = ('before_delete_relation',)
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1329
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1330
    def __call__(self):
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1331
        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
  1332
            return
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1333
        schema = self._cw.vreg.schema
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1334
        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
  1335
        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
  1336
        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
  1337
        CWUniqueTogetherConstraintDelOp(self._cw, entity=entity,
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9365
diff changeset
  1338
                                        cstrname=cstr.name, cols=cols)
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6163
diff changeset
  1339
2641
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1340
9c33d98a074e R [schema 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
# 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
  1342
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
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
  1344
    """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
  1345
    __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
  1346
    __select__ = SyncSchemaHook.__select__ & hook.match_rtype(
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1347
        'read_permission', 'add_permission', 'delete_permission',
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1348
        'update_permission')
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1349
    events = ('after_add_relation',)
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1350
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1351
    def __call__(self):
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1352
        action = self.rtype.split('_', 1)[0]
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9463
diff changeset
  1353
        if self._cw.entity_metas(self.eidto)['type'] == 'CWGroup':
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1354
            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
  1355
                                   group_eid=self.eidto)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1356
        else: # RQLExpression
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1357
            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
  1358
            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
  1359
                                   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
  1360
9c33d98a074e R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2624
diff changeset
  1361
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1362
class BeforeDelPermissionHook(AfterAddPermissionHook):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1363
    """delete entity/relation *_permission, need to update schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1364
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1365
    skip the operation if the related type is being deleted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1366
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1367
    __regid__ = 'syncdelperm'
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1368
    events = ('before_delete_relation',)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1369
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1370
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1371
        if self._cw.deleted_in_transaction(self.eidfrom):
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1372
            return
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1373
        action = self.rtype.split('_', 1)[0]
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9463
diff changeset
  1374
        if self._cw.entity_metas(self.eidto)['type'] == 'CWGroup':
3890
d7a270f50f54 backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3889 3777
diff changeset
  1375
            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
  1376
                                   group_eid=self.eidto)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1377
        else: # RQLExpression
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
  1378
            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
  1379
            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
  1380
                                   expr=expr)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1381
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1382
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1383
6426
541659c39f6a [hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6333
diff changeset
  1384
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
  1385
    """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
  1386
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
  1387
    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
  1388
    the commit.
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1389
    """
10250
582301c2c8ca [hooks/syncschema] use a list instead of a set for UpdateFTIndexOp
Julien Cristau <julien.cristau@logilab.fr>
parents: 10201
diff changeset
  1390
    containercls = list
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1391
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1392
    def postcommit_event(self):
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1393
        cnx = self.cnx
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1394
        source = cnx.repo.system_source
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1395
        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
  1396
        to_reindex = self.get_data()
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1397
        self.info('%i etypes need full text indexed reindexation',
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
  1398
                  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
  1399
        for etype in to_reindex:
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1400
            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
  1401
            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
  1402
                      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
  1403
            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
  1404
            for entity in rset.entities():
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1405
                source.fti_unindex_entities(cnx, [entity])
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
  1406
                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
  1407
                    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
  1408
                        source.fti_unindex_entities(cnx, [container])
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1409
                        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
  1410
        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
  1411
            # Transaction has already been committed
9621
202c4797e365 [hooks/syncschema] work with connections not sessions
Julien Cristau <julien.cristau@logilab.fr>
parents: 9548
diff changeset
  1412
            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
  1413
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1414
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1415
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4624
diff changeset
  1416
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1417
# specializes synchronization hooks ############################################
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1418
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1419
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
class AfterAddSpecializesHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1421
    __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
  1422
    __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
  1423
    events = ('after_add_relation',)
2835
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1424
04034421b072 [hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2745
diff changeset
  1425
    def __call__(self):
3404
9cb6b0cc2a7c fix some name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3403
diff changeset
  1426
        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
  1427
                                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
  1428
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1429
3410
47832374cf14 fix class name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3404
diff changeset
  1430
class AfterDelSpecializesHook(SyncSchemaHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
  1431
    __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
  1432
    __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
  1433
    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
  1434
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2967 2902
diff changeset
  1435
    def __call__(self):
3404
9cb6b0cc2a7c fix some name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3403
diff changeset
  1436
        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
  1437
                                parentetypeeid=self.eidto)