diff -r 7644e68c4e9f -r 70538ea2532d devtools/__init__.py --- a/devtools/__init__.py Wed Apr 13 08:38:24 2011 +0200 +++ b/devtools/__init__.py Wed Apr 13 09:46:29 2011 +0200 @@ -232,6 +232,11 @@ # XXX merge with BaseApptestConfiguration ? class ApptestConfiguration(BaseApptestConfiguration): + # `skip_db_create_and_restore` controls wether or not the test database + # should be created / backuped / restored. If set to True, those + # steps are completely skipped, the database is used as is and is + # considered initialized + skip_db_create_and_restore = False def __init__(self, appid, apphome=None, log_threshold=logging.CRITICAL, sourcefile=None): @@ -260,6 +265,7 @@ self.view('foaf', rset) """ + skip_db_create_and_restore = True read_instance_schema = True # read schema from database @@ -477,13 +483,38 @@ cnx.close() self.backup_database(test_db_id) + +class NoCreateDropDatabaseHandler(TestDataBaseHandler): + """This handler is used if config.skip_db_create_and_restore is True + + This is typically the case with RealDBConfig. In that case, + we explicitely want to skip init / backup / restore phases. + + This handler redefines the three corresponding methods and delegates + to original handler for any other method / attribute + """ + + def __init__(self, base_handler): + self.base_handler = base_handler + + # override init / backup / restore methods + def init_test_database(self): + pass + + def backup_database(self, db_id): + pass + + def restore_database(self, db_id): + pass + + # delegate to original handler in all other cases + def __getattr__(self, attrname): + return getattr(self.base_handler, attrname) + + ### postgres test database handling ############################################ class PostgresTestDataBaseHandler(TestDataBaseHandler): - - # XXX - # XXX PostgresTestDataBaseHandler Have not been tested at all. - # XXX DRIVER = 'postgres' @property @@ -504,14 +535,19 @@ def cursor(self): return self.dbcnx.cursor() + def process_cache_entry(self, directory, dbname, db_id, entry): + backup_name = self._backup_name(db_id) + if backup_name in self.helper.list_databases(self.cursor): + return backup_name + return None + def init_test_database(self): - """initialize a fresh postgresql databse used for testing purpose""" + """initialize a fresh postgresql database used for testing purpose""" from cubicweb.server import init_repository from cubicweb.server.serverctl import system_source_cnx, createdb # connect on the dbms system base to create our base try: self._drop(self.dbname) - createdb(self.helper, self.system_source, self.dbcnx, self.cursor) self.dbcnx.commit() cnx = system_source_cnx(self.system_source, special_privs='LANGUAGE C', @@ -555,7 +591,6 @@ def _drop(self, db_name): if db_name in self.helper.list_databases(self.cursor): - #print 'dropping overwritted database:', db_name self.cursor.execute('DROP DATABASE %s' % db_name) self.dbcnx.commit() @@ -567,7 +602,6 @@ orig_name = self.system_source['db-name'] try: backup_name = self._backup_name(db_id) - #print 'storing postgres backup as', backup_name self._drop(backup_name) self.system_source['db-name'] = backup_name createdb(self.helper, self.system_source, self.dbcnx, self.cursor, template=orig_name) @@ -581,7 +615,6 @@ """Actual restore of the current database. Use the value tostored in db_cache as input """ - #print 'restoring postgrest backup from', backup_coordinates self._drop(self.dbname) createdb(self.helper, self.system_source, self.dbcnx, self.cursor, template=backup_coordinates) @@ -647,7 +680,6 @@ # remove database file if it exists ? dbfile = self.absolute_dbfile() self._cleanup_database(dbfile) - #print 'resto from', backup_coordinates shutil.copy(backup_coordinates, dbfile) repo = self.get_repo() @@ -754,6 +786,8 @@ handlerkls = HANDLERS.get(driver, None) if handlerkls is not None: handler = handlerkls(config) + if config.skip_db_create_and_restore: + handler = NoCreateDropDatabaseHandler(handler) HCACHE.set(config, handler) return handler else: