def shutdown(self): """ Closes all the OpenERP Service database connections :return: None """ if self.db_name: import sql_db sql_db.close_db(self.db_name)
def drop_database(self): import sql_db sql_db.close_db(self.db_name) conn = sql_db.db_connect('template1') cursor = conn.cursor() try: logger.info('Droping database %s', self.db_name) cursor.autocommit(True) cursor.execute('DROP DATABASE ' + self.db_name) finally: cursor.close()
def __getattr__(self, name): if not self.fork and (os.getpid() != self.ppid): import sql_db sql_db.close_db(self.dbname) service = OpenERPService() service.pooler.pool_dic.pop(self.dbname, None) service.db_name = self.dbname self.pool = service.pool self.fork = True name = camel2dot(name) return self.model(name)
def createdb(dbname=False): conn = sql_db.db_connect('postgres') cursor = conn.cursor() if not dbname: dbname = 'translate_{}'.format(str(int(time.time()))) try: cursor.autocommit(True) cursor.execute('CREATE DATABASE {}'.format(dbname)) return dbname finally: cursor.close() sql_db.close_db('postgres')
def exp_rename(self, old_name, new_name): sql_db.close_db(old_name) logger = netsvc.Logger() db = sql_db.db_connect('template1') cr = db.cursor() cr.autocommit(True) # avoid transaction block 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:
def exp_drop(self, db_name): sql_db.close_db(db_name) logger = netsvc.Logger() db = sql_db.db_connect('template1') cr = db.cursor() cr.autocommit(True) # avoid transaction block try: try: cr.execute('DROP DATABASE "%s"' % db_name) except Exception, e: logger.notifyChannel("web-services", netsvc.LOG_ERROR, 'DROP DB: %s failed:\n%s' % (db_name, e)) raise Exception("Couldn't drop database %s: %s" % (db_name, e)) else:
def __getattr__(self, name): """ Gets the value of a property :param name: property name :return: Returns the value of a property """ if not self.fork and (os.getpid() != self.ppid): import sql_db sql_db.close_db(self.dbname) service = OpenERPService() service.pooler.pool_dic.pop(self.dbname, None) service.db_name = self.dbname self.pool = service.pool self.fork = True name = camel2dot(name) return self.model(name)
def drop_database(self): """Drop database from `self.db_name` """ import sql_db sql_db.close_db(self.db_name) conn = sql_db.db_connect('template1') cursor = conn.cursor() try: logger.info('Droping database %s', self.db_name) cursor.autocommit(True) logger.info('Disconnect all sessions from database %s', self.db_name) cursor.execute( "SELECT pg_terminate_backend(pg_stat_activity.pid) " " FROM pg_stat_activity " " WHERE pg_stat_activity.datname = '{}'" " AND pid <> pg_backend_pid() ".format(self.db_name) ) cursor.execute('DROP DATABASE ' + self.db_name) finally: cursor.close()
def drop_database(self): """Drop database from `self.db_name` """ import sql_db sql_db.close_db(self.db_name) conn = sql_db.db_connect('template1') cursor = conn.cursor() try: logger.info('Droping database %s', self.db_name) cursor.autocommit(True) logger.info('Disconnect all sessions from database %s', self.db_name) cursor.execute("SELECT pg_terminate_backend(pg_stat_activity.pid) " " FROM pg_stat_activity " " WHERE pg_stat_activity.datname = '{}'" " AND pid <> pg_backend_pid() ".format( self.db_name)) cursor.execute('DROP DATABASE ' + self.db_name) finally: cursor.close()
def create_database(self, template=True): """ Creates a new database. :param template: use a template (name must be `base`) (default True) """ db_name = 'test_' + str(int(time.time())) import sql_db conn = sql_db.db_connect('postgres') cursor = conn.cursor() try: self.logger.info('Creating database %s', db_name) cursor.autocommit(True) if template: cursor.execute( 'CREATE DATABASE {} WITH TEMPLATE base'.format(db_name)) else: cursor.execute('CREATE DATABASE {}'.format(db_name)) return db_name finally: cursor.close() sql_db.close_db('postgres')
def create_database(self, template=True, db_name=None): """Creates a new database. :param template: use a template (name must be `base`) (default True) """ if db_name is None: db_name = 'test_' + str(int(time.time())) import sql_db conn = sql_db.db_connect('postgres') cursor = conn.cursor() try: logger.info('Creating database %s', db_name) cursor.autocommit(True) if template: cursor.execute('CREATE DATABASE {} WITH TEMPLATE base'.format( db_name )) else: cursor.execute('CREATE DATABASE {}'.format(db_name)) return db_name finally: cursor.close() sql_db.close_db('postgres')
def shutdown(self): if self.db_name: import sql_db sql_db.close_db(self.db_name)