def exp_drop(db_name): """Monkeypatched to delete cloud container instead of filesystem directory""" if db_name not in exp_list(True): return False # Determine storage "on foot" with raw SQL (while the database is still there) db = openerp.sql_db.db_connect(db_name) with closing(db.cursor()) as cr: cr.execute("""SELECT value FROM ir_config_parameter WHERE key='ir_attachment.location'""") result = cr.dictfetchone() storage = result and result['value'] or 'file' if storage == 'openstack': cr.execute( """SELECT value FROM ir_config_parameter WHERE key='ir_attachment.location.openstack'""" ) result = cr.dictfetchone() config_params = result and json.loads(result['value']) openerp.modules.registry.RegistryManager.delete(db_name) openerp.sql_db.close_db(db_name) db = openerp.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: cr.autocommit(True) # avoid transaction block _drop_conn(cr, db_name) try: cr.execute('DROP DATABASE "%s"' % db_name) except Exception, e: _logger.error('DROP DB: %s failed:\n%s', db_name, e) raise Exception("Couldn't drop database %s: %s" % (db_name, e)) else:
def exp_duplicate_database(db_original_name, db_name): """Monkeypatched to duplicate cloud container instead of filesystem directory""" _logger.info('Duplicate database `%s` to `%s`.', db_original_name, db_name) openerp.sql_db.close_db(db_original_name) db = openerp.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: cr.autocommit(True) # avoid transaction block _drop_conn(cr, db_original_name) cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "%s" """ % (db_name, db_original_name)) # Determine storage "on foot" with raw SQL db = openerp.sql_db.db_connect(db_original_name) with closing(db.cursor()) as cr: cr.execute("""SELECT value FROM ir_config_parameter WHERE key='ir_attachment.location'""") result = cr.dictfetchone() storage = result and result['value'] or 'file' if storage.startswith('http://') or storage.startswith('https://') or storage.startwith('s3://'): # Copy bucket objects source_bucket_url = storage + '-' + db_original_name.lower() source_bucket = get_s3_bucket(source_bucket_url, autocreate=False) target_bucket_url = storage + '-' + db_name.lower() target_bucket = get_s3_bucket(target_bucket_url, autocreate=False) if source_bucket and not target_bucket: target_bucket = get_s3_bucket(target_bucket_url) sync_buckets(source_bucket, target_bucket) else: # File system copy as per original Odoo 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.copytree(from_fs, to_fs) return True
def drop_con(self): self.ensure_one() db_ws._drop_conn(self._cr, self.name) # Por si no anda... # db = sql_db.db_connect('postgres') # with closing(db.cursor()) as pg_cr: # pg_cr.autocommit(True) # avoid transaction block # db_ws._drop_conn(pg_cr, self.name) return True
def drop_con(self): """Drop connections. This is used when can not make backups. Usually in instances with workers. """ db_ws._drop_conn(self._cr, self.name) # Por si no anda... # db = sql_db.db_connect('postgres') # with closing(db.cursor()) as pg_cr: # pg_cr.autocommit(True) # avoid transaction block # db_ws._drop_conn(pg_cr, self.name) return True
def exp_drop_only_db(db_name): openerp.modules.registry.RegistryManager.delete(db_name) openerp.sql_db.close_db(db_name) db = openerp.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: cr.autocommit(True) # avoid transaction block db_ws._drop_conn(cr, db_name) try: cr.execute('DROP DATABASE "%s"' % db_name) except Exception, e: _logger.error('DROP DB: %s failed:\n%s', db_name, e) raise Exception("Couldn't drop database %s: %s" % (db_name, e)) else:
def exp_duplicate_database(db_original_name, db_name, duplicate_filestore=False): """Monkeypatched to duplicate cloud container instead of filesystem directory""" _logger.info('Duplicate database `%s` to `%s`.', db_original_name, db_name) openerp.sql_db.close_db(db_original_name) db = openerp.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: cr.autocommit(True) # avoid transaction block _drop_conn(cr, db_original_name) cr.execute( """CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "%s" """ % ( db_name, db_original_name ) ) # Determine storage "on foot" with raw SQL db = openerp.sql_db.db_connect(db_original_name) with closing(db.cursor()) as cr: cr.execute( """SELECT value FROM ir_config_parameter WHERE key='ir_attachment.location'""" ) result = cr.dictfetchone() storage = result and result['value'] or 'file' if duplicate_filestore: if storage == 'openstack': with closing(db.cursor()) as cr: cr.execute( """SELECT value FROM ir_config_parameter WHERE key='ir_attachment.location.openstack'""" ) result = cr.dictfetchone() config_params = result and json.loads(result['value']) # Copy container objects source_container_name = '{}-{}'.format( config_params['container_prefix'], db_original_name.lower() ) source_conn = SwiftClient(config_params) source_container = source_conn.get_container(source_container_name) target_container_name = '{}-{}'.format( config_params['container_prefix'], db_name.lower() ) target_conn = SwiftClient(config_params) target_container = target_conn.get_container(target_container_name) # Only copy if source bucket exists and target bucket does not if source_container and not target_container: _logger.info( 'Duplicate object storage container `%s` to `%s`', source_container_name, target_container_name ) duplicate_container( source_conn, target_conn, source_container_name, target_container_name ) else: # File system copy as per original Odoo 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): _logger.info( 'Duplicate file system directory `%s` to `%s`', from_fs, to_fs ) shutil.copytree(from_fs, to_fs) return True