throws SQLException
{
try
{
// Setup Flyway API against our database
Flyway flyway = setupFlyway(datasource);
// Set whethe Flyway will run migrations "out of order". By default, this is false,
// and Flyway ONLY runs migrations that have a higher version number.
flyway.setOutOfOrder(outOfOrder);
// If a target version was specified, tell Flyway to ONLY migrate to that version
// (i.e. all later migrations are left as "pending"). By default we always migrate to latest version.
if(!StringUtils.isBlank(targetVersion))
{
flyway.setTarget(targetVersion);
}
// Does the necessary Flyway table ("schema_version") exist in this database?
// If not, then this is the first time Flyway has run, and we need to initialize
// NOTE: search is case sensitive, as flyway table name is ALWAYS lowercase,
// See: http://flywaydb.org/documentation/faq.html#case-sensitive
if(!tableExists(connection, flyway.getTable(), true))
{
// Try to determine our DSpace database version, so we know what to tell Flyway to do
String dbVersion = determineDBVersion(connection);
// If this is a fresh install, dbVersion will be null
if (dbVersion==null)
{
// Initialize the Flyway database table with defaults (version=1)
flyway.init();
}
else
{
// Otherwise, pass our determined DB version to Flyway to initialize database table
flyway.setInitVersion(dbVersion);
flyway.setInitDescription("Initializing from DSpace " + dbVersion + " database schema");
flyway.init();
}
}
// Determine pending Database migrations
MigrationInfo[] pending = flyway.info().pending();
// As long as there are pending migrations, log them and run migrate()
if (pending!=null && pending.length>0)
{
log.info("Pending DSpace database schema migrations:");
for (MigrationInfo info : pending)
{
log.info("\t" + info.getVersion() + " " + info.getDescription() + " " + info.getType() + " " + info.getState());
}
// Run all pending Flyway migrations to ensure the DSpace Database is up to date
flyway.migrate();
// Flag that Discovery will need reindexing, since database was updated
setReindexDiscovery(true);
}
else