def test_no_upgrades(self): with self.assertRaises(SystemExit) as ctx: upgrade_db(conn, 'openquake.server.tests.db.no_upgrades') self.assertTrue( str(ctx.exception).startswith( 'The upgrade_dir does not contain scripts matching the pattern' ))
def test_insert_error(self): with self.assertRaises(sqlite3.IntegrityError): # run 03-insert-error.sql upgrade_db(conn, pkg, skip_versions=['0002']) # check that the rollback works: the version # table contains only the base script and the # tables are not populated, i.e. '0001' has to be rolled back self.assertEqual(count(conn, 'test_version'), 1) self.assertEqual(count(conn, 'test_hazard_calculation'), 0) self.assertEqual(count(conn, 'test_lt_source_model'), 0)
def test_insert_error(self): with self.assertRaises(psycopg2.DataError): # run 03-insert-error.sql upgrade_db(conn, pkg, skip_versions=['0002']) # check that the rollback works: the version # table contains only the base script and the # tables are not populated, i.e. '0001' was rolled back self.assertEqual(count(conn, 'test.version'), 1) self.assertEqual(count(conn, 'test.hazard_calculation'), 0) self.assertEqual(count(conn, 'test.lt_source_model'), 0)
def test_insert_error(self): with self.assertRaises(sqlite3.IntegrityError): # run 03-insert-error.sql upgrade_db(conn, pkg, skip_versions=['0002']) # check that the rollback works: the version # table contains only the base script and the # tables are not populated, i.e. '0001' has to be rolled back raise unittest.SkipTest self.assertEqual(count(conn, 'test_version'), 1) self.assertEqual(count(conn, 'test_hazard_calculation'), 0) self.assertEqual(count(conn, 'test_lt_source_model'), 0)
def setup_module(): global tmpfile fh, tmpfile = tempfile.mkstemp() os.close(fh) DATABASE['NAME'] = tmpfile DATABASE['USER'] = USER connection.cursor() # connect to the db # sanity check: make sure we are using the right db fname = connection.connection.execute( 'PRAGMA database_list').fetchone()[-1] assert fname == tmpfile, (fname, tmpfile) upgrade_manager.upgrade_db(connection.connection)
def use_tmp_db(tmpfile_port): from django.db import connection from openquake.engine import config from openquake.server.settings import DATABASE from openquake.server.db import upgrade_manager tmpfile, port_str = tmpfile_port.rsplit(':', 1) DATABASE['NAME'] = tmpfile DATABASE['PORT'] = port = int(port_str) connection.cursor() # connect to the db upgrade_manager.upgrade_db(connection.connection) connection.close() # make sure we use the server on the temporary db config.DBS_ADDRESS = ('localhost', port)
def test_script_lower_than_current_version(self): applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split()) self.assertEqual(applied, '0001 0005'.split()) self.assertEqual(count(conn, 'test.hazard_calculation'), 2) self.assertEqual(count(conn, 'test.lt_source_model'), 6) # a script 0004 can enter when the database is already at version 0005 # (this is a convenient feature during development) but officially # this is not supported and what_if_I_upgrade must raise an exception with temp_script('0004-do-nothing.sql', 'SELECT 1'): applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split()) self.assertEqual(applied, ['0004']) # check that the script 0004 is rejected by what_if_I_upgrade with temp_script('0004-do-nothing.sql', 'SELECT 1'): with self.assertRaises(VersionTooSmall): what_if_I_upgrade(conn, pkg, 'read_scripts')
def test_script_lower_than_current_version(self): applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split()) self.assertEqual(applied, '0001 0005'.split()) self.assertEqual(count(conn, 'test_hazard_calculation'), 2) self.assertEqual(count(conn, 'test_lt_source_model'), 6) # a script 0004 can enter when the database is already at version 0005 # (this is a convenient feature during development) but officially # this is not supported and what_if_I_upgrade must raise an exception with temp_script('0004-do-nothing.sql', 'SELECT 1'): applied = upgrade_db(conn, pkg, skip_versions='0002 0003'.split()) self.assertEqual(applied, ['0004']) # check that the script 0004 is rejected by what_if_I_upgrade with temp_script('0004-do-nothing.sql', 'SELECT 1'): with self.assertRaises(VersionTooSmall): what_if_I_upgrade(conn, pkg, 'read_scripts')
def test_duplicated_version(self): # there are two scripts with version '0001' with self.assertRaises(DuplicatedVersion): with temp_script('0001-do-nothing.sql', 'SELECT 1'): upgrade_db(conn, pkg)
def main(): arg_parser = set_up_arg_parser() args = arg_parser.parse_args() exports = args.exports or 'xml,csv' if args.version: print __version__ sys.exit(0) if args.run or args.run_hazard or args.run_risk: # the logging will be configured in engine.py pass else: # configure a basic logging logging.basicConfig(level=logging.INFO) if args.config_file: os.environ[utils.config.OQ_CONFIG_FILE_VAR] = \ abspath(expanduser(args.config_file)) utils.config.refresh() if args.no_distribute: os.environ[openquake.engine.NO_DISTRIBUTE_VAR] = '1' if args.upgrade_db: logs.set_level('info') msg = upgrade_manager.what_if_I_upgrade( conn, extract_scripts='read_scripts') print msg if msg.startswith('Your database is already updated'): pass elif args.yes or utils.confirm('Proceed? (y/n) '): upgrade_manager.upgrade_db(conn) sys.exit(0) if args.version_db: print upgrade_manager.version_db(conn) sys.exit(0) if args.what_if_I_upgrade: print upgrade_manager.what_if_I_upgrade(conn) sys.exit(0) # check if the db is outdated outdated = dbcmd('check_outdated') if outdated: sys.exit(outdated) # hazard or hazard+risk hc_id = args.hazard_calculation_id if hc_id and int(hc_id) < 0: # make it possible commands like `oq-engine --run job_risk.ini --hc -1` hc_id = dbcmd('get_hc_id', int(hc_id)) if args.run: job_inis = map(expanduser, args.run.split(',')) if len(job_inis) not in (1, 2): sys.exit('%s should be a .ini filename or a pair of filenames ' 'separated by a comma' % args.run) for job_ini in job_inis: open(job_ini).read() # raise an IOError if the file does not exist log_file = expanduser(args.log_file) \ if args.log_file is not None else None if len(job_inis) == 2: # run hazard job_id = run_job(job_inis[0], args.log_level, log_file, args.exports) # run risk run_job(job_inis[1], args.log_level, log_file, args.exports, hazard_calculation_id=job_id) else: run_job( expanduser(args.run), args.log_level, log_file, args.exports, hazard_calculation_id=hc_id) # hazard elif args.list_hazard_calculations: dbcmd('list_calculations', 'hazard') elif args.run_hazard is not None: log_file = expanduser(args.log_file) \ if args.log_file is not None else None run_job(expanduser(args.run_hazard), args.log_level, log_file, args.exports) elif args.delete_calculation is not None: dbcmd('delete_calculation', args.delete_calculation, args.yes) # risk elif args.list_risk_calculations: dbcmd('list_calculations', 'risk') elif args.run_risk is not None: if args.hazard_calculation_id is None: sys.exit(MISSING_HAZARD_MSG) log_file = expanduser(args.log_file) \ if args.log_file is not None else None run_job( expanduser(args.run_risk), args.log_level, log_file, args.exports, hazard_calculation_id=hc_id) # export elif args.make_html_report: print 'Written', make_report(conn, args.make_html_report) sys.exit(0) elif args.list_outputs is not None: hc_id = dbcmd('get_hc_id', args.list_outputs) dbcmd('list_outputs', hc_id) elif args.show_view is not None: job_id, view_name = args.show_view print views.view(view_name, datastore.read(int(job_id))) elif args.show_log is not None: hc_id = dbcmd('get_hc_id', args.show_log[0]) print dbcmd('get_log', hc_id) elif args.export_output is not None: output_id, target_dir = args.export_output dbcmd('export_output', int(output_id), expanduser(target_dir), exports) elif args.export_outputs is not None: job_id, target_dir = args.export_outputs hc_id = dbcmd('get_hc_id', job_id) dbcmd('export_outputs', hc_id, expanduser(target_dir), exports) elif args.delete_uncompleted_calculations: dbcmd('delete_uncompleted_calculations') else: arg_parser.print_usage()
def test_syntax_error(self): with self.assertRaises(sqlite3.OperationalError) as ctx: upgrade_db(conn, pkg, skip_versions=['0001']) self.assertIn('syntax error', str(ctx.exception))
def upgrade_db(db): """ :param db: a :class:`openquake.server.dbapi.Db` instance """ return upgrade_manager.upgrade_db(db.conn)
def upgrade_db(): db.connection.cursor() # bind the connection conn = db.connection.connection return upgrade_manager.upgrade_db(conn)
def upgrade_db(): conn = db.connection.connection return upgrade_manager.upgrade_db(conn)
def test_syntax_error(self): with self.assertRaises(psycopg2.ProgrammingError) as ctx: upgrade_db(conn, pkg, skip_versions=['0001']) self.assertTrue(str(ctx.exception).startswith( 'syntax error at or near'))
def test_missing_pkg(self): with self.assertRaises(SystemExit) as ctx: upgrade_db(conn, 'openquake.server.tests.db.not_exists') self.assertTrue( str(ctx.exception).startswith( 'Could not import openquake.server.tests.db.not_exists'))
def test_no_upgrades(self): with self.assertRaises(SystemExit) as ctx: upgrade_db(conn, 'openquake.server.tests.db.no_upgrades') self.assertTrue(str(ctx.exception).startswith( 'The upgrade_dir does not contain scripts matching the pattern'))
def test_missing_pkg(self): with self.assertRaises(SystemExit) as ctx: upgrade_db(conn, 'openquake.server.tests.db.not_exists') self.assertTrue(str(ctx.exception).startswith( 'Could not import openquake.server.tests.db.not_exists'))
def test_syntax_error(self): with self.assertRaises(psycopg2.ProgrammingError) as ctx: upgrade_db(conn, pkg, skip_versions=['0001']) self.assertTrue( str(ctx.exception).startswith('syntax error at or near'))
def main(): arg_parser = set_up_arg_parser() args = arg_parser.parse_args() exports = args.exports or 'xml,csv' if args.version: print __version__ sys.exit(0) if args.run or args.run_hazard or args.run_risk: # the logging will be configured in engine.py pass else: # configure a basic logging logging.basicConfig(level=logging.INFO) if args.config_file: os.environ[config.OQ_CONFIG_FILE_VAR] = \ abspath(expanduser(args.config_file)) config.refresh() if args.no_distribute: os.environ['OQ_DISTRIBUTE'] = 'no' if args.upgrade_db: logs.set_level('info') msg = upgrade_manager.what_if_I_upgrade( conn, extract_scripts='read_scripts') print msg if msg.startswith('Your database is already updated'): pass elif args.yes or utils.confirm('Proceed? (y/n) '): upgrade_manager.upgrade_db(conn) sys.exit(0) if args.version_db: print upgrade_manager.version_db(conn) sys.exit(0) if args.what_if_I_upgrade: print upgrade_manager.what_if_I_upgrade(conn) sys.exit(0) # check if the db is outdated outdated = dbcmd('check_outdated') if outdated: sys.exit(outdated) # hazard or hazard+risk hc_id = args.hazard_calculation_id if hc_id: hc_id = int(hc_id) if hc_id < 0: # make it possible to run `oq-engine --run job_risk.ini --hc -1` hc_id = dbcmd('get_hc_id', hc_id) if args.run: job_inis = map(expanduser, args.run.split(',')) if len(job_inis) not in (1, 2): sys.exit('%s should be a .ini filename or a pair of filenames ' 'separated by a comma' % args.run) for job_ini in job_inis: open(job_ini).read() # raise an IOError if the file does not exist log_file = expanduser(args.log_file) \ if args.log_file is not None else None if len(job_inis) == 2: # run hazard job_id = run_job(job_inis[0], args.log_level, log_file, args.exports) # run risk run_job(job_inis[1], args.log_level, log_file, args.exports, hazard_calculation_id=job_id) else: run_job( expanduser(args.run), args.log_level, log_file, args.exports, hazard_calculation_id=hc_id) # hazard elif args.list_hazard_calculations: dbcmd('list_calculations', 'hazard') elif args.run_hazard is not None: log_file = expanduser(args.log_file) \ if args.log_file is not None else None run_job(expanduser(args.run_hazard), args.log_level, log_file, args.exports) elif args.delete_calculation is not None: dbcmd('delete_calculation', args.delete_calculation, args.yes) # risk elif args.list_risk_calculations: dbcmd('list_calculations', 'risk') elif args.run_risk is not None: if args.hazard_calculation_id is None: sys.exit(MISSING_HAZARD_MSG) log_file = expanduser(args.log_file) \ if args.log_file is not None else None run_job( expanduser(args.run_risk), args.log_level, log_file, args.exports, hazard_calculation_id=hc_id) # export elif args.make_html_report: print 'Written', make_report(conn, args.make_html_report) sys.exit(0) elif args.list_outputs is not None: hc_id = dbcmd('get_hc_id', args.list_outputs) dbcmd('list_outputs', hc_id) elif args.show_view is not None: job_id, view_name = args.show_view print views.view(view_name, datastore.read(int(job_id))) elif args.show_log is not None: hc_id = dbcmd('get_hc_id', args.show_log[0]) print dbcmd('get_log', hc_id) elif args.export_output is not None: output_id, target_dir = args.export_output dbcmd('export_output', int(output_id), expanduser(target_dir), exports) elif args.export_outputs is not None: job_id, target_dir = args.export_outputs hc_id = dbcmd('get_hc_id', job_id) dbcmd('export_outputs', hc_id, expanduser(target_dir), exports) elif args.delete_uncompleted_calculations: dbcmd('delete_uncompleted_calculations') else: arg_parser.print_usage()