[IMP] add a method to rename a database (and the associated filestore directory)
authorChristophe Simonis <christophe@tinyerp.com>
Wed, 20 May 2009 14:58:19 +0000 (16:58 +0200)
committerChristophe Simonis <christophe@tinyerp.com>
Wed, 20 May 2009 14:58:19 +0000 (16:58 +0200)
bzr revid: christophe@tinyerp.com-20090520145819-5gx1w7f8zgsmp3is

bin/service/web_services.py

index f571379..2d14132 100644 (file)
@@ -50,6 +50,7 @@ class db(netsvc.Service):
         self.exportMethod(self.drop)
         self.exportMethod(self.dump)
         self.exportMethod(self.restore)
+        self.exportMethod(self.rename)
         self.exportMethod(self.list)
         self.exportMethod(self.list_lang)
         self.exportMethod(self.change_admin_password)
@@ -238,6 +239,32 @@ class db(netsvc.Service):
                 'RESTORE DB: %s' % (db_name))
         return True
 
+    def rename(self, password, old_name, new_name):
+        security.check_super(password)
+        sql_db.close_db(old_name)
+        logger = netsvc.Logger()
+
+        db = sql_db.db_connect('template1')
+        cr = db.serialized_cursor()
+        try:
+            try:
+                cr.execute('ALTER DATABASE "%s" RENAME TO "%s"' % (old_name, new_name))
+            except Exception, e:
+                logger.notifyChannel("web-services", netsvc.LOG_ERROR,
+                        'RENAME DB: %s -> %s failed:\n%s' % (old_name, new_name, e))
+                raise Exception("Couldn't rename database %s to %s: %s" % (old_name, new_name, e))
+            else:
+                fs = os.path.join(tools.config['root_path'], 'filestore')
+                if os.path.exists(os.path.join(fs, old_name)):
+                    os.rename(os.path.join(fs, old_name), os.path.join(fs, new_name))
+                    
+                logger.notifyChannel("web-services", netsvc.LOG_INFO,
+                    'RENAME DB: %s -> %s' % (old_name, new_name))
+        finally:
+            cr.close()
+            sql_db.close_db('template1')
+        return True
+
     def db_exist(self, db_name):
         try:
             db = sql_db.db_connect(db_name)