Search code examples
tomcatdatabase-schemaliquibasebaseline

Run liquibase under Tomcat?


How would one start a liquibase schema db check aginst a base line under Tomcat on a schedule? Like how to deploy liquibase under Tomcat?


Solution

  • What about utilizing ServletContextListener and inside it create new object of Liquibase.

    something like

    import java.sql.Connection;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    import liquibase.Liquibase;
    import liquibase.database.Database;
    import liquibase.database.DatabaseFactory;
    import liquibase.database.jvm.JdbcConnection;
    import liquibase.resource.ClassLoaderResourceAccessor;
    import liquibase.resource.CompositeResourceAccessor;
    import liquibase.resource.FileSystemResourceAccessor;
    import lombok.SneakyThrows;
    
    @WebListener
    public class LiquibaseListener implements ServletContextListener {
    
      @SneakyThrows
      @Override
      public void contextInitialized(ServletContextEvent sce) {
        final Connection connection = null;
        final Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
        final Liquibase liquibase = new Liquibase("changelogpath", new CompositeResourceAccessor(new ClassLoaderResourceAccessor(), new FileSystemResourceAccessor()), database);
        liquibase.update("");
        
      }
    }
    

    Or have a look inside SpringLiquibase how this is done for spring framework and you can get inspiration from there.