import java.nio.file.*;
import java.sql.*;
import java.util.*;
/**
* One-off backfill for the development database: applies V144's create-table statements with
* `if not exists`, taken verbatim from the migration file so the dev schema cannot drift from it.
*
*
Needed only because a server started before {@code MerchantOnboardingSchemaInitializer} learned
* to create tables keeps running with the old code path; after a restart the initializer does this
* by itself, and on a Flyway-managed schema (the test database) Flyway has already done it.
*
*
Additive only: it never drops or alters anything, and it refuses to touch any table that
* already exists.
*
* usage: java -cp :. ApplyV144ToDevDatabase.java [jdbcUrl]
*/
public class ApplyV144ToDevDatabase {
public static void main(String[] args) throws Exception {
String url = args.length > 0
? args[0]
: "jdbc:mysql://127.0.0.1:8134/talent_card?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useSSL=false";
String migration = Path.of("../../talent-card-server/src/main/resources/db/migration/V144__existing_merchant_onboarding_progress.sql").toAbsolutePath().normalize().toString();
String[] statements = Files.readString(Path.of(migration))
.replaceAll("(?i)create table (?!if not exists)", "create table if not exists ")
.split(";\\s*\\n");
try (Connection connection = DriverManager.getConnection(url, "root", "123456")) {
for (String statement : statements) {
if (statement.isBlank()) continue;
try (Statement executor = connection.createStatement()) {
executor.execute(statement);
System.out.println("applied: " + statement.strip().split("\\n")[0]);
}
}
}
}
}