V2__TestMigration.java

package db.migration;

import db.BaseMigration;
import org.apache.log4j.Logger;
import org.flywaydb.core.api.migration.Context;

import java.sql.ResultSet;
import java.sql.Statement;

public class V2__TestMigration extends BaseMigration {
     Logger log = Logger.getLogger(V2__TestMigration.class);

    public void migrate(Context context) throws Exception {
        log.debug("------test java migration ");
        log.debug(context.getConnection().getClientInfo());
        try (Statement select = context.getConnection().createStatement()) {
            try (ResultSet rows = select.executeQuery("SELECT id FROM users ORDER BY id")) {
                while (rows.next()) {
                    int id = rows.getInt(1);
                    try (Statement update = context.getConnection().createStatement()) {
                        update.execute("UPDATE users SET incorrectloginattempts=0 WHERE id=" + id);
                    }
                }
            }
        }
    }

    @Override
    public Integer getChecksum() {
        try {
            return new Long(checksum(this.getClass())).intValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /// EXAMPLES
/*
    public void migrate(Context context) throws Exception {
        try (PreparedStatement statement =
                     context
                             .getConnection()
                             .prepareStatement("INSERT INTO test_user (name) VALUES ('Obelix')")) {
            statement.execute();
        }
    }*/

    //   spring example
/*    public class V1_2__Another_user extends BaseJavaMigration {
        public void migrate(Context context) {
            new JdbcTemplate(new SingleConnectionDataSource(context.getConnection(), true))
                    .execute("INSERT INTO test_user (name) VALUES ('Obelix')");
        }
    }*/
}