001/* 002 * Copyright 2012-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.flyway; 018 019import org.flywaydb.core.Flyway; 020 021import org.springframework.beans.factory.InitializingBean; 022import org.springframework.core.Ordered; 023import org.springframework.util.Assert; 024 025/** 026 * {@link InitializingBean} used to trigger {@link Flyway} migration via the 027 * {@link FlywayMigrationStrategy}. 028 * 029 * @author Phillip Webb 030 * @since 1.3.0 031 */ 032public class FlywayMigrationInitializer implements InitializingBean, Ordered { 033 034 private final Flyway flyway; 035 036 private final FlywayMigrationStrategy migrationStrategy; 037 038 private int order = 0; 039 040 /** 041 * Create a new {@link FlywayMigrationInitializer} instance. 042 * @param flyway the flyway instance 043 */ 044 public FlywayMigrationInitializer(Flyway flyway) { 045 this(flyway, null); 046 } 047 048 /** 049 * Create a new {@link FlywayMigrationInitializer} instance. 050 * @param flyway the flyway instance 051 * @param migrationStrategy the migration strategy or {@code null} 052 */ 053 public FlywayMigrationInitializer(Flyway flyway, 054 FlywayMigrationStrategy migrationStrategy) { 055 Assert.notNull(flyway, "Flyway must not be null"); 056 this.flyway = flyway; 057 this.migrationStrategy = migrationStrategy; 058 } 059 060 @Override 061 public void afterPropertiesSet() throws Exception { 062 if (this.migrationStrategy != null) { 063 this.migrationStrategy.migrate(this.flyway); 064 } 065 else { 066 this.flyway.migrate(); 067 } 068 } 069 070 @Override 071 public int getOrder() { 072 return this.order; 073 } 074 075 public void setOrder(int order) { 076 this.order = order; 077 } 078 079}