misc/migration/bootstrapmigration_repository.py
changeset 10589 7c23b7de2b8d
parent 10553 1d824df4f2bd
child 10663 54b8a1f249fb
equal deleted inserted replaced
10588:fdaa0e4b7eaf 10589:7c23b7de2b8d
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """allways executed before all others in server migration
    18 """allways executed before all others in server migration
    19 
    19 
    20 it should only include low level schema changes
    20 it should only include low level schema changes
    21 """
    21 """
       
    22 from __future__ import print_function
    22 
    23 
    23 from cubicweb import ConfigurationError
    24 from cubicweb import ConfigurationError
    24 from cubicweb.server.session import hooks_control
    25 from cubicweb.server.session import hooks_control
    25 from cubicweb.server import schemaserial as ss
    26 from cubicweb.server import schemaserial as ss
    26 
    27 
    75         # need explicit drop of the indexes on some database systems (sqlserver)
    76         # need explicit drop of the indexes on some database systems (sqlserver)
    76         sql(repo.system_source.dbhelper.sql_drop_index('entities', 'mtime'))
    77         sql(repo.system_source.dbhelper.sql_drop_index('entities', 'mtime'))
    77         sql('ALTER TABLE "entities" DROP COLUMN "mtime"')
    78         sql('ALTER TABLE "entities" DROP COLUMN "mtime"')
    78         sql('ALTER TABLE "entities" DROP COLUMN "source"')
    79         sql('ALTER TABLE "entities" DROP COLUMN "source"')
    79     except: # programming error, already migrated
    80     except: # programming error, already migrated
    80         print "Failed to drop mtime or source database columns"
    81         print("Failed to drop mtime or source database columns")
    81         print "'entities' table of the database has probably been already updated"
    82         print("'entities' table of the database has probably been already updated")
    82 
    83 
    83     commit()
    84     commit()
    84 
    85 
    85     replace_eid_sequence_with_eid_numrange(session)
    86     replace_eid_sequence_with_eid_numrange(session)
    86 
    87 
    99 
   100 
   100 if applcubicwebversion < (3, 18, 0) and cubicwebversion >= (3, 18, 0):
   101 if applcubicwebversion < (3, 18, 0) and cubicwebversion >= (3, 18, 0):
   101     driver = config.system_source_config['db-driver']
   102     driver = config.system_source_config['db-driver']
   102     if not (driver == 'postgres' or driver.startswith('sqlserver')):
   103     if not (driver == 'postgres' or driver.startswith('sqlserver')):
   103         import sys
   104         import sys
   104         print >>sys.stderr, 'This migration is not supported for backends other than sqlserver or postgres (yet).'
   105         print('This migration is not supported for backends other than sqlserver or postgres (yet).', file=sys.stderr)
   105         sys.exit(1)
   106         sys.exit(1)
   106 
   107 
   107     add_relation_definition('CWAttribute', 'add_permission', 'CWGroup')
   108     add_relation_definition('CWAttribute', 'add_permission', 'CWGroup')
   108     add_relation_definition('CWAttribute', 'add_permission', 'RQLExpression')
   109     add_relation_definition('CWAttribute', 'add_permission', 'RQLExpression')
   109 
   110 
   194                                                (rschema.type, ','.join(subjects))))
   195                                                (rschema.type, ','.join(subjects))))
   195             martians |= set(str(eid) for eid, in sql('SELECT eid_from FROM %s_relation, entities WHERE eid_from = eid AND type NOT IN (%s)' %
   196             martians |= set(str(eid) for eid, in sql('SELECT eid_from FROM %s_relation, entities WHERE eid_from = eid AND type NOT IN (%s)' %
   196                                                 (rschema.type, ','.join(subjects))))
   197                                                 (rschema.type, ','.join(subjects))))
   197             if martians:
   198             if martians:
   198                 martians = ','.join(martians)
   199                 martians = ','.join(martians)
   199                 print 'deleting broken relations %s for eids %s' % (rschema.type, martians)
   200                 print('deleting broken relations %s for eids %s' % (rschema.type, martians))
   200                 sql('DELETE FROM %s_relation WHERE eid_from IN (%s) OR eid_to IN (%s)' % (rschema.type, martians, martians))
   201                 sql('DELETE FROM %s_relation WHERE eid_from IN (%s) OR eid_to IN (%s)' % (rschema.type, martians, martians))
   201             with session.deny_all_hooks_but():
   202             with session.deny_all_hooks_but():
   202                 rql('SET X %(r)s Y WHERE Y %(r)s X, NOT X %(r)s Y' % {'r': rschema.type})
   203                 rql('SET X %(r)s Y WHERE Y %(r)s X, NOT X %(r)s Y' % {'r': rschema.type})
   203             commit()
   204             commit()
   204 
   205 
   217 
   218 
   218     # low-level wipe code for postgres & sqlserver, plain sql ...
   219     # low-level wipe code for postgres & sqlserver, plain sql ...
   219     if driver == 'postgres':
   220     if driver == 'postgres':
   220         for indexname, in sql('select indexname from pg_indexes'):
   221         for indexname, in sql('select indexname from pg_indexes'):
   221             if indexname.startswith('unique_'):
   222             if indexname.startswith('unique_'):
   222                 print 'dropping index', indexname
   223                 print('dropping index', indexname)
   223                 sql('DROP INDEX %s' % indexname)
   224                 sql('DROP INDEX %s' % indexname)
   224         commit()
   225         commit()
   225     elif driver.startswith('sqlserver'):
   226     elif driver.startswith('sqlserver'):
   226         for viewname, in sql('select name from sys.views'):
   227         for viewname, in sql('select name from sys.views'):
   227             if viewname.startswith('utv_'):
   228             if viewname.startswith('utv_'):
   228                 print 'dropping view (index should be cascade-deleted)', viewname
   229                 print('dropping view (index should be cascade-deleted)', viewname)
   229                 sql('DROP VIEW %s' % viewname)
   230                 sql('DROP VIEW %s' % viewname)
   230         commit()
   231         commit()
   231 
   232 
   232     # recreate the constraints, hook will lead to low-level recreation
   233     # recreate the constraints, hook will lead to low-level recreation
   233     for eschema in sorted(schema.entities()):
   234     for eschema in sorted(schema.entities()):
   234         if eschema._unique_together:
   235         if eschema._unique_together:
   235             print 'recreate unique indexes for', eschema
   236             print('recreate unique indexes for', eschema)
   236             rql_args = schemaserial.uniquetogether2rqls(eschema)
   237             rql_args = schemaserial.uniquetogether2rqls(eschema)
   237             for rql, args in rql_args:
   238             for rql, args in rql_args:
   238                 args['x'] = eschema.eid
   239                 args['x'] = eschema.eid
   239                 session.execute(rql, args)
   240                 session.execute(rql, args)
   240     commit()
   241     commit()
   241 
   242 
   242     # all attributes perms have to be refreshed ...
   243     # all attributes perms have to be refreshed ...
   243     for rschema in sorted(schema.relations()):
   244     for rschema in sorted(schema.relations()):
   244         if rschema.final:
   245         if rschema.final:
   245             if rschema.type in fsschema:
   246             if rschema.type in fsschema:
   246                 print 'sync perms for', rschema.type
   247                 print('sync perms for', rschema.type)
   247                 sync_schema_props_perms(rschema.type, syncprops=False, ask_confirm=False, commit=False)
   248                 sync_schema_props_perms(rschema.type, syncprops=False, ask_confirm=False, commit=False)
   248             else:
   249             else:
   249                 print 'WARNING: attribute %s missing from fs schema' % rschema.type
   250                 print('WARNING: attribute %s missing from fs schema' % rschema.type)
   250     commit()
   251     commit()
   251 
   252 
   252 if applcubicwebversion < (3, 17, 0) and cubicwebversion >= (3, 17, 0):
   253 if applcubicwebversion < (3, 17, 0) and cubicwebversion >= (3, 17, 0):
   253     try:
   254     try:
   254         add_cube('sioc', update_database=False)
   255         add_cube('sioc', update_database=False)