示例#1
0
 def clean_type(self, cls):
     """Delete all entities of the given class."""
     try:
         self._session.query(cls).delete()
     except sqlalchemy.exc.OperationalError as e:
         six.reraise(
             avalon.exc.OperationalError,
             avalon.exc.OperationalError('{0}'.format(e)),
             sys.exc_info()[2])
示例#2
0
def _flush_session(session):
    """Issue any pending changes in the session to the database.

    We do this to attempt to uncover potential transient errors (such
    as not having write access) with the database as soon as possible
    when writing new entries.
    """
    try:
        session.flush()
    except sqlalchemy.exc.OperationalError as e:
        six.reraise(
            avalon.exc.OperationalError,
            avalon.exc.OperationalError('{0}'.format(e)),
            sys.exc_info()[2])
示例#3
0
    def connect(self):
        """Connect to the database and configure the session factory
        to use the connection, and create any needed tables (if they
        do not already exist).

        Required tables for all models will be created if they do not already
        exist. If they do exist, they will not be modified or altered.

        :raises avalon.exc.ConnectionError: If there was a problem connecting
            to the database.
        :raises avalon.exc.OperationalError: If there was a problem creating
            any of the needed tables for models.
        """
        try:
            # Attempt to connect to the engine to make sure it's valid and
            # flush out any errors we're going to encounter before trying
            # to create tables or insert into it.
            self.validate()
        except OperationalError as e:
            six.reraise(
                avalon.exc.ConnectionError,
                avalon.exc.ConnectionError(
                    'Could not connect to {0} database: {1}'.format(self._engine.name, e)),
                sys.exc_info()[2])

        self._session_factory.configure(bind=self._engine)

        try:
            # Attempt to catch and wrap errors here related to not being able
            # to create tables for each model. It's entirely possible that the
            # tables already exist and we won't actually encounter a permission
            # error until we try to rescan (and delete / insert) a collection.
            self._metadata.create_all(self._engine)
        except OperationalError as e:
            six.reraise(
                avalon.exc.OperationalError,
                avalon.exc.OperationalError(
                    'Could not initialize required schema: {0}'.format(e)),
                sys.exc_info()[2])