<commit_before>/*
 * Copyright 2014 "<NAME>" <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package javasnack.flywaydb;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.AbstractKeyedHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.googlecode.flyway.core.Flyway;
import com.googlecode.flyway.core.api.MigrationInfo;
import com.googlecode.flyway.core.api.MigrationInfoService;
import com.googlecode.flyway.core.api.MigrationState;
import com.googlecode.flyway.core.api.MigrationVersion;

public class FlywaydbDemo3Test {

    Connection conn;

    class T1 {
        public long id;
        public String name;
        public int age;
        public String hobby;
    }

    @BeforeEach
    public void prepareDb() throws Exception {
        Class.forName("org.h2.Driver");
        conn = DriverManager.getConnection("jdbc:h2:mem:flywaydb_demo3", "sa",
                "");
        // prepare empty database.
    }

    @AfterEach
    public void closeDb() throws SQLException {
        conn.close();
    }

    @Test
    public void usingPlaceholderAndEncoding() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("flyway.user", "sa");
        properties.setProperty("flyway.password", "");
        properties.setProperty("flyway.url",
                "jdbc:h2:mem:flywaydb_demo3;DB_CLOSE_DELAY=-1");
        properties.setProperty("flyway.driver", "org.h2.Driver");

        final Flyway flyway = new Flyway();
        flyway.configure(properties);
        flyway.setLocations("flywaydbdemos/demo3");

        flyway.setEncoding("UTF-8");
        Map<String, String> placeholders = new HashMap<>();
        placeholders.put("L1", "abc");
        placeholders.put("L2", "def");
        placeholders.put("L3", "abc");
        placeholders.put("L4", "def");
        flyway.setPlaceholders(placeholders);

        MigrationInfoService mis = flyway.info();
        assertEquals(3, mis.all().length);
        for (MigrationInfo mi : mis.all()) {
            System.out.println(mi.getVersion());
            System.out.println(mi.getDescription());
            System.out.println(mi.getState());
            System.out.println(mi.getType());

        }
        // 3 migrations (V1, V1.1, V1.2) must be pending status.
        assertEquals(3, mis.pending().length);
        // no version applied.
        assertEquals(0, mis.applied().length);
        // no current version.
        MigrationInfo mi = mis.current();
        assertNull(mi);

        // migrate to V1.1
        flyway.setTarget(MigrationVersion.fromVersion("1.1"));
        flyway.migrate();
        mis = flyway.info();
        assertEquals(3, mis.all().length);
        // no pending, V1.2 -> "ABOVE_TARGET".
        assertEquals(0, mis.pending().length);
        // V1, V1.1, V1.2 were applied.
        assertEquals(2, mis.applied().length);
        for (MigrationInfo _mi : mis.all()) {
            System.out.println(_mi.getVersion());
            System.out.println(_mi.getDescription());
            System.out.println(_mi.getState());
            System.out.println(_mi.getType());

        }
        mi = mis.current();
        assertEquals("1.1", mi.getVersion().getVersion());
        assertEquals("add t1 hobby column", mi.getDescription());
        assertEquals(MigrationState.SUCCESS, mi.getState());

        // change placeholder prefix and suffix.
        flyway.setPlaceholderPrefix("%{%");
        flyway.setPlaceholderSuffix("%}%");

        // migrate to latest version
        flyway.setTarget(MigrationVersion.LATEST);
        flyway.migrate();
        mis = flyway.info();
        assertEquals(3, mis.all().length);
        assertEquals(0, mis.pending().length);
        assertEquals(3, mis.applied().length);
        mi = mis.current();
        assertEquals("1.2", mi.getVersion().getVersion());
        assertEquals("insert t1 data2", mi.getDescription());
        assertEquals(MigrationState.SUCCESS, mi.getState());

        // select and validate records.
        QueryRunner run = new QueryRunner();
        ResultSetHandler<Map<Long, T1>> h = new AbstractKeyedHandler<Long, T1>() {
            @Override
            protected T1 createRow(ResultSet rs) throws SQLException {
                T1 row = new T1();
                row.id = rs.getLong("id");
                row.name = rs.getString("name");
                row.age = rs.getInt("age");
                row.hobby = rs.getString("hobby");
                return row;<commit_msg>migrate FlywaydbDemo3Test to latest flywaydb api.<commit_after>/*
 * Copyright 2014 "<NAME>" <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package javasnack.flywaydb;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.AbstractKeyedHandler;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.flywaydb.core.api.MigrationInfoService;
import org.flywaydb.core.api.MigrationState;
import org.flywaydb.core.api.MigrationVersion;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class FlywaydbDemo3Test {

    Connection conn;

    class T1 {
        public long id;
        public String name;
        public int age;
        public String hobby;
    }

    @BeforeEach
    public void prepareDb() throws Exception {
        Class.forName("org.h2.Driver");
        conn = DriverManager.getConnection("jdbc:h2:mem:flywaydb_demo3", "sa",
                "");
        // prepare empty database.
    }

    @AfterEach
    public void closeDb() throws SQLException {
        conn.close();
    }

    @Test
    public void usingPlaceholderAndEncoding() throws Exception {
        final Map<String, String> placeholders = new HashMap<>();
        placeholders.put("L1", "abc");
        placeholders.put("L2", "def");
        placeholders.put("L3", "abc");
        placeholders.put("L4", "def");
        final Flyway flyway0 = Flyway.configure()
                .dataSource("jdbc:h2:mem:flywaydb_demo3;DB_CLOSE_DELAY=-1", "sa", "")
                .locations("flywaydbdemos/demo3")
                .encoding(StandardCharsets.UTF_8)
                .placeholders(placeholders)
                .load();

        // before calling baseline() method
        MigrationInfoService mis = flyway0.info();
        assertEquals(3, mis.all().length);
        for (MigrationInfo mi : mis.all()) {
            System.out.println(mi.getVersion());
            System.out.println(mi.getDescription());
            System.out.println(mi.getState());
            System.out.println(mi.getType());
        }
        // 3 migrations (V1, V1.1, V1.2) must be pending status.
        assertEquals(3, mis.pending().length);
        // no version applied.
        assertEquals(0, mis.applied().length);
        // no current version.
        MigrationInfo mi = mis.current();
        assertNull(mi);

        // migrate to V1.1
        final Flyway flyway1 = Flyway.configure()
                .configuration(flyway0.getConfiguration())
                .target(MigrationVersion.fromVersion("1.1"))
                .load();
        flyway1.migrate();
        mis = flyway1.info();
        assertEquals(3, mis.all().length);
        // no pending, V1.2 -> "ABOVE_TARGET".
        assertEquals(0, mis.pending().length);
        // V1, V1.1, V1.2 were applied.
        assertEquals(2, mis.applied().length);
        for (MigrationInfo _mi : mis.all()) {
            System.out.println(_mi.getVersion());
            System.out.println(_mi.getDescription());
            System.out.println(_mi.getState());
            System.out.println(_mi.getType());

        }
        mi = mis.current();
        assertEquals("1.1", mi.getVersion().getVersion());
        assertEquals("add t1 hobby column", mi.getDescription());
        assertEquals(MigrationState.SUCCESS, mi.getState());

        final Flyway flyway2 = Flyway.configure()
                .configuration(flyway0.getConfiguration())
                // change placeholder prefix and suffix.
                .placeholderPrefix("%{%")
                .placeholderSuffix("%}%")
                // migrate to latest version
                .target(MigrationVersion.LATEST)
                .load();
        flyway2.migrate();
        mis = flyway2.info();
        assertEquals(3, mis.all().length);
        assertEquals(0, mis.pending().length);
        assertEquals(3, mis.applied().length);
        mi = mis.current();
        assertEquals("1.2", mi.getVersion().getVersion());
        assertEquals("insert t1 data2", mi.getDescription());
        assertEquals(MigrationState.SUCCESS, mi.getState());

        // select and validate records.
        QueryRunner run = new QueryRunner();
        ResultSetHandler<Map<Long, T1>> h = new AbstractKeyedHandler<Long, T1>() {
            @Override
            protected T1 createRow(ResultSet rs) throws SQLException {
                T1 row = new T1();
                row.id = rs.getLong("id");
                row.name = rs.getString("name");
                row.age = rs.getInt("age");
                row.hobby = rs.getString("hobby");
                return row;
{'fraction_non_alphanumeric': 0.12648970747562296, 'fraction_numerical': 0.011917659804983749, 'mean_word_length': 2.549182954181352, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055898', 'n_tokens_mistral': 3139, 'n_tokens_neox': 3082, 'n_words': 707}