Exemple #1
0
 def setUp(self):
     """Setup method."""
     ForsetiTestCase.setUp(self)
     self.engine, self.dbfile = create_test_engine_with_file()
     session_maker = sessionmaker()
     self.session = session_maker(bind=self.engine)
     storage.initialize(self.engine)
     scanner_dao.initialize(self.engine)
     self.session.flush()
     self.violation_access = scanner_dao.ViolationAccess(self.session)
     self.inv_index_id1, self.inv_index_id2, self.inv_index_id3 = (
         _setup_inv_indices(self.session))
    print(sys.argv)
    DB_CONN_STR = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_DB_CONN_STR

    SQL_ENGINE = general_dao.create_engine(DB_CONN_STR, pool_recycle=3600)

    # Drop the CaiTemporaryStore table to ensure it is using the
    # latest schema.
    inventory_dao.initialize(SQL_ENGINE)
    INVENTORY_TABLES = inventory_dao.BASE.metadata.tables
    CAI_TABLE = INVENTORY_TABLES.get(
        inventory_dao.CaiTemporaryStore.__tablename__)
    CAI_TABLE.drop(SQL_ENGINE)

    # Create tables if not exists.
    inventory_dao.initialize(SQL_ENGINE)
    scanner_dao.initialize(SQL_ENGINE)

    # Find all the child classes inherited from declarative base class.
    SCANNER_DAO_CLASSES = _find_subclasses(scanner_dao.BASE)

    INVENTORY_DAO_CLASSES = _find_subclasses(inventory_dao.BASE)
    INVENTORY_DAO_CLASSES.extend([inventory_dao.CaiTemporaryStore])

    DECLARITIVE_BASE_MAPPING = {
        scanner_dao.BASE: SCANNER_DAO_CLASSES,
        inventory_dao.BASE: INVENTORY_DAO_CLASSES
    }

    for declaritive_base, classes in DECLARITIVE_BASE_MAPPING.items():
        declaritive_base.metadata.bind = SQL_ENGINE
        migrate_schema(declaritive_base, classes)