org.reusables.solidbase.DatabaseUpgrade.java Source code

Java tutorial

Introduction

Here is the source code for org.reusables.solidbase.DatabaseUpgrade.java

Source

/*
 * Copyright 2010-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.reusables.solidbase;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Required;

/**
 * Upgrade the database to the requered version at the starup of a Spring application context.
 * 
 * <p/>
 * 
 * In the following example the database is upgraded on startup so that the application can use the correct database setup:
 * 
 * <code><pre>
 * &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
 *    &lt;property name="driverClassName" value="${database.driver}"/&gt;
 *    etc.
 * &lt;/bean&gt;
 * 
 * &lt;bean id="databaseUpgrade" class="org.reusables.solidbase.DatabaseUpgrade"&gt;
 *    &lt;property name="upgradeTarget" value="1.0.*"/&gt;
 *    &lt;property name="upgradeFilePath" value="/upgrade-test.sql"/&gt;
 *    &lt;property <b>name="dataSource" ref="dataSource"</b>/&gt;
 * &lt;/bean&gt;
 * 
 * &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" <b>depends-on="databaseUpgrade"</b>&gt;
 *    &lt;property name="dataSource" ref="dataSource"/&gt;
 *    etc.
 * &lt;/bean&gt;
 * </pre></code>
 * <p/>
 * The Hibernate sessionFactory depends on the databaseUpgrade bean so that the database is upgraded before any hibernate activity. If you don't use Hibernate
 * you could for example also let your transactionManager depend on the databaseUpgrade bean.
 * 
 * @author marcel
 **/
public class DatabaseUpgrade implements InitializingBean {
    private String upgradeTarget;
    private String upgradeFilePath = "/upgrade.sql";
    private boolean downgradeAllowed = false;
    private DataSource dataSource;

    /**
     * @param upgradeTarget The upgrade target, e.g. 1.0.*.
     */
    @Required
    public void setUpgradeTarget(final String upgradeTarget) {
        this.upgradeTarget = upgradeTarget;
    }

    /**
     * @param upgradeFilePath The path of the upgrade script to use, default is "/upgrade.sql".
     */
    public void setUpgradeFilePath(final String upgradeFilePath) {
        this.upgradeFilePath = upgradeFilePath;
    }

    /**
     * @param downgradeAllowed Indicates that downgrade paths are allowed to reach the given target.
     */
    public void setDowngradeAllowed(final boolean downgradeAllowed) {
        this.downgradeAllowed = downgradeAllowed;
    }

    /**
     * @param dataSource The dataSource to use.
     */
    @Required
    public void setDataSource(final DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public final void afterPropertiesSet() throws Exception {
        SolidBaseUtils.upgradeDatabase(this.dataSource, this.upgradeFilePath, this.upgradeTarget,
                this.downgradeAllowed);
    }
}