package com.atlassian.stash.internal.johnson.handler.bean;

import com.atlassian.bitbucket.Product;
import com.atlassian.stash.internal.johnson.handler.EventDetails;
import org.springframework.beans.factory.BeanCreationException;

import javax.annotation.Nonnull;

/**
 * If the sessionFactory fails, the most likely cause is that the schema created by Liquibase does not
 * match the Hibernate annotations. Since the "liquibase" bean didn't fail, we can assume the database
 * is available and all of the changesets defined have been successfully applied. That means the most
 * likely problem is that the schema was prepared by a newer version of the system. In AMPS, this can
 * happen when the data version used is newer than the product version. Alternatively, a DBA has messed
 * with the tables or a developer has coded something wrong.
 *
 * @since 2.8.2
 */
public class SessionFactoryFailureHandler implements BeanFailureHandler {

    @Override
    public EventDetails handle(@Nonnull BeanCreationException exception) {
        return new EventDetails("bad-schema",
                "The database schema is not valid. Are you trying to run an older version of " +
                        Product.NAME + " against a newer version of the data?",
                exception.getCause()); // Get the HibernateException
    }
}
