|
|
@@ -11,6 +11,7 @@ import org.springframework.util.StreamUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 数据库初始化:执行 schema.sql 建表,并初始化默认管理账号与种子数据。
|
|
|
@@ -44,10 +45,15 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
try {
|
|
|
ClassPathResource resource = new ClassPathResource("schema.sql");
|
|
|
String sql = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
|
|
|
- // 按分号拆分成多条语句执行(简化;实际生产建议用专门的迁移工具)
|
|
|
- for (String statement : sql.split(";")) {
|
|
|
+ // 先剔除整行注释(-- 开头),再按分号拆分。
|
|
|
+ // 否则注释与紧跟其后的 CREATE TABLE 会粘在同一段,
|
|
|
+ // 导致整段被 startsWith("--") 误判跳过,建表语句静默丢失。
|
|
|
+ String cleaned = sql.lines()
|
|
|
+ .filter(line -> !line.trim().startsWith("--"))
|
|
|
+ .collect(Collectors.joining("\n"));
|
|
|
+ for (String statement : cleaned.split(";")) {
|
|
|
String trimmed = statement.trim();
|
|
|
- if (trimmed.isEmpty() || trimmed.startsWith("--")) {
|
|
|
+ if (trimmed.isEmpty()) {
|
|
|
continue;
|
|
|
}
|
|
|
try {
|