package marketing.heyday.hartmann.fotodocumentation.core.db.migration;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.ejb.Singleton;
import jakarta.ejb.Startup;
import jakarta.ejb.TransactionManagement;
import jakarta.ejb.TransactionManagementType;
import javax.sql.DataSource;

import org.flywaydb.core.Flyway;

/**
 * 
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 19 Jan 2026
 */

@Singleton
@Startup
@TransactionManagement(TransactionManagementType.BEAN)
public class DbMigrator {

	@Resource(lookup = "java:/jdbc/fotoDocumentationDS")
	private DataSource dataSource;

	@PostConstruct
	public void onStartup() {
		Flyway flyway = getFlyway();
		flyway.setDataSource(dataSource);
		flyway.setTable("DB_MIGRATION");
		flyway.setLocations(this.getClass().getPackage().getName());
		flyway.setBaselineOnMigrate(false);
		flyway.setValidateOnMigrate(false);
		flyway.setIgnoreFailedFutureMigration(true);
		
		flyway.migrate();
	}

	protected Flyway getFlyway() {
		return new Flyway();
	}
}
