diff --git a/openerp/service/db.py b/openerp/service/db.py index adbd058755d..4bc92a79801 100644 --- a/openerp/service/db.py +++ b/openerp/service/db.py @@ -144,6 +144,24 @@ def exp_get_progress(id): self_actions.pop(id) raise Exception, e + +def _drop_conn(cr, db_name): + # Try to terminate all other connections that might prevent + # dropping the database + try: + # PostgreSQL 9.2 renamed pg_stat_activity.procpid to pid: + # http://www.postgresql.org/docs/9.2/static/release-9-2.html#AEN110389 + pid_col = 'pid' if cr._cnx.server_version >= 90200 else 'procpid' + + cr.execute("""SELECT pg_terminate_backend(%(pid_col)s) + FROM pg_stat_activity + WHERE datname = %%s AND + %(pid_col)s != pg_backend_pid()""" % {'pid_col': pid_col}, + (db_name,)) + except Exception: + pass + + def exp_drop(db_name): if db_name not in exp_list(True): return False @@ -153,21 +171,7 @@ def exp_drop(db_name): db = openerp.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: cr.autocommit(True) # avoid transaction block - # Try to terminate all other connections that might prevent - # dropping the database - try: - - # PostgreSQL 9.2 renamed pg_stat_activity.procpid to pid: - # http://www.postgresql.org/docs/9.2/static/release-9-2.html#AEN110389 - pid_col = 'pid' if cr._cnx.server_version >= 90200 else 'procpid' - - cr.execute("""SELECT pg_terminate_backend(%(pid_col)s) - FROM pg_stat_activity - WHERE datname = %%s AND - %(pid_col)s != pg_backend_pid()""" % {'pid_col': pid_col}, - (db_name,)) - except Exception: - pass + _drop_conn(cr, db_name) try: cr.execute('DROP DATABASE "%s"' % db_name) @@ -274,6 +278,7 @@ def exp_rename(old_name, new_name): db = openerp.sql_db.db_connect('postgres') with closing(db.cursor()) as cr: cr.autocommit(True) # avoid transaction block + _drop_conn(cr, old_name) try: cr.execute('ALTER DATABASE "%s" RENAME TO "%s"' % (old_name, new_name)) except Exception, e: