server/sqlutils.py
changeset 4342 b4e186da08f2
parent 4322 f65743cc53e4
child 4353 7db69db4913c
equal deleted inserted replaced
4341:2d2aa2526c56 4342:b4e186da08f2
    30 from cubicweb.server.utils import crypt_password
    30 from cubicweb.server.utils import crypt_password
    31 
    31 
    32 
    32 
    33 lgc.USE_MX_DATETIME = False
    33 lgc.USE_MX_DATETIME = False
    34 SQL_PREFIX = 'cw_'
    34 SQL_PREFIX = 'cw_'
       
    35 
       
    36 def bw_run_command(cmd):
       
    37     """backup/restore command are string w/ lgc < 0.47, lists with earlier versions
       
    38     """
       
    39     if isinstance(cmd, basestring):
       
    40         print cmd
       
    41         return os.system(cmd)
       
    42     print ' '.join(cmd)
       
    43     return subprocess.call(cmd)
    35 
    44 
    36 
    45 
    37 def sqlexec(sqlstmts, cursor_or_execute, withpb=not os.environ.get('APYCOT_ROOT'),
    46 def sqlexec(sqlstmts, cursor_or_execute, withpb=not os.environ.get('APYCOT_ROOT'),
    38             pbtitle='', delimiter=';'):
    47             pbtitle='', delimiter=';'):
    39     """execute sql statements ignoring DROP/ CREATE GROUP or USER statements
    48     """execute sql statements ignoring DROP/ CREATE GROUP or USER statements
   169         init_cnx(self.dbdriver, cnx)
   178         init_cnx(self.dbdriver, cnx)
   170         #self.dbapi_module.type_code_test(cnx.cursor())
   179         #self.dbapi_module.type_code_test(cnx.cursor())
   171         return cnx
   180         return cnx
   172 
   181 
   173     def backup_to_file(self, backupfile):
   182     def backup_to_file(self, backupfile):
   174         cmd = self.dbhelper.backup_command(self.dbname, self.dbhost,
   183         for cmd in self.dbhelper.backup_commands(self.dbname, self.dbhost,
   175                                            self.dbuser, backupfile,
   184                                                  self.dbuser, backupfile,
   176                                            keepownership=False)
   185                                                  keepownership=False):
   177         if subprocess.call(cmd):
   186             if bw_run_command(cmd):
   178             raise Exception('Failed command: %s' % cmd)
   187                 if not confirm('-> Failed. Continue anyway?', default='n'):
       
   188                     raise Exception('Failed command: %s' % cmd)
   179 
   189 
   180     def restore_from_file(self, backupfile, confirm, drop=True):
   190     def restore_from_file(self, backupfile, confirm, drop=True):
   181         for cmd in self.dbhelper.restore_commands(self.dbname, self.dbhost,
   191         for cmd in self.dbhelper.restore_commands(self.dbname, self.dbhost,
   182                                                   self.dbuser, backupfile,
   192                                                   self.dbuser, backupfile,
   183                                                   self.encoding,
   193                                                   self.encoding,
   184                                                   keepownership=False,
   194                                                   keepownership=False,
   185                                                   drop=drop):
   195                                                   drop=drop):
   186             if subprocess.call(cmd):
   196             if bw_run_command(cmd):
   187                 print '-> Failed command: %s' % cmd
   197                 if not confirm('Failed. Continue anyway?', default='n'):
   188                 if not confirm('Continue anyway?', default='n'):
       
   189                     raise Exception('Failed command: %s' % cmd)
   198                     raise Exception('Failed command: %s' % cmd)
   190 
   199 
   191     def merge_args(self, args, query_args):
   200     def merge_args(self, args, query_args):
   192         if args is not None:
   201         if args is not None:
   193             args = dict(args)
   202             args = dict(args)