# Path: f8a_worker/models.py
# class Ecosystem(Base):
#     """Table for Ecosystem."""
# 
#     __tablename__ = 'ecosystems'
# 
#     id = Column(Integer, primary_key=True)
#     name = Column(String(255), unique=True)
#     url = Column(String(255))
#     fetch_url = Column(String(255))
#     _backend = Column(
#         Enum(*[b.name for b in EcosystemBackend], name='ecosystem_backend_enum'))
# 
#     packages = relationship('Package', back_populates='ecosystem')
#     feedback = relationship('RecommendationFeedback',
#                             back_populates='ecosystem')
# 
#     @property
#     def backend(self):
#         """Get backend property."""
#         return EcosystemBackend[self._backend]
# 
#     @backend.setter
#     def backend(self, backend):
#         """Set backend property."""
#         self._backend = EcosystemBackend(backend).name
# 
#     def is_backed_by(self, backend):
#         """Is this ecosystem backed by specified backend?."""
#         return self.backend == backend
# 
#     @classmethod
#     def by_name(cls, session, name):
#         """Get a row with specified name."""
#         try:
#             return cls._by_attrs(session, name=name)
#         except NoResultFound:
#             # What to do here ?
#             raise


import os
from selinon import DataStorage
from sqlalchemy import create_engine
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from f8a_worker.errors import TaskAlreadyExistsError
from f8a_worker.models import Ecosystem
        remove weird-looking errors like (un-committed changes due to errors
        in init task):
          DETAIL: Key (package_analysis_id)=(1113452) is not present in table "package_analyses".
        """
        if task_name in ('InitPackageFlow', 'InitAnalysisFlow')\
                or issubclass(exc_info[0], TaskAlreadyExistsError):
            return

        # Sanity checks
        if not self.is_connected():
            self.connect()

        res = self._create_result_entry(node_args, flow_name, task_name, task_id, result=result,
                                        error=True)
        try:
            PostgresBase.session.add(res)
            PostgresBase.session.commit()
        except IntegrityError:
            # the result has been already stored before the error occurred
            # hence there is no reason to re-raise
            PostgresBase.session.rollback()
        except SQLAlchemyError:
            PostgresBase.session.rollback()
            raise

    def get_ecosystem(self, name):
        """Get ecosystem by name."""
        if not self.is_connected():
            self.connect()


# Path: f8a_worker/models.py
# class Ecosystem(Base):
#     """Table for Ecosystem."""
# 
#     __tablename__ = 'ecosystems'
# 
#     id = Column(Integer, primary_key=True)
#     name = Column(String(255), unique=True)
#     url = Column(String(255))
#     fetch_url = Column(String(255))
#     _backend = Column(
#         Enum(*[b.name for b in EcosystemBackend], name='ecosystem_backend_enum'))
# 
#     packages = relationship('Package', back_populates='ecosystem')
#     feedback = relationship('RecommendationFeedback',
#                             back_populates='ecosystem')
# 
#     @property
#     def backend(self):
#         """Get backend property."""
#         return EcosystemBackend[self._backend]
# 
#     @backend.setter
#     def backend(self, backend):
#         """Set backend property."""
#         self._backend = EcosystemBackend(backend).name
# 
#     def is_backed_by(self, backend):
#         """Is this ecosystem backed by specified backend?."""
#         return self.backend == backend
# 
#     @classmethod
#     def by_name(cls, session, name):
#         """Get a row with specified name."""
#         try:
#             return cls._by_attrs(session, name=name)
#         except NoResultFound:
#             # What to do here ?
#             raise


# Path: f8a_worker/storages/postgres_base.py
import os
from selinon import DataStorage
from sqlalchemy import create_engine
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from f8a_worker.errors import TaskAlreadyExistsError
from f8a_worker.models import Ecosystem

        remove weird-looking errors like (un-committed changes due to errors
        in init task):
          DETAIL: Key (package_analysis_id)=(1113452) is not present in table "package_analyses".
        """
        if task_name in ('InitPackageFlow', 'InitAnalysisFlow')\
                or issubclass(exc_info[0], TaskAlreadyExistsError):
            return

        # Sanity checks
        if not self.is_connected():
            self.connect()

        res = self._create_result_entry(node_args, flow_name, task_name, task_id, result=result,
                                        error=True)
        try:
            PostgresBase.session.add(res)
            PostgresBase.session.commit()
        except IntegrityError:
            # the result has been already stored before the error occurred
            # hence there is no reason to re-raise
            PostgresBase.session.rollback()
        except SQLAlchemyError:
            PostgresBase.session.rollback()
            raise

    def get_ecosystem(self, name):
        """Get ecosystem by name."""
        if not self.is_connected():
            self.connect()


        return Ecosystem.by_name(PostgresBase.session, name)