Initial commit
This commit is contained in:
commit
880e39de2e
511 files changed
+38363
No files matched your search
@@ -0,0 +1,6 @@
|
||||
Portal Migration
|
||||
==
|
||||
|
||||
The portal_migration module contains flyway migration information to migrate the databae during deploy or apply any updates that are required to the database. No migration will happen if a migration file has been modified from when it was initially deployed.
|
||||
|
||||
Each time a modification to the database has to occur, a new migration file needs to be created to apply the modification. This allows for any older versions of the software to be upgraded to the latest version without having to worry about migrating the database
|
||||
@@ -0,0 +1,79 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.locusworks</groupId>
|
||||
<artifactId>portal</artifactId>
|
||||
<version>1.0.0-RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>portal_migration</artifactId>
|
||||
<name>Portal Database Migration</name>
|
||||
<description>Migration handler</description>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<main.basedir>${project.parent.basedir}</main.basedir>
|
||||
<database.driver>com.mysql.jdbc.Driver</database.driver>
|
||||
</properties>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>findbugs-maven-plugin</artifactId>
|
||||
<version>${findbugs.version}</version>
|
||||
<configuration>
|
||||
<xmlOutput>true</xmlOutput>
|
||||
<xmlOutputDirectory>target/site/${project.artifactId}</xmlOutputDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<!-- executing Flyway migration in the "process-sources" phase -->
|
||||
<plugin>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<outOfOrder>false</outOfOrder>
|
||||
<skip>false</skip>
|
||||
<schemas>
|
||||
<schema> portal </schema>
|
||||
</schemas>
|
||||
<table> _flyway_migration </table>
|
||||
<locations>
|
||||
<location>
|
||||
filesystem:${basedir}/../portal_database/src/main/resources/database/migration
|
||||
</location>
|
||||
</locations>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-sources</phase>
|
||||
<goals>
|
||||
<goal>migrate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.locusworks</groupId>
|
||||
<artifactId>portal_database</artifactId>
|
||||
<version>${current.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.locusworks</groupId>
|
||||
<artifactId>portal_common</artifactId>
|
||||
<version>${current.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package net.locusworks.portal.migration.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.MigrationInfo;
|
||||
import org.flywaydb.core.internal.info.MigrationInfoDumper;
|
||||
|
||||
import net.locusworks.common.logger.ApplicationLogger;
|
||||
import net.locusworks.common.logger.ApplicationLoggerFactory;
|
||||
|
||||
/**
|
||||
* Migration item class to help with flyway migrations
|
||||
* @author Isaac Parenteau
|
||||
* @since 1.0.0-RELEASE
|
||||
* @version 1.0
|
||||
*/
|
||||
public class MigrationItem {
|
||||
|
||||
private static ApplicationLogger logger = ApplicationLoggerFactory.getLogger(MigrationItem.class);
|
||||
|
||||
protected Flyway flyway = null;
|
||||
protected MigrationInfo[] pendingMigrations;
|
||||
protected MigrationInfo[] allMigrations;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param dataSource The datasource to connect to for the migration
|
||||
* @param flyway The flyway migration object to migrate
|
||||
*/
|
||||
public MigrationItem(Flyway flyway) {
|
||||
allMigrations = flyway.info().all();
|
||||
pendingMigrations = flyway.info().pending();
|
||||
this.flyway = flyway;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current schemas for migration
|
||||
* @return array of schemas
|
||||
*/
|
||||
public String[] getSchemas() {
|
||||
return flyway.getSchemas();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of pending migrations
|
||||
* @return number of pending migrations
|
||||
*/
|
||||
public int qtyPending() {
|
||||
return pendingMigrations.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the migration
|
||||
*/
|
||||
public void migrate() {
|
||||
// no harm in calling migrate even if none pending, log will contain
|
||||
// assurance that the migrations were verified
|
||||
try {
|
||||
String schemas = Arrays.toString(getSchemas()).replace("[", "").replace("]", "");
|
||||
logger.info(String.format("Migration status for %s:%n%s", schemas, getAllMigrationsLog()));
|
||||
flyway.migrate();
|
||||
} catch (Exception e) {
|
||||
String message = String.format("%nDatabase migration error:%n %s %nPortal webapp cannot continue.", e.getMessage());
|
||||
logger.error(message);
|
||||
throw new RuntimeException(message, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the migration logs
|
||||
* @return the migration log string
|
||||
*/
|
||||
public String getAllMigrationsLog() {
|
||||
return MigrationInfoDumper.dumpToAsciiTable(allMigrations);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package net.locusworks.portal.migration.util;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import net.locusworks.common.logger.ApplicationLogger;
|
||||
import net.locusworks.common.logger.ApplicationLoggerFactory;
|
||||
import net.locusworks.common.utils.Utils;
|
||||
import net.locusworks.portal.common.PortalDataSource;
|
||||
import net.locusworks.portal.database.migration.PortalMigration;
|
||||
|
||||
/**
|
||||
* Component for handling the flyway migrations
|
||||
* @author Isaac Parenteau
|
||||
* @since 1.0.0-RELEASE
|
||||
* @version 1.0
|
||||
*/
|
||||
@Component
|
||||
public class MigrationManager {
|
||||
|
||||
@Autowired
|
||||
private PortalDataSource portalDatasource;
|
||||
|
||||
private List<MigrationItem> migrations;
|
||||
|
||||
private static ApplicationLogger logger = ApplicationLoggerFactory.getLogger(MigrationManager.class);
|
||||
|
||||
public MigrationManager() { }
|
||||
|
||||
/**
|
||||
* Perform the migrations
|
||||
*/
|
||||
public void migrate() {
|
||||
logger.info("Performing database migrations");
|
||||
|
||||
DataSource ds = portalDatasource.getPortalRootDataSource();
|
||||
migrations = Utils.toList( new MigrationItem(PortalMigration.getFlyway(ds)) );
|
||||
migrations.forEach(item -> item.migrate());
|
||||
portalDatasource.closeRootDataSource();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user