Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- import liquibase.Liquibase;
- import liquibase.database.Database;
- import liquibase.database.DatabaseFactory;
- import liquibase.database.jvm.JdbcConnection;
- import liquibase.exception.LiquibaseException;
- import liquibase.resource.ClassLoaderResourceAccessor;
- import liquibase.resource.FileSystemResourceAccessor;
- import liquibase.resource.ResourceAccessor;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.mycompany.arch.configuration.EnvironmentProperties;
- /**
- * Utility class for tasks related with Liquibase.
- */
- public final class LiquibaseUtils {
- private static final Logger LOG = LoggerFactory.getLogger( LiquibaseUtils.class );
- public static final String ROOT_CHANGELOG_PATH = "db/changelog.xml";
- public static final String FS_CHANGELOG_PREFIX = "file://";
- /**
- * Apply any pending changes to the database.
- *
- * Uses environment properties for connection.
- *
- * @return
- */
- public static boolean applyDatabaseChanges() {
- // Get database connection details from properties file
- Properties envProps = EnvironmentProperties.get();
- String dbDriver = envProps.getProperty( "db.driver" );
- String dbURL = envProps.getProperty( "db.url" );
- String dbUser = envProps.getProperty( "db.owner_username" );
- String dbPass = envProps.getProperty( "db.owner_password" );
- String dbDataUser = envProps.getProperty( "db.username" );
- // Supplemental changelog (used for specific configurations, unit tests, development deployments...)
- String extraChangelog = envProps.getProperty( "ddna.db.extra_changelog" );
- LOG.info( "Applying pending changes to the database..." );
- if( StringUtils.isEmpty( extraChangelog ) ) {
- return applyChangelogs( dbDriver, dbURL, dbUser, dbPass, dbDataUser, false, ROOT_CHANGELOG_PATH );
- }
- else {
- return applyChangelogs( dbDriver, dbURL, dbUser, dbPass, dbDataUser, false, ROOT_CHANGELOG_PATH, FS_CHANGELOG_PREFIX + extraChangelog );
- }
- }
- /**
- * Apply a Liquibase changelog to a database.
- *
- * NB: Changes will be applied directly to the database with no consideration for potential ORM caches.
- *
- * @param dbDriver Driver class for the database
- * @param dbURL JDBC URL for the database to target
- * @param dbUsername Username of the account used to apply changes
- * @param dbPassword Password of the account used to apply changes
- * @param dbDataUsername Username of the account used to manipulate data (used for grants)
- * @param dropFirst Flag indicating if all object should be dropped before apply the changelog (used for unit tests)
- * @param changelogRefs List of changelogs to apply
- * @return
- */
- public static boolean applyChangelogs( String dbDriver, String dbURL, String dbUsername, String dbPassword,
- String dbDataUsername, boolean dropFirst, String... changelogRefs ) {
- boolean success = false;
- Connection conn = null;
- try {
- // Register JDBC driver in case it is not already.
- Class.forName( dbDriver );
- conn = DriverManager.getConnection( dbURL, dbUsername, dbPassword );
- // Username properties for grants
- System.setProperty( "db.changelog.datauser", dbDataUsername );
- System.setProperty( "db.changelog.owneruser", dbUsername );
- Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation( new JdbcConnection( conn ) );
- ResourceAccessor clRsrcAccessor = new ClassLoaderResourceAccessor(LiquibaseUtils.class.getClassLoader());
- ResourceAccessor fileResourceAccessor = new FileSystemResourceAccessor();
- if( dropFirst ) {
- Liquibase liquibase = new Liquibase( "", clRsrcAccessor, database );
- liquibase.dropAll();
- }
- for ( String changeLogRef : changelogRefs ) {
- Liquibase liquibase = null;
- if( changeLogRef.startsWith( FS_CHANGELOG_PREFIX ) ) {
- liquibase = new Liquibase( changeLogRef.substring( FS_CHANGELOG_PREFIX.length() ), fileResourceAccessor, database );
- }
- else {
- liquibase = new Liquibase( changeLogRef, clRsrcAccessor, database );
- }
- liquibase.update( "" );
- }
- success = true;
- } catch ( ClassNotFoundException | SQLException | LiquibaseException e ) {
- LOG.warn( "Error apply [{}] changes to the database [{}]", changelogRefs, dbURL );
- LOG.warn( "Error details: ", e );
- } finally {
- if ( conn != null ) {
- try {
- conn.rollback();
- conn.close();
- } catch ( SQLException e ) {
- // Not much to do at this stage.
- }
- }
- }
- return success;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement