Java tutorial
/* * 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> * <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> * <property name="driverClassName" value="${database.driver}"/> * etc. * </bean> * * <bean id="databaseUpgrade" class="org.reusables.solidbase.DatabaseUpgrade"> * <property name="upgradeTarget" value="1.0.*"/> * <property name="upgradeFilePath" value="/upgrade-test.sql"/> * <property <b>name="dataSource" ref="dataSource"</b>/> * </bean> * * <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" <b>depends-on="databaseUpgrade"</b>> * <property name="dataSource" ref="dataSource"/> * etc. * </bean> * </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); } }