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

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

There is a newer version: 3.5-j11
Show newest version
package org.javalite.db_migrator;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.*;
import java.util.Date;

import static java.lang.String.format;
import static org.javalite.db_migrator.DatabaseType.HSQL;
import static org.javalite.db_migrator.DatabaseType.SQL_SERVER;
import static org.javalite.db_migrator.DbUtils.*;


class DefaultMap extends HashMap {
    private final String DEFAULT_VALUE = "create table %s (%s varchar(32) not null unique, %s timestamp not null, %s int not null)";

    @Override
    public String get(Object key) {
        return containsKey(key) ? super.get(key) : DEFAULT_VALUE;
    }
}

/**
 * A trivial VersionStrategy which tracks only the minimal information required to note the current state of the database: the current version.
 */
public class VersionStrategy {
    public static final String VERSION_TABLE = "schema_version";
    public static final String VERSION_COLUMN = "version";
    public static final String APPLIED_DATE_COLUMN = "applied_on";
    public static final String DURATION_COLUMN = "duration";

    private static final Logger LOGGER = LoggerFactory.getLogger(VersionStrategy.class);

    private static final Map CREATE_VERSION_TABLE_MAP;

    static {
        CREATE_VERSION_TABLE_MAP = new DefaultMap();
        CREATE_VERSION_TABLE_MAP.put(HSQL, "create table %s (%s varchar not null, %s datetime not null, %s int not null, constraint %2$s_unique unique (%2$s))");
        CREATE_VERSION_TABLE_MAP.put(SQL_SERVER, "create table %s (%s varchar(32) not null unique, %s datetime not null, %s int not null)");
    }


    public void createSchemaVersionTable(DatabaseType dbType) {
        String ddl = format(CREATE_VERSION_TABLE_MAP.get(dbType), VERSION_TABLE, VERSION_COLUMN, APPLIED_DATE_COLUMN, DURATION_COLUMN);
        exec(ddl);
    }

    public boolean versionTableExists() {
        try {
            countRows(VERSION_TABLE);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public List getAppliedMigrations()  {
        Statement st = null;
        ResultSet rs = null;
        try{
            st = connection().createStatement();
            rs = st.executeQuery("select " + VERSION_COLUMN + " from " + VERSION_TABLE);
            List migrations = new ArrayList();
            while (rs.next()){
                migrations.add(rs.getString(1));
            }
            return migrations;
        }catch(SQLException e){
            throw new MigrationException(e);
        }finally {
            closeQuietly(rs);
            closeQuietly(st);
        }
    }

    public void recordMigration(String version, Date startTime, long duration) {
        PreparedStatement st = null;
        try{
            st = connection().prepareStatement("insert into " + VERSION_TABLE + " values (?, ?, ?)");
            st.setString(1, version);
            st.setTimestamp(2, new Timestamp(startTime.getTime()));
            st.setLong(3, duration);
            st.execute();
        }catch(Exception e){
            throw  new MigrationException(e);
        }finally {
            closeQuietly(st);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy