All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javalite.db_migrator.MigrationManager Maven / Gradle / Ivy

The newest version!
package org.javalite.db_migrator;

import org.apache.maven.plugin.logging.Log;
import org.javalite.activejdbc.Base;
import org.javalite.cassandra.jdbc.CassandraJDBCConnection;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static org.javalite.db_migrator.DbUtils.databaseType;

public class MigrationManager {

    private DatabaseType dbType;
    private VersionStrategy versionStrategy;
    private MigrationResolver migrationResolver;


    public MigrationManager(String migrationLocation, String url) throws SQLException {
        this.dbType = determineDatabaseType();
        migrationResolver = new MigrationResolver(migrationLocation);
        String databaseName;
        if(url != null){
            databaseName = DbUtils.extractDatabaseName(url);
        }else{
            throw new IllegalArgumentException("URL cannot be null");
        }
        versionStrategy = new VersionStrategy(databaseName, dbType);
    }

    /**
     * Returns pending migrations.
     *
     * @return a sorted set of pending migrations
     */
    public List getPendingMigrations() {
        List appliedMigrations = getAppliedMigrationVersions();

        List allMigrations = migrationResolver.resolve();
        List pendingMigrations = new ArrayList<>();
        for (Migration migration : allMigrations) {
            if(!appliedMigrations.contains(migration.getVersion())){
                pendingMigrations.add(migration);
            }
        }
        return pendingMigrations;
    }

    /**
     * Migrates the database to the latest version, enabling migrations if necessary.
     */
    public void migrate(Log log, String encoding) {

        createSchemaVersionTableIfDoesNotExist();

        final Collection pendingMigrations = getPendingMigrations();

        if (pendingMigrations.isEmpty()) {
            log.info("No new migrations are found");
            return;
        }
        log.info("Migrating database, applying " + pendingMigrations.size() + " migration(s)");
        Migration currentMigration = null;

        try {
            Base.connection().setAutoCommit(false);
            for (Migration migration : pendingMigrations) {
                currentMigration = migration;
                log.info("Running migration " + currentMigration.getName());
                long start = System.currentTimeMillis();

                currentMigration.migrate(encoding);
                versionStrategy.recordMigration(currentMigration.getVersion(), new Date(start), (start - System.currentTimeMillis()));
                Base.connection().commit();
            }
        } catch (Exception e) {
            try{Base.connection().rollback();}catch(Exception ex){throw new MigrationException(e);}
            assert currentMigration != null;
            throw new MigrationException("Migration for version " + currentMigration.getVersion() + " failed, rolling back and terminating migration.", e);
        }
        log.info("Migrated database");
    }

    private DatabaseType determineDatabaseType() throws SQLException {
        Connection connection =  Base.connection();
        return connection instanceof CassandraJDBCConnection ? DatabaseType.CASSANDRA
                : databaseType(Base.connection().getMetaData().getURL());
    }

    private boolean versionTableExists() {
        return versionStrategy.versionTableExists();
    }

    public void createSchemaVersionTableIfDoesNotExist() {
        if (!versionTableExists()) {
            versionStrategy.createSchemaVersionTable(dbType);
        }
    }

    private List getAppliedMigrationVersions() {
        return versionStrategy.getAppliedMigrations();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy