cubicweb/misc/migration/3.24.4_Any.py
branch3.24
changeset 11895 74e0d1a5560b
child 11938 fc19dda111dc
equal deleted inserted replaced
11894:ec29989fba13 11895:74e0d1a5560b
       
     1 
       
     2 from yams.constraints import UniqueConstraint
       
     3 from cubicweb.schema import PURE_VIRTUAL_RTYPES
       
     4 from cubicweb.server.checkintegrity import expected_indexes, database_indexes
       
     5 
       
     6 source = repo.system_source
       
     7 
       
     8 for rschema in schema.relations():
       
     9     if rschema.rule or rschema in PURE_VIRTUAL_RTYPES:
       
    10         continue
       
    11     if rschema.final or rschema.inlined:
       
    12         for rdef in rschema.rdefs.values():
       
    13             table = 'cw_{0}'.format(rdef.subject)
       
    14             column = 'cw_{0}'.format(rdef.rtype)
       
    15             if any(isinstance(cstr, UniqueConstraint) for cstr in rdef.constraints):
       
    16                 source.create_index(cnx, table, column, unique=True)
       
    17                 commit()
       
    18             if rschema.inlined or rdef.indexed:
       
    19                 source.create_index(cnx, table, column)
       
    20                 commit()
       
    21 
       
    22 schema_indices = expected_indexes(cnx)
       
    23 db_indices = database_indexes(cnx)
       
    24 for additional_index in (db_indices - set(schema_indices)):
       
    25     try:
       
    26         sql('DROP INDEX %s' % additional_index)
       
    27         commit()
       
    28     except:
       
    29         # ignore if this is not an index but a constraint
       
    30         pass
       
    31 
       
    32 if source.dbhelper == 'postgres' and 'appears_words_idx' not in db_indices:
       
    33     sql('CREATE INDEX appears_words_idx ON appears USING gin(words)')
       
    34     db_indices.add('appears_words_idx')
       
    35 
       
    36 for missing_index in (set(schema_indices) - db_indices):
       
    37     print('WARNING: missing index', missing_index)
       
    38