DatabaseSchemaCreator.java

/*
 * Copyright 2020 Global Crop Diversity Trust
 *
 * 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 org.gringlobal.spring;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ResourceLoader;

import liquibase.exception.LiquibaseException;
import liquibase.integration.spring.SpringLiquibase;

/**
 * Create schema and load initial data if no tables are found in the target
 * database!
 * 
 * @author Matija Obreza
 */
@Slf4j
public class DatabaseSchemaCreator {

	private DataSource dataSource;

	private boolean enabled = false;

	// Required by SpringLiquibase
	private ResourceLoader resourceLoader;

	// The list of Liquibase changeLog scripts to execute
	private String[] scripts;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void setScripts(String... liquibaseChangeLogs) {
		this.scripts = liquibaseChangeLogs;
	}

	public void setResourceLoader(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void createDatabaseSchema() throws SQLException, LiquibaseException {
		if (enabled && countTables() == 0) {
			assert (resourceLoader != null);
			assert (scripts != null);
			assert (scripts.length > 0);

			initializeDatabaseSchema();
		}
	}

	private int countTables() throws SQLException {
		assert (dataSource != null);

		int tableCount = 0;

		try (Connection conn = dataSource.getConnection()) {
			log.info("Inspecting catalog={} schema={}", conn.getCatalog(), conn.getSchema());
			DatabaseMetaData md = conn.getMetaData();
			try (ResultSet rs = md.getTables(conn.getCatalog(), conn.getSchema(), null, null)) {
				while (rs.next()) {
					log.debug("Found table {}.{}", conn.getSchema(), rs.getString(3));
					tableCount++;
				}
			}
		}

		return tableCount;
	}

	private void initializeDatabaseSchema() throws LiquibaseException, SQLException {
		for (String script : scripts) {
			log.warn("Will execute {}", script);
			SpringLiquibase liquibase = new SpringLiquibase();
			liquibase.setResourceLoader(resourceLoader);
			liquibase.setBeanName(script);
			liquibase.setChangeLog(script);
			liquibase.setDataSource(dataSource);
			liquibase.afterPropertiesSet();
		}

		try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) {
			log.warn("Cleaning DATABASECHANGELOG");
			stmt.execute("delete from DATABASECHANGELOG");
			log.warn("DATABASECHANGELOG cleared.");
		}
	}

}