1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.
 *
 * <p>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.
 *
 * <p>Additive only: it never drops or alters anything, and it refuses to touch any table that
 * already exists.
 *
 * usage: java -cp <mysql-connector-j.jar>:. 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]);
        }
      }
    }
  }
}