Run MyBatis on startup of application

Asked by At

I have myBatis setup for my account. This by using the migrate command in the command line (in Jenkins). Now I want to integrate this with the application itself (Spring boot). Currently I have different sql files with @Undo and up sql code.

So When I start the Sping boot application I want to run the migrate up command without changing the sql files that I already have? Is this possible in MyBatis and Spring?

1 Answers

1
ave On Best Solutions

This is about MyBatis-Migrations, right?
Spring Boot does not provide out-of-box support, however, it seems to be possible to write a custom DatabasePopulator.

Here is a simple implementation.
It uses Migrations' Runtime Migration feature.

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.migration.ConnectionProvider;
import org.apache.ibatis.migration.FileMigrationLoader;
import org.apache.ibatis.migration.MigrationException;
import org.apache.ibatis.migration.operations.UpOperation;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ScriptException;
import org.springframework.jdbc.datasource.init.UncategorizedScriptException;

public class MyBatisMigrationsPopulator implements DatabasePopulator {
  private final DataSource dataSource;
  private final String scriptsDir;
  private final Properties properties;
  private final DatabaseOperationOption options;

  public MyBatisMigrationsPopulator(DataSource dataSource, String scriptsDir,
      Properties properties, DatabaseOperationOption options) {
    super();
    this.dataSource = dataSource;
    this.scriptsDir = scriptsDir;
    this.properties = properties;
    this.options = options;
  }

  @Override
  public void populate(Connection connection) throws SQLException, ScriptException {
    try {
      new UpOperation().operate(new DataSourceConnectionProvider(dataSource),
        createMigrationsLoader(), options, null);
    } catch (MigrationException e) {
      throw new UncategorizedScriptException("Migration failed.", e.getCause());
    }
  }

  protected FileMigrationLoader createMigrationsLoader() {
    URL url = getClass().getClassLoader().getResource(scriptsDir);
    File dir = new File(url.getFile());
    return new FileMigrationLoader(dir, "utf-8", properties);
  }
}

An example bean definition of DataSourceInitializer:

@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
  // the directory that contains SQL scripts
  String scriptsDir = "scripts";
  String CHANGELOG_TABLE = "databaseChangelog";
  Properties properties = new Properties();
  properties.setProperty("changelog", CHANGELOG_TABLE);
  DatabaseOperationOption options = new DatabaseOperationOption();
  options.setChangelogTable(CHANGELOG_TABLE);
  MyBatisMigrationsPopulator populator = 
    new MyBatisMigrationsPopulator(dataSource, scriptsDir, properties, options);
  DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
  dataSourceInitializer.setDataSource(dataSource);
  dataSourceInitializer.setDatabasePopulator(populator);
  return dataSourceInitializer;
}

Here is an executable demo project.
You may need to modify the datasource settings in application.properties.

Hope this helps!