示例#1
0
    def check_db_version(self, db_version_info, session=None):
        """
        Checks the database version and prints an error message on database
        version mismatch.

        - On mismatching or on missing version a sys.exit(1) is called.
        - On missing DBVersion table, it returns False
        - On compatible DB version, it returns True

        Parameters:
            db_version_info (db_version.DBVersionInfo): required database
                version.
            session: an open database session or None. If session is None, a
                new session is created.
        """

        try:
            dispose_engine = False
            if session is None:
                engine = SQLServer.create_engine(self.get_connection_string())
                dispose_engine = True
                session = CreateSession(engine)
            else:
                engine = session.get_bind()

            if not engine.has_table(quoted_name(DBVersion.__tablename__,
                                                True)):
                LOG.debug("Missing DBVersion table!")
                return False

            version = session.query(DBVersion).first()
            if version is None:
                # Version is not populated yet
                LOG.error('No version information found in the database.')
                sys.exit(1)
            elif not db_version_info.is_compatible(version.major,
                                                   version.minor):
                LOG.error('Version mismatch. Expected database version: ' +
                          str(db_version_info))
                version_from_db = 'v' + str(version.major) + '.' + str(
                    version.minor)
                LOG.error('Version from the database is: ' + version_from_db)
                LOG.error('Please update your database.')
                sys.exit(1)

            LOG.debug("Database version is compatible.")
            return True
        finally:
            session.commit()
            if dispose_engine:
                engine.dispose()
示例#2
0
    def _create_or_update_schema(self, use_migration=True):
        """
        Creates or updates the database schema. The database server should be
        started before this method is called.

        If use_migration is True, this method runs an alembic upgrade to HEAD.

        In the False case, there is no migration support and only SQLAlchemy
        meta data is used for schema creation.

        On error sys.exit(1) is called.
        """

        try:
            db_uri = self.get_connection_string()
            engine = SQLServer.create_engine(db_uri)

            LOG.debug('Update/create database schema')
            if use_migration:
                LOG.debug('Creating new database session')
                session = CreateSession(engine)
                connection = session.connection()

                cfg = config.Config()
                cfg.set_main_option("script_location", self.migration_root)
                cfg.attributes["connection"] = connection
                command.upgrade(cfg, "head")

                session.commit()
            else:
                CC_META.create_all(engine)

            engine.dispose()
            LOG.debug('Update/create database schema done')
            return True

        except sqlalchemy.exc.SQLAlchemyError as alch_err:
            LOG.error(str(alch_err))
            sys.exit(1)
        except CommandError as cerr:
            LOG.error("Database schema and CodeChecker is incompatible."
                      "Please update CodeChecker.")
            LOG.debug(str(cerr))
            sys.exit(1)
示例#3
0
    def _create_or_update_schema(self, use_migration=True):
        """
        Creates or updates the database schema. The database server should be
        started before this method is called.

        If use_migration is True, this method runs an alembic upgrade to HEAD.

        In the False case, there is no migration support and only SQLAlchemy
        meta data is used for schema creation.

        On error sys.exit(1) is called.
        """

        try:
            db_uri = self.get_connection_string()
            engine = SQLServer.create_engine(db_uri)

            LOG.debug('Update/create database schema')
            if use_migration:
                LOG.debug('Creating new database session')
                session = CreateSession(engine)
                connection = session.connection()

                cfg = config.Config()
                cfg.set_main_option("script_location", self.migration_root)
                cfg.attributes["connection"] = connection
                command.upgrade(cfg, "head")

                session.commit()
            else:
                CC_META.create_all(engine)

            engine.dispose()
            LOG.debug('Update/create database schema done')
            return True

        except sqlalchemy.exc.SQLAlchemyError as alch_err:
            LOG.error(str(alch_err))
            sys.exit(1)
        except CommandError as cerr:
            LOG.error("Database schema and CodeChecker is incompatible."
                      "Please update CodeChecker.")
            LOG.debug(str(cerr))
            sys.exit(1)
示例#4
0
    def _add_version(self, db_version_info, session=None):
        """
        Fills the DBVersion table.
        """

        engine = None
        if session is None:
            engine = SQLServer.create_engine(self.get_connection_string())
            session = CreateSession(engine)

        expected = db_version_info.get_expected_version()
        LOG.debug('Adding DB version: ' + str(expected))

        session.add(DBVersion(expected[0], expected[1]))
        session.commit()

        if engine:
            engine.dispose()

        LOG.debug('Adding DB version done!')
示例#5
0
    def check_db_version(self, db_version_info, session=None):
        '''
        Checks the database version and prints an error message on database
        version mismatch.

        - On mismatching or on missing version a sys.exit(1) is called.
        - On missing DBVersion table, it returns False
        - On compatible DB version, it returns True

        Parameters:
            db_version_info (db_version.DBVersionInfo): required database
                version.
            session: an open database session or None. If session is None, a
                new session is created.
        '''

        try:
            dispose_engine = False
            if session is None:
                engine = SQLServer.create_engine(self.get_connection_string())
                dispose_engine = True
                session = CreateSession(engine)
            else:
                engine = session.get_bind()

            if not engine.has_table(quoted_name(DBVersion.__tablename__,
                                                True)):
                LOG.debug("Missing DBVersion table!")
                return False

            version = session.query(DBVersion).first()
            if version is None:
                # Version is not populated yet
                LOG.error('No version information found in the database.')
                sys.exit(1)
            elif not db_version_info.is_compatible(version.major,
                                                   version.minor):
                LOG.error('Version mismatch. Expected database version: ' +
                          str(db_version_info))
                version_from_db = 'v' + str(version.major) + '.' + str(
                    version.minor)
                LOG.error('Version from the database is: ' + version_from_db)
                LOG.error('Please update your database.')
                sys.exit(1)

            LOG.debug("Database version is compatible.")
            return True
        finally:
            session.commit()
            if dispose_engine:
                engine.dispose()
示例#6
0
    def _add_version(self, db_version_info, session=None):
        '''
        Fills the DBVersion table.
        '''

        engine = None
        if session is None:
            engine = SQLServer.create_engine(self.get_connection_string())
            session = CreateSession(engine)

        expected = db_version_info.get_expected_version()
        LOG.debug('Adding DB version: ' + str(expected))

        session.add(DBVersion(expected[0], expected[1]))
        session.commit()

        if engine:
            engine.dispose()

        LOG.debug('Adding DB version done!')