63
64
self._pg_psw_env_var_is_set = False # on win32, pg_dump need the PGPASSWORD env var
66
def _create_empty_database(self, name):
67
db = sql_db.db_connect('template1')
70
cr.autocommit(True) # avoid transaction block
71
cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "template0" """ % name)
65
75
def create(self, password, db_name, demo, lang, user_password='admin'):
66
76
security.check_super(password)
67
77
self.id_protect.acquire()
72
82
self.actions[id] = {'clean': False}
74
db = sql_db.db_connect('template1')
79
cr.autocommit(True) # avoid transaction block
80
cr.execute('CREATE DATABASE "%s" ENCODING \'unicode\'' % db_name)
84
self._create_empty_database(db_name)
86
86
class DBInitialize(object):
87
87
def __call__(self, serv, id, db_name, demo, lang, user_password='admin'):
90
90
serv.actions[id]['progress'] = 0
92
91
cr = sql_db.db_connect(db_name).cursor()
138
136
def get_progress(self, password, id):
139
137
security.check_super(password)
138
tools.debug((id, self.actions.keys()))
140
139
if self.actions[id]['thread'].isAlive():
141
140
# return addons.init_progress[db_name]
142
141
return (min(self.actions[id].get('progress', 0),0.95), [])
144
143
clean = self.actions[id]['clean']
146
145
users = self.actions[id]['users']
148
147
return (1.0, users)
150
149
e = self.actions[id]['exception']
152
151
raise Exception, e
154
153
def drop(self, password, db_name):
157
156
logger = netsvc.Logger()
159
158
db = sql_db.db_connect('template1')
160
cr.autocommit(True) # avoid transaction block
163
cr.autocommit(True) # avoid transaction block
166
cr.execute('DROP DATABASE "%s"' % db_name)
168
logger.notifyChannel("web-services", netsvc.LOG_ERROR,
169
'DROP DB: %s failed:\n%s' % (db_name, e))
170
raise Exception("Couldn't drop database %s: %s" % (db_name, e))
172
logger.notifyChannel("web-services", netsvc.LOG_INFO,
173
'DROP DB: %s' % (db_name))
163
cr.execute('DROP DATABASE "%s"' % db_name)
165
logger.notifyChannel("web-services", netsvc.LOG_ERROR,
166
'DROP DB: %s failed:\n%s' % (db_name, e))
167
raise Exception("Couldn't drop database %s: %s" % (db_name, e))
169
logger.notifyChannel("web-services", netsvc.LOG_INFO,
170
'DROP DB: %s' % (db_name))
180
175
def _set_pg_psw_env_var(self):
227
222
'RESTORE DB: %s already exists' % (db_name,))
228
223
raise Exception, "Database already exists"
230
db = sql_db.db_connect('template1')
234
cr.autocommit(True) # avoid transaction block
236
cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "template0" """ % db_name)
225
self._create_empty_database(db_name)
242
227
cmd = ['pg_restore', '--no-owner']
243
228
if tools.config['db_user']:
276
261
logger = netsvc.Logger()
278
263
db = sql_db.db_connect('template1')
284
cr.execute('ALTER DATABASE "%s" RENAME TO "%s"' % (old_name, new_name))
286
logger.notifyChannel("web-services", netsvc.LOG_ERROR,
287
'RENAME DB: %s -> %s failed:\n%s' % (old_name, new_name, e))
288
raise Exception("Couldn't rename database %s to %s: %s" % (old_name, new_name, e))
290
fs = os.path.join(tools.config['root_path'], 'filestore')
291
if os.path.exists(os.path.join(fs, old_name)):
292
os.rename(os.path.join(fs, old_name), os.path.join(fs, new_name))
267
cr.execute('ALTER DATABASE "%s" RENAME TO "%s"' % (old_name, new_name))
269
logger.notifyChannel("web-services", netsvc.LOG_ERROR,
270
'RENAME DB: %s -> %s failed:\n%s' % (old_name, new_name, e))
271
raise Exception("Couldn't rename database %s to %s: %s" % (old_name, new_name, e))
273
fs = os.path.join(tools.config['root_path'], 'filestore')
274
if os.path.exists(os.path.join(fs, old_name)):
275
os.rename(os.path.join(fs, old_name), os.path.join(fs, new_name))
294
logger.notifyChannel("web-services", netsvc.LOG_INFO,
295
'RENAME DB: %s -> %s' % (old_name, new_name))
277
logger.notifyChannel("web-services", netsvc.LOG_INFO,
278
'RENAME DB: %s -> %s' % (old_name, new_name))
302
283
def db_exist(self, db_name):
308
289
raise Exception('AccessDenied')
310
291
db = sql_db.db_connect('template1')
316
db_user = tools.config["db_user"]
317
if not db_user and os.name == 'posix':
319
db_user = pwd.getpwuid(os.getuid())[0]
321
cr.execute("select decode(usename, 'escape') from pg_user where usesysid=(select datdba from pg_database where datname=%s)", (tools.config["db_name"],))
323
db_user = res and str(res[0])
325
cr.execute("select decode(datname, 'escape') from pg_database where datdba=(select usesysid from pg_user where usename=%s) and datname not in ('template0', 'template1', 'postgres') order by datname", (db_user,))
327
cr.execute("select decode(datname, 'escape') from pg_database where datname not in('template0', 'template1','postgres') order by datname")
328
res = [str(name) for (name,) in cr.fetchall()]
295
db_user = tools.config["db_user"]
296
if not db_user and os.name == 'posix':
298
db_user = pwd.getpwuid(os.getuid())[0]
300
cr.execute("select decode(usename, 'escape') from pg_user where usesysid=(select datdba from pg_database where datname=%s)", (tools.config["db_name"],))
302
db_user = res and str(res[0])
304
cr.execute("select decode(datname, 'escape') from pg_database where datdba=(select usesysid from pg_user where usename=%s) and datname not in ('template0', 'template1', 'postgres') order by datname", (db_user,))
306
cr.execute("select decode(datname, 'escape') from pg_database where datname not in('template0', 'template1','postgres') order by datname")
307
res = [str(name) for (name,) in cr.fetchall()]
366
343
self.abortResponse(1, inst.name, 'warning', inst.value)
367
344
except except_osv, inst:
368
345
self.abortResponse(1, inst.name, inst.exc_type, inst.value)
371
348
tb_s = reduce(lambda x, y: x+y, traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback))
372
349
l.notifyChannel('web-services', netsvc.LOG_ERROR, tb_s)
389
366
self.exportMethod(self.get_migration_scripts)
390
367
self.exportMethod(self.get_server_environment)
391
368
self.exportMethod(self.login_message)
369
self.exportMethod(self.check_connectivity)
393
371
def ir_set(self, db, uid, password, keys, args, name, value, replace=True, isobject=False):
394
372
security.check(db, uid, password)
512
490
l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to read the module %s' % (module,))
515
zip_contents = cStringIO.StringIO(base64_decoded)
493
zip_contents = StringIO(base64_decoded)
516
494
zip_contents.seek(0)
570
548
%(platform.release(), platform.version(), platform.architecture()[0],
571
549
os_lang, platform.python_version(),release.version,rev_id)
572
550
return environment
575
552
def login_message(self):
576
553
return tools.config.get('login_message', False)
555
def check_connectivity(self):
556
return bool(sql_db.db_connect('template1'))
580
560
class objects_proxy(netsvc.Service):