misc/migration/3.18.0_Any.py
author Paul Tonelli <paul.tonelli@logilab.fr>
Tue, 29 Apr 2014 11:34:42 +0200
changeset 10010 3dd66bdbfe8e
parent 9635 aaf099172bb9
child 10061 ecbfec2da8a2
permissions -rw-r--r--
[serverctl] Ask for URL when adding a new source (closes #3824868)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9395
diff changeset
     1
driver = config.system_source_config['db-driver']
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
     2
if not (driver == 'postgres' or driver.startswith('sqlserver')):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
     3
    import sys
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
     4
    print >>sys.stderr, 'This migration is not supported for backends other than sqlserver or postgres (yet).'
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
     5
    sys.exit(1)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
     6
9407
34fccd9104bc [migration/3.18] Add add_permission relation definitions earlier
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9406
diff changeset
     7
add_relation_definition('CWAttribute', 'add_permission', 'CWGroup')
34fccd9104bc [migration/3.18] Add add_permission relation definitions earlier
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9406
diff changeset
     8
add_relation_definition('CWAttribute', 'add_permission', 'RQLExpression')
34fccd9104bc [migration/3.18] Add add_permission relation definitions earlier
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9406
diff changeset
     9
9414
d8c0784038f4 [migration/3.18] recover from bad CWSource.in_synchronization default value
Julien Cristau <julien.cristau@logilab.fr>
parents: 9410
diff changeset
    10
# a bad defaultval in 3.13.8 schema was fixed in 3.13.9, but the migration was missed
d8c0784038f4 [migration/3.18] recover from bad CWSource.in_synchronization default value
Julien Cristau <julien.cristau@logilab.fr>
parents: 9410
diff changeset
    11
rql('SET ATTR defaultval NULL WHERE ATTR from_entity E, E name "CWSource", ATTR relation_type T, T name "in_synchronization"')
d8c0784038f4 [migration/3.18] recover from bad CWSource.in_synchronization default value
Julien Cristau <julien.cristau@logilab.fr>
parents: 9410
diff changeset
    12
9410
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    13
# the migration gets confused when we change rdefs out from under it.  So
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    14
# explicitly remove this size constraint so it doesn't stick around and break
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    15
# things later.
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    16
rdefeid = schema['defaultval'].rdefs.values()[0].eid
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    17
rql('DELETE CWConstraint C WHERE C cstrtype T, T name "SizeConstraint", R constrained_by C, R eid %(eid)s', {'eid': rdefeid})
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    18
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:
diff changeset
    19
sync_schema_props_perms('defaultval')
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    20
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    21
def convert_defaultval(cwattr, default):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    22
    from decimal import Decimal
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    23
    import yams
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    24
    from cubicweb import Binary
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    25
    if default is 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:
diff changeset
    26
        return
9409
2f684e538174 [migration/3.18] Idempotency fixes
Julien Cristau <julien.cristau@logilab.fr>
parents: 9408
diff changeset
    27
    if isinstance(default, Binary):
2f684e538174 [migration/3.18] Idempotency fixes
Julien Cristau <julien.cristau@logilab.fr>
parents: 9408
diff changeset
    28
        # partially migrated instance, try to be idempotent
2f684e538174 [migration/3.18] Idempotency fixes
Julien Cristau <julien.cristau@logilab.fr>
parents: 9408
diff changeset
    29
        return default
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:
diff changeset
    30
    atype = cwattr.to_entity[0].name
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    31
    if atype == 'Boolean':
9360
eda5071e30a1 [migration] fix handling of default value for boolean attributes
Julien Cristau <julien.cristau@logilab.fr>
parents: 9300
diff changeset
    32
        # boolean attributes with default=False were stored as ''
eda5071e30a1 [migration] fix handling of default value for boolean attributes
Julien Cristau <julien.cristau@logilab.fr>
parents: 9300
diff changeset
    33
        assert default in ('True', 'False', ''), repr(default)
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:
diff changeset
    34
        default = default == 'True'
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    35
    elif atype in ('Int', 'BigInt'):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    36
        default = int(default)
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    37
    elif atype == 'Float':
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    38
        default = float(default)
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    39
    elif atype == 'Decimal':
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    40
        default = Decimal(default)
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    41
    elif atype in ('Date', 'Datetime', 'TZDatetime', 'Time'):
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    42
        try:
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    43
            # handle NOW and TODAY, keep them stored as strings
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    44
            yams.KEYWORD_MAP[atype][default.upper()]
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    45
            default = default.upper()
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    46
        except KeyError:
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    47
            # otherwise get an actual date or datetime
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    48
            default = yams.DATE_FACTORY_MAP[atype](default)
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    49
    else:
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    50
        assert atype == 'String', atype
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    51
        default = unicode(default)
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    52
    return Binary.zpickle(default)
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    53
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    54
dbh = repo.system_source.dbhelper
9300
5f10cd13224d defaultval migration for sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
    55
5f10cd13224d defaultval migration for sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
    56
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    57
sql('ALTER TABLE cw_cwattribute ADD new_defaultval %s' % dbh.TYPE_MAPPING['Bytes'])
9300
5f10cd13224d defaultval migration for sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
    58
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    59
for cwattr in rql('CWAttribute X').entities():
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    60
    olddefault = cwattr.defaultval
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    61
    if olddefault is not None:
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    62
        req = "UPDATE cw_cwattribute SET new_defaultval = %(val)s WHERE cw_eid = %(eid)s"
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    63
        args = {'val': dbh.binary_value(convert_defaultval(cwattr, olddefault).getvalue()), 'eid': cwattr.eid}
9300
5f10cd13224d defaultval migration for sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
    64
        sql(req, args, ask_confirm=False)
5f10cd13224d defaultval migration for sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
    65
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    66
sql('ALTER TABLE cw_cwattribute DROP COLUMN cw_defaultval')
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    67
if driver == 'postgres':
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    68
    sql('ALTER TABLE cw_cwattribute RENAME COLUMN new_defaultval TO cw_defaultval')
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    69
else: # sqlserver
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
    70
    sql("sp_rename 'cw_cwattribute.new_defaultval', 'cw_defaultval', 'COLUMN'")
9300
5f10cd13224d defaultval migration for sqlite
Julien Cristau <julien.cristau@logilab.fr>
parents: 9299
diff changeset
    71
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:
diff changeset
    72
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    73
# Set object type to "Bytes" for CWAttribute's "defaultval" 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:
diff changeset
    74
rql('SET X to_entity B WHERE X is CWAttribute, X from_entity Y, Y name "CWAttribute", '
9409
2f684e538174 [migration/3.18] Idempotency fixes
Julien Cristau <julien.cristau@logilab.fr>
parents: 9408
diff changeset
    75
    'X relation_type Z, Z name "defaultval", B name "Bytes", NOT X to_entity B')
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:
diff changeset
    76
9589
7e4897901c64 [migration] Improve update of in-memory schema during 3.18 CWAttribute.defaultval change
Julien Cristau <julien.cristau@logilab.fr>
parents: 9477
diff changeset
    77
oldrdef = schema['CWAttribute'].rdef('defaultval')
7e4897901c64 [migration] Improve update of in-memory schema during 3.18 CWAttribute.defaultval change
Julien Cristau <julien.cristau@logilab.fr>
parents: 9477
diff changeset
    78
import yams.buildobjs as ybo
7e4897901c64 [migration] Improve update of in-memory schema during 3.18 CWAttribute.defaultval change
Julien Cristau <julien.cristau@logilab.fr>
parents: 9477
diff changeset
    79
newrdef = ybo.RelationDefinition('CWAttribute', 'defaultval', 'Bytes')
7e4897901c64 [migration] Improve update of in-memory schema during 3.18 CWAttribute.defaultval change
Julien Cristau <julien.cristau@logilab.fr>
parents: 9477
diff changeset
    80
newrdef.eid = oldrdef.eid
7e4897901c64 [migration] Improve update of in-memory schema during 3.18 CWAttribute.defaultval change
Julien Cristau <julien.cristau@logilab.fr>
parents: 9477
diff changeset
    81
schema.add_relation_def(newrdef)
7e4897901c64 [migration] Improve update of in-memory schema during 3.18 CWAttribute.defaultval change
Julien Cristau <julien.cristau@logilab.fr>
parents: 9477
diff changeset
    82
schema.del_relation_def('CWAttribute', 'defaultval', 'String')
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:
diff changeset
    83
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents:
diff changeset
    84
commit()
9361
0542a85fe667 symmetric relations: replace bogus rql2sql translation by a hook
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9360
diff changeset
    85
9410
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    86
sync_schema_props_perms('defaultval')
ce072c9aa573 [migration/3.18] Explicitly delete constraint on defaultval
Julien Cristau <julien.cristau@logilab.fr>
parents: 9409
diff changeset
    87
9361
0542a85fe667 symmetric relations: replace bogus rql2sql translation by a hook
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9360
diff changeset
    88
for rschema in schema.relations():
0542a85fe667 symmetric relations: replace bogus rql2sql translation by a hook
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9360
diff changeset
    89
    if rschema.symmetric:
9370
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    90
        subjects = set(repr(e.type) for e in rschema.subjects())
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    91
        objects = set(repr(e.type) for e in rschema.objects())
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    92
        assert subjects == objects
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    93
        martians = set(str(eid) for eid, in sql('SELECT eid_to FROM %s_relation, entities WHERE eid_to = eid AND type NOT IN (%s)' %
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    94
                                           (rschema.type, ','.join(subjects))))
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    95
        martians |= set(str(eid) for eid, in sql('SELECT eid_from FROM %s_relation, entities WHERE eid_from = eid AND type NOT IN (%s)' %
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    96
                                            (rschema.type, ','.join(subjects))))
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    97
        if martians:
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    98
            martians = ','.join(martians)
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
    99
            print 'deleting broken relations %s for eids %s' % (rschema.type, martians)
15c695d8d865 [migration/3.18] add sanity checks before changing symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9369
diff changeset
   100
            sql('DELETE FROM %s_relation WHERE eid_from IN (%s) OR eid_to IN (%s)' % (rschema.type, martians, martians))
9369
176c1edf51b0 [migration/3.18] disable all hooks when setting up symmetric relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 9361
diff changeset
   101
        with session.deny_all_hooks_but():
9361
0542a85fe667 symmetric relations: replace bogus rql2sql translation by a hook
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9360
diff changeset
   102
            rql('SET X %(r)s Y WHERE Y %(r)s X, NOT X %(r)s Y' % {'r': rschema.type})
9406
38debfa8b536 [migration/3.18] Only commit when we did something
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9395
diff changeset
   103
        commit()
9375
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   104
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   105
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   106
# multi columns unique constraints regeneration
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   107
from cubicweb.server import schemaserial
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   108
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   109
# syncschema hooks would try to remove indices but
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   110
# 1) we already do that below
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   111
# 2) the hook expects the CWUniqueTogetherConstraint.name attribute that hasn't
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   112
#    yet been added
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   113
with session.allow_all_hooks_but('syncschema'):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   114
    rql('DELETE CWUniqueTogetherConstraint C')
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   115
commit()
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   116
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   117
add_attribute('CWUniqueTogetherConstraint', 'name')
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   118
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   119
# low-level wipe code for postgres & sqlserver, plain sql ...
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   120
if driver == 'postgres':
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   121
    for indexname, in sql('select indexname from pg_indexes'):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   122
        if indexname.startswith('unique_'):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   123
            print 'dropping index', indexname
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   124
            sql('DROP INDEX %s' % indexname)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   125
    commit()
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   126
elif driver.startswith('sqlserver'):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   127
    for viewname, in sql('select name from sys.views'):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   128
        if viewname.startswith('utv_'):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   129
            print 'dropping view (index should be cascade-deleted)', viewname
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   130
            sql('DROP VIEW %s' % viewname)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   131
    commit()
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   132
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   133
# recreate the constraints, hook will lead to low-level recreation
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   134
for eschema in sorted(schema.entities()):
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   135
    if eschema._unique_together:
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   136
        rql_args = schemaserial.uniquetogether2rqls(eschema)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   137
        for rql, args in rql_args:
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   138
            args['x'] = eschema.eid
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   139
            session.execute(rql, args)
8e88576787c3 [schema] fix unique together index handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9370
diff changeset
   140
        commit()
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9375
diff changeset
   141
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9375
diff changeset
   142
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9375
diff changeset
   143
# all attributes perms have to be refreshed ...
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9375
diff changeset
   144
for rschema in schema.relations():
9408
f11854169654 [migration/3.18] Fix NameError
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9407
diff changeset
   145
    if rschema.final:
9477
07fc991612b0 [migration/3.18] protect against attributes in db missing from fsschema
Julien Cristau <julien.cristau@logilab.fr>
parents: 9414
diff changeset
   146
        if rschema.type in fsschema:
07fc991612b0 [migration/3.18] protect against attributes in db missing from fsschema
Julien Cristau <julien.cristau@logilab.fr>
parents: 9414
diff changeset
   147
            sync_schema_props_perms(rschema.type, syncprops=False, ask_confirm=False)
07fc991612b0 [migration/3.18] protect against attributes in db missing from fsschema
Julien Cristau <julien.cristau@logilab.fr>
parents: 9414
diff changeset
   148
        else:
07fc991612b0 [migration/3.18] protect against attributes in db missing from fsschema
Julien Cristau <julien.cristau@logilab.fr>
parents: 9414
diff changeset
   149
            print 'WARNING: attribute %s missing from fs schema' % rschema.type