[FIX] duplicate/rename/drop database: handle filestore
authorChristophe Simonis <chs@openerp.com>
Fri, 21 Mar 2014 15:56:59 +0000 (16:56 +0100)
committerChristophe Simonis <chs@openerp.com>
Fri, 21 Mar 2014 15:56:59 +0000 (16:56 +0100)
bzr revid: chs@openerp.com-20140321155659-gvg4br76214lur4l

openerp/addons/base/ir/ir_attachment.py
openerp/service/db.py
openerp/tools/config.py

index 992ac1b..186c02f 100644 (file)
@@ -69,7 +69,7 @@ class ir_attachment(osv.osv):
 
     @tools.ormcache()
     def _filestore(self, cr, uid, context=None):
-        return os.path.join(tools.config['data_dir'], 'filestore', cr.dbname)
+        return tools.config.filestore(cr.dbname)
 
     # 'data' field implementation
     def _full_path(self, cr, uid, location, path):
index f10f39a..bb97e72 100644 (file)
@@ -126,6 +126,11 @@ def exp_duplicate_database(db_original_name, db_name):
     with closing(db.cursor()) as cr:
         cr.autocommit(True)     # avoid transaction block
         cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "%s" """ % (db_name, db_original_name))
+
+    from_fs = openerp.tools.config.filestore(db_original_name)
+    to_fs = openerp.tools.config.filestore(db_name)
+    if os.path.exists(from_fs) and not os.path.exists(to_fs):
+        shutil.copy(from_fs, to_fs)
     return True
 
 def exp_get_progress(id):
@@ -178,6 +183,10 @@ def exp_drop(db_name):
             raise Exception("Couldn't drop database %s: %s" % (db_name, e))
         else:
             _logger.info('DROP DB: %s', db_name)
+
+    fs = openerp.tools.config.filestore(db_name)
+    if os.path.exists(fs):
+        shutil.rmtree(fs)
     return True
 
 def _set_pg_password_in_environment(func):
@@ -328,6 +337,11 @@ def exp_rename(old_name, new_name):
         except Exception, e:
             _logger.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))
+
+    old_fs = openerp.tools.config.filestore(old_name)
+    new_fs = openerp.tools.config.filestore(new_name)
+    if os.path.exists(old_fs) and not os.path.exists(new_fs):
+        shutil.move(old_fs, new_fs)
     return True
 
 @openerp.tools.mute_logger('openerp.sql_db')
index 8313f4d..899d72f 100644 (file)
@@ -662,6 +662,9 @@ class configmanager(object):
             os.chmod(d, 0700)
         return d
 
+    def filestore(self, dbname):
+        return os.path.join(self['data_dir'], 'filestore', dbname)
+
 config = configmanager()