def perf_cursor(dbname): perf_dbname = dbname + '_perf' try: _create_empty_database(perf_dbname) except DatabaseExists: pass return db_connect(perf_dbname).cursor()
def run(self, cmdargs): parser = argparse.ArgumentParser( prog="%s start" % sys.argv[0].split(os.path.sep)[-1], description=self.__doc__ ) parser.add_argument('--path', default=".", help="Directory where your project's modules are stored (will autodetect from current dir)") parser.add_argument("-d", "--database", dest="db_name", default=None, help="Specify the database name (default to project's directory name") args, unknown = parser.parse_known_args(args=cmdargs) # When in a virtualenv, by default use it's path rather than the cwd if args.path == '.' and os.environ.get('VIRTUAL_ENV'): args.path = os.environ.get('VIRTUAL_ENV') project_path = os.path.abspath(os.path.expanduser(os.path.expandvars(args.path))) module_root = get_module_root(project_path) db_name = None if module_root: # started in a module so we choose this module name for database db_name = project_path.split(os.path.sep)[-1] # go to the parent's directory of the module root project_path = os.path.abspath(os.path.join(project_path, os.pardir)) # check if one of the subfolders has at least one module mods = self.get_module_list(project_path) if mods and '--addons-path' not in cmdargs: cmdargs.append('--addons-path=%s' % project_path) if not args.db_name: args.db_name = db_name or project_path.split(os.path.sep)[-1] cmdargs.extend(('-d', args.db_name)) # TODO: forbid some database names ? eg template1, ... try: _create_empty_database(args.db_name) odoo.tools.config['init']['base'] = True except DatabaseExists as e: pass except Exception as e: die("Could not create database `%s`. (%s)" % (args.db_name, e)) if '--db-filter' not in cmdargs: cmdargs.append('--db-filter=^%s$' % args.db_name) # Remove --path /-p options from the command arguments def to_remove(i, l): return l[i] == '-p' or l[i].startswith('--path') or \ (i > 0 and l[i-1] in ['-p', '--path']) cmdargs = [v for i, v in enumerate(cmdargs) if not to_remove(i, cmdargs)] main(cmdargs)
def _create_table(self): cr = self._cr perf_dbname = cr.dbname + '_perf' try: _create_empty_database(perf_dbname) except DatabaseExists: pass columns = [(k, f.column_type[1]) for k, f in self._fields.iteritems() if f.column_type] with db_connect(perf_dbname).cursor() as new_cr: new_cr.execute( "SELECT relname FROM pg_class WHERE relkind IN ('r','v') AND relname=%s", (self._table, )) if not new_cr.rowcount: new_cr.execute( 'CREATE TABLE "%s" (id SERIAL NOT NULL, PRIMARY KEY(id))' % self._table) for f, t in columns: if f == 'id': continue new_cr.execute( 'SELECT c.relname FROM pg_class c, pg_attribute a ' 'WHERE c.relname=%s AND a.attname=%s AND c.oid=a.attrelid', (self._table, f)) if not new_cr.rowcount: new_cr.execute('ALTER TABLE "%s" ADD COLUMN "%s" %s' % (self._table, f, t)) cr.execute('CREATE EXTENSION IF NOT EXISTS postgres_fdw') cr.execute( "SELECT srvname FROM pg_foreign_server WHERE srvname='perf_server'" ) if not cr.rowcount: db_host = config['db_host'] or 'localhost' db_port = str(config['db_port'] or 5432) cr.execute( """ CREATE SERVER perf_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host %s, port %s, dbname %s) """, (db_host, db_port, perf_dbname)) db_user = config['db_user'] db_password = config['db_password'] cr.execute('DROP USER MAPPING IF EXISTS FOR %s SERVER perf_server' % db_user) cr.execute( 'CREATE USER MAPPING FOR %s SERVER perf_server OPTIONS ' '(user %%s, password %%s)' % db_user, (db_user, db_password)) cr.execute('DROP FOREIGN TABLE IF EXISTS %s' % self._table) cr.execute('CREATE FOREIGN TABLE %s (%s) SERVER perf_server' % (self._table, ', '.join(['%s %s' % c for c in columns]))) cr.commit()
def run(self, cmdargs): parser = argparse.ArgumentParser(prog="%s start" % sys.argv[0].split(os.path.sep)[-1], description=self.__doc__) parser.add_argument( '--path', default=".", help= "Directory where your project's modules are stored (will autodetect from current dir)" ) parser.add_argument( "-d", "--database", dest="db_name", default=None, help= "Specify the database name (default to project's directory name") args, unknown = parser.parse_known_args(args=cmdargs) # When in a virtualenv, by default use it's path rather than the cwd if args.path == '.' and os.environ.get('VIRTUAL_ENV'): args.path = os.environ.get('VIRTUAL_ENV') project_path = os.path.abspath( os.path.expanduser(os.path.expandvars(args.path))) module_root = get_module_root(project_path) db_name = None if module_root: # started in a module so we choose this module name for database db_name = project_path.split(os.path.sep)[-1] # go to the parent's directory of the module root project_path = os.path.abspath( os.path.join(project_path, os.pardir)) # check if one of the subfolders has at least one module mods = self.get_module_list(project_path) if mods and '--addons-path' not in cmdargs: cmdargs.append('--addons-path=%s' % project_path) if not args.db_name: args.db_name = db_name or project_path.split(os.path.sep)[-1] cmdargs.extend(('-d', args.db_name)) # TODO: forbid some database names ? eg template1, ... try: _create_empty_database(args.db_name) _add_timescale_extention(args.db_name) except DatabaseExists, e: pass
def runtests(ctx, db_name, module): config, logger = ( ctx.obj['config'], ctx.obj['logger'], ) if module: modules = { module_name: 1 for module_name in module } config['init'] = dict(modules) # !! Tests need demo data config['without_demo'] = '' # Enables demo data config['test_enable'] = True config['xmlrpc_port'] = 8000 # !! Database signalling needs to be turned off while # running tests import odoo odoo.multi_process = False # Now import further from odoo.tests.common import PORT from odoo.modules.registry import Registry from odoo.service.db import _create_empty_database from odooku.services.wsgi import WSGIServer _create_empty_database(db_name) server = WSGIServer( PORT, max_accept=1 ) gevent.spawn(server.serve_forever) def runtests(): registry = Registry.new(db_name) total = (registry._assertion_report.successes + registry._assertion_report.failures) failures = registry._assertion_report.failures logger.info("Completed (%s) tests. %s failures." % (total, failures)) sys.exit(1 if failures else 0) gevent.spawn(runtests).join()
def client_db_create(self, dbname=None, demo=False, lang='en_US', user_password='******', user_login='******', country_code=None, phone=None, template_dbname=None, **params): demo = str2bool(demo, False) if not dbname: raise werkzeug.exceptions.BadRequest( description='Missing parameter: dbname') _logger.info("Create database: %s (demo=%r)", dbname, demo) try: service_db._create_empty_database(dbname) except service_db.DatabaseExists as bd_ex: raise werkzeug.exceptions.Conflict(description=str(bd_ex)) service_db._initialize_db( id, dbname, demo, lang, user_password, user_login, country_code=str_filter_falsy(country_code), phone=str_filter_falsy(phone), ) db = db_connect(dbname) with closing(db.cursor()) as cr: db_init = modules_db.is_initialized(cr) if not db_init: raise werkzeug.exceptions.InternalServerError( description='Database not initialized.') return Response('OK', status=200)
def run(self, cmdargs): parser = argparse.ArgumentParser( prog="%s start" % sys.argv[0].split(os.path.sep)[-1], description=self.__doc__ ) parser.add_argument('--path', default=".", help="Directory where your project's modules are stored (will autodetect from current dir)") parser.add_argument("-d", "--database", dest="db_name", default=None, help="Specify the database name (default to project's directory name") args, unknown = parser.parse_known_args(args=cmdargs) project_path = os.path.abspath(os.path.expanduser(os.path.expandvars(args.path))) module_root = get_module_root(project_path) db_name = None if module_root: # started in a module so we choose this module name for database db_name = project_path.split(os.path.sep)[-1] # go to the parent's directory of the module root project_path = os.path.abspath(os.path.join(project_path, os.pardir)) # check if one of the subfolders has at least one module mods = self.get_module_list(project_path) if mods and '--addons-path' not in cmdargs: cmdargs.append('--addons-path=%s' % project_path) if not args.db_name: args.db_name = db_name or project_path.split(os.path.sep)[-1] cmdargs.extend(('-d', args.db_name)) # TODO: forbid some database names ? eg template1, ... try: _create_empty_database(args.db_name) except DatabaseExists, e: pass
def run(self, cmdargs): parser = argparse.ArgumentParser(prog="%s start" % sys.argv[0].split(os.path.sep)[-1], description=self.__doc__) parser.add_argument( '--path', default=".", help= "Directory where your project's modules are stored (will autodetect from current dir)" ) parser.add_argument( "-d", "--database", dest="db_name", default=None, help= "Specify the database name (default to project's directory name") args, unknown = parser.parse_known_args(args=cmdargs) project_path = os.path.abspath( os.path.expanduser(os.path.expandvars(args.path))) module_root = get_module_root(project_path) db_name = None if module_root: # started in a module so we choose this module name for database db_name = project_path.split(os.path.sep)[-1] # go to the parent's directory of the module root project_path = os.path.abspath( os.path.join(project_path, os.pardir)) # check if one of the subfolders has at least one module mods = self.get_module_list(project_path) if mods and '--addons-path' not in cmdargs: cmdargs.append('--addons-path=%s' % project_path) if not args.db_name: args.db_name = db_name or project_path.split(os.path.sep)[-1] cmdargs.extend(('-d', args.db_name)) # TODO: forbid some database names ? eg template1, ... try: _create_empty_database(args.db_name) except DatabaseExists as e: pass except Exception as e: die("Could not create database `%s`. (%s)" % (args.db_name, e)) if '--db-filter' not in cmdargs: cmdargs.append('--db-filter=^%s$' % args.db_name) # Remove --path /-p options from the command arguments def to_remove(i, l): return l[i] == '-p' or l[i].startswith('--path') or \ (i > 0 and l[i-1] in ['-p', '--path']) cmdargs = [ v for i, v in enumerate(cmdargs) if not to_remove(i, cmdargs) ] main(cmdargs)
def exp_create_database(vals, country_code, lang, users_detail): _logger.info('Create database `%s`.', vals['name']) db._create_empty_database(vals['name']) _initialize_db(id, vals, country_code, lang, users_detail) return True