[FIX] oe drop: correctly call netsvc.init_logger(), and use pid instead of procpid under postgres 9.2.

bzr revid: vmt@openerp.com-20130219151613-k0pvlemire9kvrok
This commit is contained in:
Vo Minh Thu 2013-02-19 16:16:13 +01:00
parent eafdbbd601
commit 1f269d0d93
1 changed files with 9 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import common
# openerp/service/web_services.py). # openerp/service/web_services.py).
def drop_database(database_name): def drop_database(database_name):
import openerp import openerp
openerp.netsvc.init_logger()
db = openerp.sql_db.db_connect('template1') db = openerp.sql_db.db_connect('template1')
cr = db.cursor() cr = db.cursor()
cr.autocommit(True) # avoid transaction block cr.autocommit(True) # avoid transaction block
@ -16,17 +17,21 @@ def drop_database(database_name):
# TODO option for doing this. # TODO option for doing this.
# Try to terminate all other connections that might prevent # Try to terminate all other connections that might prevent
# dropping the database # dropping the database
# 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'
try: try:
cr.execute("""SELECT pg_terminate_backend(procpid) cr.execute("""SELECT pg_terminate_backend(%(pid_col)s)
FROM pg_stat_activity FROM pg_stat_activity
WHERE datname = %s AND WHERE datname = %%s AND
procpid != pg_backend_pid()""", %(pid_col)s != pg_backend_pid()""" % {'pid_col': pid_col},
(database_name,)) (database_name,))
except Exception: except Exception:
pass pass
try: try:
cr.execute('DROP DATABASE "%s"' % database_name) cr.execute('DROP DATABASE "%s"' % database_name, log_exceptions=False)
except Exception, e: except Exception, e:
print "Can't drop %s" % (database_name,) print "Can't drop %s" % (database_name,)
finally: finally: