server/migractions.py
changeset 3998 94cc7cad3d2d
parent 3890 d7a270f50f54
parent 3935 2fbb79054a1a
child 4011 394f853bb653
equal deleted inserted replaced
3895:92ead039d3d0 3998:94cc7cad3d2d
    22 import tarfile
    22 import tarfile
    23 import tempfile
    23 import tempfile
    24 import shutil
    24 import shutil
    25 import os.path as osp
    25 import os.path as osp
    26 from datetime import datetime
    26 from datetime import datetime
       
    27 from glob import glob
       
    28 from warnings import warn
    27 
    29 
    28 from logilab.common.deprecation import deprecated
    30 from logilab.common.deprecation import deprecated
    29 from logilab.common.decorators import cached, clear_cache
    31 from logilab.common.decorators import cached, clear_cache
    30 from logilab.common.adbh import get_adv_func_helper
    32 from logilab.common.adbh import get_adv_func_helper
    31 
    33 
   102         """
   104         """
   103         try:
   105         try:
   104             if migrscript.endswith('.sql'):
   106             if migrscript.endswith('.sql'):
   105                 if self.execscript_confirm(migrscript):
   107                 if self.execscript_confirm(migrscript):
   106                     sqlexec(open(migrscript).read(), self.session.system_sql)
   108                     sqlexec(open(migrscript).read(), self.session.system_sql)
   107             elif migrscript.endswith('.py'):
   109             elif migrscript.endswith('.py') or migrscript.endswith('.txt'):
   108                 return super(ServerMigrationHelper, self).cmd_process_script(
   110                 return super(ServerMigrationHelper, self).cmd_process_script(
   109                     migrscript, funcname, *args, **kwargs)
   111                     migrscript, funcname, *args, **kwargs)
   110             else:
   112             else:
   111                 print
   113                 print
   112                 print ('-> ignoring %s, only .py and .sql scripts are considered' %
   114                 print ('-> ignoring %s, only .py .sql and .txt scripts are considered' %
   113                        migrscript)
   115                        migrscript)
   114                 print
   116                 print
   115             self.commit()
   117             self.commit()
   116         except:
   118         except:
   117             self.rollback()
   119             self.rollback()
   305                 self.execscript_confirm = execscript_confirm
   307                 self.execscript_confirm = execscript_confirm
   306                 if self.config.free_wheel:
   308                 if self.config.free_wheel:
   307                     self.repo.hm.register_hook(setowner_after_add_entity,
   309                     self.repo.hm.register_hook(setowner_after_add_entity,
   308                                                'after_add_entity', '')
   310                                                'after_add_entity', '')
   309                     self.cmd_reactivate_verification_hooks()
   311                     self.cmd_reactivate_verification_hooks()
       
   312 
       
   313     def install_custom_sql_scripts(self, directory, driver):
       
   314         self.session.set_pool() # ensure pool is set
       
   315         for fpath in glob(osp.join(directory, '*.sql.%s' % driver)):
       
   316             newname = osp.basename(fpath).replace('.sql.%s' % driver,
       
   317                                                   '.%s.sql' % driver)
       
   318             warn('[3.5.6] rename %s into %s' % (fpath, newname),
       
   319                  DeprecationWarning)
       
   320             print '-> installing', fpath
       
   321             sqlexec(open(fpath).read(), self.session.system_sql, False,
       
   322                     delimiter=';;')
       
   323         for fpath in glob(osp.join(directory, '*.%s.sql' % driver)):
       
   324             print '-> installing', fpath
       
   325             sqlexec(open(fpath).read(), self.session.system_sql, False,
       
   326                     delimiter=';;')
   310 
   327 
   311     # schema synchronization internals ########################################
   328     # schema synchronization internals ########################################
   312 
   329 
   313     def _synchronize_permissions(self, erschema, teid):
   330     def _synchronize_permissions(self, erschema, teid):
   314         """permission synchronization for an entity or relation type"""
   331         """permission synchronization for an entity or relation type"""
   542         # XXX we have to replace fs_schema, used in cmd_add_relation_type
   559         # XXX we have to replace fs_schema, used in cmd_add_relation_type
   543         # etc. and fsschema of migration script contexts
   560         # etc. and fsschema of migration script contexts
   544         self.fs_schema = self._create_context()['fsschema'] = newcubes_schema
   561         self.fs_schema = self._create_context()['fsschema'] = newcubes_schema
   545         new = set()
   562         new = set()
   546         # execute pre-create files
   563         # execute pre-create files
       
   564         driver = self.repo.system_source.dbdriver
   547         for pack in reversed(newcubes):
   565         for pack in reversed(newcubes):
   548             self.exec_event_script('precreate', self.config.cube_dir(pack))
   566             cubedir = self.config.cube_dir(pack)
       
   567             self.install_custom_sql_scripts(osp.join(cubedir, 'schema'), driver)
       
   568             self.exec_event_script('precreate', cubedir)
   549         # add new entity and relation types
   569         # add new entity and relation types
   550         for rschema in newcubes_schema.relations():
   570         for rschema in newcubes_schema.relations():
   551             if not rschema in self.repo.schema:
   571             if not rschema in self.repo.schema:
   552                 self.cmd_add_relation_type(rschema.type)
   572                 self.cmd_add_relation_type(rschema.type)
   553                 new.add(rschema.type)
   573                 new.add(rschema.type)