import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.List; import java.util.Properties; /** Executes only the reviewed schema-only business migrations against the FMS database. */ public final class RunBusinessMigrations { private static final List MIGRATIONS = List.of( "015_normalize_existing_business_tables.sql", "016_create_base_tables.sql", "017_create_contract_file_tables.sql", "018_create_business_tables.sql", "019_create_finance_tables.sql", "020_create_oa_tables.sql", "021_create_business_views.sql" ); public static void main(String[] args) throws Exception { Path apiRoot = Path.of("").toAbsolutePath().normalize(); Path configPath = apiRoot.resolve("config/dbconfigs/G3HD.properties"); Path migrationDirectory = apiRoot.resolve("config/migrations"); Properties properties = new Properties(); try (InputStream input = Files.newInputStream(configPath)) { properties.load(input); } try (Connection connection = DriverManager.getConnection( properties.getProperty("url"), properties.getProperty("username"), properties.getProperty("password") )) { if (!"FMS".equalsIgnoreCase(connection.getCatalog())) { throw new IllegalStateException("Refusing to migrate database: " + connection.getCatalog()); } for (String migration : MIGRATIONS) { String sql = Files.readString(migrationDirectory.resolve(migration), StandardCharsets.UTF_8); try (Statement statement = connection.createStatement()) { statement.execute(sql); } System.out.println("APPLIED|" + migration); } try (Statement statement = connection.createStatement(); ResultSet rows = statement.executeQuery( "SELECT (SELECT COUNT(*) FROM sys.tables WHERE is_ms_shipped = 0), " + "(SELECT COUNT(*) FROM sys.views WHERE is_ms_shipped = 0)" )) { rows.next(); System.out.println("CATALOG|tables=" + rows.getInt(1) + "|views=" + rows.getInt(2)); } } } }