Zestaw zmian kodu Java w liquibase

Czy istnieje sposób w liquibase, aby utworzyć zestaw zmian kodu java (tzn. podać klasę java, która otrzyma połączenie JDBC i wykona pewne zmiany w bazie danych)?

(wiem, że flyway ma taką funkcję)

Author: Pavel Bernshtam, 2012-08-16

2 answers

Tak, jest taka cecha. Możesz utworzyć customChange:

    <customChange class="my.java.Class">
        <param name="id" value="2" />
    </customChange>

Klasa musi implementować interfejs liquibase.change.custom.CustomTaskChange.

@Override
public void execute(final Database arg0) throws CustomChangeException {
    JdbcConnection dbConn = (JdbcConnection) arg0.getConnection();
    try {
         ... do funny stuff ...
    } catch (Exception e) {
        // swallow the exception !
    }
}
 44
Author: poussma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-31 22:23:27

Kompletny przykład będzie wyglądał tak

Utwórz klasę, która implementuje CustomTaskChange lub CustomSqlChange.

package com.example;

import liquibase.change.custom.CustomTaskChange;
import liquibase.database.Database;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.CustomChangeException;
import liquibase.exception.SetupException;
import liquibase.exception.ValidationErrors;
import liquibase.logging.LogFactory;
import liquibase.resource.ResourceAccessor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DataLoaderTask implements CustomTaskChange {

    //to hold the parameter value
    private String file;


    private ResourceAccessor resourceAccessor;


    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }


    @Override
    public void execute(Database database) throws CustomChangeException {
        JdbcConnection databaseConnection = (JdbcConnection) database.getConnection();
        try {

            //Opening my data file
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(resourceAccessor.getResourceAsStream(file)));

            //Ignore header
            String str = in.readLine();

            while ((str = in.readLine()) != null && !str.trim().equals("")) {
                LogFactory.getLogger().info("Processing line "+ str);
                //Do whatever is necessary
            }
            in.close();
        } catch (Exception e) {
            throw new CustomChangeException(e);
        }
    }

    @Override
    public String getConfirmationMessage() {
        return null;
    }

    @Override
    public void setUp() throws SetupException {

    }

    @Override
    public void setFileOpener(ResourceAccessor resourceAccessor) {
        this.resourceAccessor = resourceAccessor;
    }

    @Override
    public ValidationErrors validate(Database database) {
        return null;
    }

}

W changeset xml można użyć klasy jak poniżej

<changeSet id="1" author="murali" runAlways="false" failOnError="true" >
        <customChange class="com.example.DataLoaderTask">
            <param name="file" value="/com/example/data/user.csv" />
        </customChange>
</changeSet>

Dla mnie plik danych znajduje się w katalogu src/main/resources/com/example/data

Hope this helps

 26
Author: Murali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-11 11:42:52