mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
fix(db): replay upstream changelog history and add westernization cleanup changeset
Register the upstream Liquibase changesets (202606231030..202609172100) that were dropped during the rebase, and append a downstream cleanup changeset (202610111200) that westernizes EdgeTTS field remarks, purges the retired MinimaxStreamTTS rows and updates the HA / web-search plugin descriptors. Extend the H2 replay harness to cover what the upstream SQL now needs: - MySQL JSON_SET/REPLACE semantics incl. array-index paths ($[0].label) - double-quoted string literals and multi-clause ALTER TABLE splitting - assertions for the new cleanup changeset results
This commit is contained in:
parent
feb6edb47f
commit
0ccaf3a6b1
4 changed files with 608 additions and 67 deletions
|
|
@ -0,0 +1,50 @@
|
|||
-- Westernize upstream seeds reintroduced by the re-registered upstream
|
||||
-- queue (202606231030..202609172100). Append-only: follow-up cleanup for
|
||||
-- Chinese-facing rows added by upstream changesets, per downstream policy.
|
||||
|
||||
-- EdgeTTS provider fields and remark were overwritten with Chinese labels.
|
||||
UPDATE `ai_model_provider`
|
||||
SET `fields` = '[{"key":"voice","label":"Voice","type":"string"},{"key":"output_dir","label":"Output directory","type":"string"},{"key":"rate","label":"Rate (-100~100)","type":"number"},{"key":"volume","label":"Volume (0~100)","type":"number"},{"key":"pitch","label":"Pitch (-100~100)","type":"number"}]'
|
||||
WHERE `id` = 'SYSTEM_TTS_edge';
|
||||
|
||||
UPDATE `ai_model_config`
|
||||
SET `remark` = 'EdgeTTS configuration:
|
||||
1. Uses the Microsoft Edge TTS service
|
||||
2. Supports many languages and voices
|
||||
3. Free to use, no registration required
|
||||
4. Requires network access
|
||||
5. Output files are saved in the tmp/ directory
|
||||
6. Rate: -100~100, 0 is normal speed
|
||||
7. Volume: 0~100, 50 is normal volume
|
||||
8. Pitch: -100~100, 0 is normal pitch'
|
||||
WHERE `id` = 'TTS_EdgeTTS';
|
||||
|
||||
-- Minimax streaming TTS voices (Chinese provider) seeded for a provider that
|
||||
-- does not exist downstream.
|
||||
DELETE FROM `ai_tts_voice` WHERE `tts_model_id` = 'TTS_MinimaxStreamTTS';
|
||||
DELETE FROM `ai_model_config` WHERE `id` = 'TTS_MinimaxStreamTTS';
|
||||
DELETE FROM `ai_model_provider` WHERE `id` = 'SYSTEM_TTS_MinimaxStreamTTS';
|
||||
|
||||
-- Hass state plugin (upstream unified get/set into one plugin): English
|
||||
-- display name and field labels.
|
||||
UPDATE `ai_model_provider`
|
||||
SET `name` = 'HomeAssistant Device Control',
|
||||
`fields` = JSON_SET(
|
||||
`fields`,
|
||||
'$[0].label', 'HA server URL',
|
||||
'$[1].label', 'HA API access token',
|
||||
'$[2].label', 'Device list (name,entity_id;...)'
|
||||
)
|
||||
WHERE `id` = 'SYSTEM_PLUGIN_HA_STATE';
|
||||
|
||||
-- Web search: upstream re-set the provider label in Chinese and the seed
|
||||
-- still defaults to the China-only metaso provider. Tavily is the western
|
||||
-- default; metaso remains selectable for users who want it.
|
||||
UPDATE `ai_model_provider`
|
||||
SET `fields` = JSON_SET(
|
||||
`fields`,
|
||||
'$[0].label', 'Search provider: tavily / serply / metaso',
|
||||
'$[0].default', 'tavily',
|
||||
'$[3].default', 'tvly-XXXX'
|
||||
)
|
||||
WHERE `id` = 'SYSTEM_PLUGIN_WEB_SEARCH';
|
||||
|
|
@ -753,3 +753,87 @@ databaseChangeLog:
|
|||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202606131500.sql
|
||||
- changeSet:
|
||||
id: 202606231030
|
||||
author: RanChen
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202606231030.sql
|
||||
- changeSet:
|
||||
id: 202606261131
|
||||
author: RanChen
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202606261131.sql
|
||||
- changeSet:
|
||||
id: 202607011405
|
||||
author: LauraGPT
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202607011405.sql
|
||||
- changeSet:
|
||||
id: 202607071530
|
||||
author: tykechen
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202607071530.sql
|
||||
- changeSet:
|
||||
id: 202607101200
|
||||
author: tykechen
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202607101200.sql
|
||||
- changeSet:
|
||||
id: 202607290930
|
||||
author: cgd
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202607290930.sql
|
||||
- changeSet:
|
||||
id: 202608061000
|
||||
author: cgd
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202608061030.sql
|
||||
- changeSet:
|
||||
id: 202608061800
|
||||
author: cgd
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202608061800.sql
|
||||
- changeSet:
|
||||
id: 202608131030
|
||||
author: tykechen
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202608131030.sql
|
||||
- changeSet:
|
||||
id: 202609021000
|
||||
author: JacobNg1
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202609021000.sql
|
||||
- changeSet:
|
||||
id: 202609172100
|
||||
author: googio
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202609172100.sql
|
||||
- changeSet:
|
||||
id: 202610111200
|
||||
author: lapy
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202610111200.sql
|
||||
|
|
|
|||
|
|
@ -6,7 +6,14 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MySQL-compatible JSON functions for the H2 in-memory changelog replay.
|
||||
* Supports object and array navigation: {@code $.a.b}, {@code $[0].label},
|
||||
* {@code $.a[2].default}, and unwraps double-encoded JSON string payloads.
|
||||
*/
|
||||
public final class H2JsonFunctions {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
|
@ -14,16 +21,16 @@ public final class H2JsonFunctions {
|
|||
private H2JsonFunctions() {
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ set
|
||||
|
||||
public static String jsonSet(String json, String path1, String value1) throws IOException {
|
||||
ObjectNode root = asObjectNode(json);
|
||||
setJsonPath(root, path1, value1);
|
||||
JsonNode root = setPath(json, path1, value1);
|
||||
return OBJECT_MAPPER.writeValueAsString(root);
|
||||
}
|
||||
|
||||
public static String jsonSet(String json, String path1, String value1, String path2, String value2) throws IOException {
|
||||
ObjectNode root = asObjectNode(json);
|
||||
setJsonPath(root, path1, value1);
|
||||
setJsonPath(root, path2, value2);
|
||||
JsonNode root = setPath(json, path1, value1);
|
||||
root = setPath(root, path2, value2);
|
||||
return OBJECT_MAPPER.writeValueAsString(root);
|
||||
}
|
||||
|
||||
|
|
@ -36,25 +43,38 @@ public final class H2JsonFunctions {
|
|||
String path3,
|
||||
String value3
|
||||
) throws IOException {
|
||||
ObjectNode root = asObjectNode(json);
|
||||
setJsonPath(root, path1, value1);
|
||||
setJsonPath(root, path2, value2);
|
||||
setJsonPath(root, path3, value3);
|
||||
JsonNode root = setPath(json, path1, value1);
|
||||
root = setPath(root, path2, value2);
|
||||
root = setPath(root, path3, value3);
|
||||
return OBJECT_MAPPER.writeValueAsString(root);
|
||||
}
|
||||
|
||||
public static String jsonSet(
|
||||
String json,
|
||||
String path1,
|
||||
String value1,
|
||||
String path2,
|
||||
String value2,
|
||||
String path3,
|
||||
String value3,
|
||||
String path4,
|
||||
String value4
|
||||
) throws IOException {
|
||||
JsonNode root = setPath(json, path1, value1);
|
||||
root = setPath(root, path2, value2);
|
||||
root = setPath(root, path3, value3);
|
||||
root = setPath(root, path4, value4);
|
||||
return OBJECT_MAPPER.writeValueAsString(root);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------- remove
|
||||
|
||||
public static String jsonRemove(String json, String path) throws IOException {
|
||||
ObjectNode root = asObjectNode(json);
|
||||
String[] tokens = tokenizePath(path);
|
||||
ObjectNode current = root;
|
||||
for (int i = 0; i < tokens.length - 1; i++) {
|
||||
JsonNode next = current.get(tokens[i]);
|
||||
if (!(next instanceof ObjectNode)) {
|
||||
return OBJECT_MAPPER.writeValueAsString(root);
|
||||
}
|
||||
current = (ObjectNode) next;
|
||||
if (json == null || json.isBlank()) {
|
||||
return json;
|
||||
}
|
||||
current.remove(tokens[tokens.length - 1]);
|
||||
JsonNode root = parseRoot(json);
|
||||
removePath(root, path);
|
||||
return OBJECT_MAPPER.writeValueAsString(root);
|
||||
}
|
||||
|
||||
|
|
@ -66,31 +86,30 @@ public final class H2JsonFunctions {
|
|||
return jsonRemove(jsonRemove(json, path1, path2), path3);
|
||||
}
|
||||
|
||||
public static String jsonRemove(String json, String path1, String path2, String path3, String path4) throws IOException {
|
||||
public static String jsonRemove(
|
||||
String json, String path1, String path2, String path3, String path4
|
||||
) throws IOException {
|
||||
return jsonRemove(jsonRemove(json, path1, path2, path3), path4);
|
||||
}
|
||||
|
||||
public static String jsonRemove(
|
||||
String json,
|
||||
String path1,
|
||||
String path2,
|
||||
String path3,
|
||||
String path4,
|
||||
String path5
|
||||
String json, String path1, String path2, String path3, String path4, String path5
|
||||
) throws IOException {
|
||||
return jsonRemove(jsonRemove(json, path1, path2, path3, path4), path5);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------- extract
|
||||
|
||||
public static String jsonExtract(String json, String path) throws IOException {
|
||||
if (json == null || json.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
JsonNode current = OBJECT_MAPPER.readTree(json);
|
||||
JsonNode current = parseRoot(json);
|
||||
for (String token : tokenizePath(path)) {
|
||||
if (current == null) {
|
||||
return null;
|
||||
}
|
||||
current = current.get(token);
|
||||
current = step(current, token);
|
||||
}
|
||||
if (current == null || current.isNull()) {
|
||||
return null;
|
||||
|
|
@ -118,7 +137,7 @@ public final class H2JsonFunctions {
|
|||
if (json == null || json.isBlank()) {
|
||||
return json;
|
||||
}
|
||||
JsonNode node = OBJECT_MAPPER.readTree(json);
|
||||
JsonNode node = parseRoot(json);
|
||||
if (!(node instanceof ArrayNode arrayNode)) {
|
||||
return json;
|
||||
}
|
||||
|
|
@ -137,33 +156,166 @@ public final class H2JsonFunctions {
|
|||
return OBJECT_MAPPER.writeValueAsString(filtered);
|
||||
}
|
||||
|
||||
private static ObjectNode asObjectNode(String json) throws IOException {
|
||||
if (json == null || json.isBlank()) {
|
||||
return OBJECT_MAPPER.createObjectNode();
|
||||
}
|
||||
JsonNode node = OBJECT_MAPPER.readTree(json);
|
||||
if (node instanceof ObjectNode objectNode) {
|
||||
return objectNode;
|
||||
}
|
||||
return OBJECT_MAPPER.createObjectNode();
|
||||
}
|
||||
// ------------------------------------------------------------- plumbing
|
||||
|
||||
private static void setJsonPath(ObjectNode root, String path, String value) throws IOException {
|
||||
String[] tokens = tokenizePath(path);
|
||||
ObjectNode current = root;
|
||||
for (int i = 0; i < tokens.length - 1; i++) {
|
||||
JsonNode next = current.get(tokens[i]);
|
||||
if (!(next instanceof ObjectNode)) {
|
||||
next = OBJECT_MAPPER.createObjectNode();
|
||||
current.set(tokens[i], next);
|
||||
private static JsonNode setPath(Object jsonValue, String path, String value) throws IOException {
|
||||
JsonNode root;
|
||||
if (jsonValue instanceof JsonNode node) {
|
||||
root = node;
|
||||
} else {
|
||||
String json = jsonValue == null ? null : jsonValue.toString();
|
||||
root = (json == null || json.isBlank())
|
||||
? OBJECT_MAPPER.createObjectNode()
|
||||
: parseRoot(json);
|
||||
}
|
||||
List<String> tokens = tokenizePath(path);
|
||||
if (tokens.isEmpty()) {
|
||||
return root;
|
||||
}
|
||||
if (!(root instanceof ObjectNode) && !(root instanceof ArrayNode)) {
|
||||
root = OBJECT_MAPPER.createObjectNode();
|
||||
}
|
||||
JsonNode current = root;
|
||||
for (int i = 0; i < tokens.size() - 1; i++) {
|
||||
String token = tokens.get(i);
|
||||
JsonNode next = step(current, token);
|
||||
if (next == null || next.isNull()) {
|
||||
JsonNode created = isIndexToken(tokens.get(i + 1))
|
||||
? OBJECT_MAPPER.createArrayNode()
|
||||
: OBJECT_MAPPER.createObjectNode();
|
||||
attach(current, token, created);
|
||||
next = created;
|
||||
}
|
||||
current = (ObjectNode) next;
|
||||
current = next;
|
||||
}
|
||||
current.set(tokens[tokens.length - 1], coerceJsonValue(value));
|
||||
attach(current, tokens.get(tokens.size() - 1), coerceJsonValue(value));
|
||||
return root;
|
||||
}
|
||||
|
||||
private static String[] tokenizePath(String path) {
|
||||
return path.replaceFirst("^\\$\\.", "").split("\\.");
|
||||
private static void removePath(JsonNode root, String path) {
|
||||
List<String> tokens = tokenizePath(path);
|
||||
if (tokens.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
JsonNode current = root;
|
||||
for (int i = 0; i < tokens.size() - 1; i++) {
|
||||
current = step(current, tokens.get(i));
|
||||
if (current == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
String last = tokens.get(tokens.size() - 1);
|
||||
if (current instanceof ObjectNode objectNode) {
|
||||
objectNode.remove(last);
|
||||
} else if (current instanceof ArrayNode arrayNode && isIndexToken(last)) {
|
||||
int index = Integer.parseInt(last);
|
||||
if (index >= 0 && index < arrayNode.size()) {
|
||||
arrayNode.remove(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse a JSON document, unwrapping double-encoded string payloads. */
|
||||
private static JsonNode parseRoot(String json) throws IOException {
|
||||
JsonNode node = OBJECT_MAPPER.readTree(json);
|
||||
// A VARCHAR column may hold a quoted JSON string ("[{...}]"); unwrap once.
|
||||
if (node.isTextual()) {
|
||||
String inner = node.textValue().trim();
|
||||
if (inner.startsWith("{") || inner.startsWith("[")) {
|
||||
try {
|
||||
return OBJECT_MAPPER.readTree(inner);
|
||||
} catch (Exception ignored) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static JsonNode step(JsonNode current, String token) {
|
||||
if (current == null || current.isNull() || token == null) {
|
||||
return null;
|
||||
}
|
||||
if (current instanceof ArrayNode arrayNode) {
|
||||
if (isIndexToken(token)) {
|
||||
int index = Integer.parseInt(token);
|
||||
return index >= 0 && index < arrayNode.size() ? arrayNode.get(index) : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (current.isTextual()) {
|
||||
// transparently descend into a double-encoded JSON string member
|
||||
try {
|
||||
JsonNode inner = parseRoot(current.textValue());
|
||||
if (inner != current) {
|
||||
return step(inner, token);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return current.get(token);
|
||||
}
|
||||
|
||||
private static void attach(JsonNode container, String token, JsonNode value) {
|
||||
if (container instanceof ObjectNode objectNode) {
|
||||
objectNode.set(token, value);
|
||||
} else if (container instanceof ArrayNode arrayNode && isIndexToken(token)) {
|
||||
int index = Integer.parseInt(token);
|
||||
while (arrayNode.size() <= index) {
|
||||
arrayNode.addNull();
|
||||
}
|
||||
if (index >= 0) {
|
||||
arrayNode.set(index, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isIndexToken(String token) {
|
||||
return token != null && !token.isEmpty() && token.chars().allMatch(Character::isDigit);
|
||||
}
|
||||
|
||||
/** Split {@code $.a[0].b} into [a, 0, b]; {@code $[3].default} into [3, default]. */
|
||||
private static List<String> tokenizePath(String path) {
|
||||
List<String> tokens = new ArrayList<>();
|
||||
if (path == null || path.isEmpty()) {
|
||||
return tokens;
|
||||
}
|
||||
String p = path;
|
||||
if (p.startsWith("$")) {
|
||||
p = p.substring(1);
|
||||
}
|
||||
StringBuilder current = new StringBuilder();
|
||||
for (int i = 0; i < p.length(); i++) {
|
||||
char c = p.charAt(i);
|
||||
if (c == '.') {
|
||||
if (current.length() > 0) {
|
||||
tokens.add(current.toString());
|
||||
current.setLength(0);
|
||||
}
|
||||
} else if (c == '[') {
|
||||
if (current.length() > 0) {
|
||||
tokens.add(current.toString());
|
||||
current.setLength(0);
|
||||
}
|
||||
int end = p.indexOf(']', i);
|
||||
if (end < 0) {
|
||||
end = p.length();
|
||||
}
|
||||
String inside = p.substring(i + 1, end).replace("\"", "").trim();
|
||||
if (!inside.isEmpty()) {
|
||||
tokens.add(inside);
|
||||
}
|
||||
i = end;
|
||||
} else {
|
||||
current.append(c);
|
||||
}
|
||||
}
|
||||
if (current.length() > 0) {
|
||||
tokens.add(current.toString());
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private static JsonNode coerceJsonValue(String value) throws IOException {
|
||||
|
|
|
|||
|
|
@ -280,25 +280,280 @@ WHERE r.sort = 0;
|
|||
}
|
||||
normalized = splitMultiAddColumnAlterStatements(normalized);
|
||||
normalized = normalizeJsonSetCalls(normalized);
|
||||
normalized = convertDoubleQuotedStringLiterals(normalized);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private String splitMultiAddColumnAlterStatements(String sql) {
|
||||
Pattern pattern = Pattern.compile(
|
||||
"(?is)ALTER\\s+TABLE\\s+(`?\\w+`?)\\s+(ADD\\s+COLUMN\\s+.*?),(\\s*ADD\\s+COLUMN\\s+.*?);"
|
||||
);
|
||||
|
||||
String normalized = sql;
|
||||
while (true) {
|
||||
Matcher matcher = pattern.matcher(normalized);
|
||||
if (!matcher.find()) {
|
||||
return normalized;
|
||||
/**
|
||||
* H2 (unlike MySQL) parses "..." as an identifier, not a string literal.
|
||||
* Walk the SQL with a small state machine so only true double-quoted string
|
||||
* literals are rewritten to single-quoted form; single-quoted payloads and
|
||||
* backtick identifiers are copied through untouched.
|
||||
*/
|
||||
private String convertDoubleQuotedStringLiterals(String sql) {
|
||||
StringBuilder out = new StringBuilder(sql.length());
|
||||
int i = 0;
|
||||
int n = sql.length();
|
||||
while (i < n) {
|
||||
char c = sql.charAt(i);
|
||||
if (c == '-' && i + 1 < n && sql.charAt(i + 1) == '-') {
|
||||
int end = sql.indexOf('\n', i);
|
||||
if (end < 0) {
|
||||
end = n;
|
||||
}
|
||||
out.append(sql, i, end);
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
String replacement =
|
||||
"ALTER TABLE " + matcher.group(1) + " " + matcher.group(2) + ";\n" +
|
||||
"ALTER TABLE " + matcher.group(1) + " " + matcher.group(3).trim() + ";";
|
||||
normalized = matcher.replaceFirst(Matcher.quoteReplacement(replacement));
|
||||
if (c == '/' && i + 1 < n && sql.charAt(i + 1) == '*') {
|
||||
int end = sql.indexOf("*/", i + 2);
|
||||
if (end < 0) {
|
||||
end = n;
|
||||
} else {
|
||||
end += 2;
|
||||
}
|
||||
out.append(sql, i, end);
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
if (c == '`') {
|
||||
int end = sql.indexOf('`', i + 1);
|
||||
if (end < 0) {
|
||||
end = n - 1;
|
||||
}
|
||||
out.append(sql, i, end + 1);
|
||||
i = end + 1;
|
||||
continue;
|
||||
}
|
||||
if (c == '\'') {
|
||||
int j = i + 1;
|
||||
StringBuilder body = new StringBuilder("'");
|
||||
while (j < n) {
|
||||
char d = sql.charAt(j);
|
||||
if (d == '\\' && j + 1 < n) {
|
||||
body.append(d).append(sql.charAt(j + 1));
|
||||
j += 2;
|
||||
continue;
|
||||
}
|
||||
if (d == '\'') {
|
||||
if (j + 1 < n && sql.charAt(j + 1) == '\'') {
|
||||
body.append("''");
|
||||
j += 2;
|
||||
continue;
|
||||
}
|
||||
body.append('\'');
|
||||
j++;
|
||||
break;
|
||||
}
|
||||
body.append(d);
|
||||
j++;
|
||||
}
|
||||
out.append(body);
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
if (c == '"') {
|
||||
StringBuilder content = new StringBuilder();
|
||||
int j = i + 1;
|
||||
boolean closed = false;
|
||||
while (j < n) {
|
||||
char d = sql.charAt(j);
|
||||
if (d == '\\' && j + 1 < n) {
|
||||
if (sql.charAt(j + 1) == '"') {
|
||||
content.append('"');
|
||||
} else {
|
||||
content.append(d).append(sql.charAt(j + 1));
|
||||
}
|
||||
j += 2;
|
||||
continue;
|
||||
}
|
||||
if (d == '"') {
|
||||
if (j + 1 < n && sql.charAt(j + 1) == '"') {
|
||||
content.append('"');
|
||||
j += 2;
|
||||
continue;
|
||||
}
|
||||
closed = true;
|
||||
j++;
|
||||
break;
|
||||
}
|
||||
content.append(d);
|
||||
j++;
|
||||
}
|
||||
if (!closed) {
|
||||
// unterminated; copy through unchanged
|
||||
out.append(sql, i, j);
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
out.append('\'').append(content.toString().replace("'", "''")).append('\'');
|
||||
i = j;
|
||||
continue;
|
||||
}
|
||||
out.append(c);
|
||||
i++;
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* H2 does not support multiple clauses in one ALTER TABLE, so split any
|
||||
* top-level comma inside ALTER TABLE ... ; into separate statements.
|
||||
* Commas inside parentheses or quotes are ignored.
|
||||
*/
|
||||
private String splitMultiAddColumnAlterStatements(String sql) {
|
||||
StringBuilder out = new StringBuilder(sql.length());
|
||||
int i = 0;
|
||||
int n = sql.length();
|
||||
while (i < n) {
|
||||
// Never touch line comments (e.g. "-- rollback ALTER TABLE ..."):
|
||||
// splitting them would turn commented SQL into live statements.
|
||||
int lineEnd = sql.indexOf('\n', i);
|
||||
if (lineEnd < 0) {
|
||||
lineEnd = n;
|
||||
}
|
||||
String line = sql.substring(i, Math.min(lineEnd, n));
|
||||
if (line.isBlank() || line.stripLeading().startsWith("--")) {
|
||||
out.append(sql, i, Math.min(lineEnd + 1, n));
|
||||
i = Math.min(lineEnd + 1, n);
|
||||
continue;
|
||||
}
|
||||
int alterAt = sql.regionMatches(true, i, "ALTER TABLE", 0, 11) ? i : findAlter(sql, i);
|
||||
if (alterAt < 0) {
|
||||
out.append(sql, i, n);
|
||||
break;
|
||||
}
|
||||
if (alterAt > i && sql.substring(i, alterAt).contains("\n--")) {
|
||||
// an ALTER found on a line after a comment line: defer to line loop
|
||||
int nextBreak = sql.indexOf('\n', i) + 1;
|
||||
out.append(sql, i, nextBreak);
|
||||
i = nextBreak;
|
||||
continue;
|
||||
}
|
||||
// locate the terminating semicolon of this statement (quote-aware)
|
||||
int end = statementEnd(sql, alterAt);
|
||||
String stmt = sql.substring(alterAt, end + 1);
|
||||
String split = splitAlterStatement(stmt);
|
||||
out.append(sql, i, alterAt).append(split);
|
||||
i = end + 1;
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private int findAlter(String sql, int from) {
|
||||
boolean lineStart = from == 0 || sql.charAt(from - 1) == '\n';
|
||||
for (int j = from; j + 11 <= sql.length(); j++) {
|
||||
char c = sql.charAt(j);
|
||||
if (c == '\n') {
|
||||
lineStart = true;
|
||||
continue;
|
||||
}
|
||||
if (lineStart && !Character.isWhitespace(c) && sql.regionMatches(j, "--", 0, 2)) {
|
||||
int eol = sql.indexOf('\n', j);
|
||||
j = (eol < 0) ? sql.length() : eol - 1; // skip whole comment line
|
||||
continue;
|
||||
}
|
||||
if (lineStart && !Character.isWhitespace(c)) {
|
||||
lineStart = false;
|
||||
}
|
||||
if (sql.regionMatches(true, j, "ALTER TABLE", 0, 11)
|
||||
&& (j == 0 || sql.charAt(j - 1) == '\n' || Character.isWhitespace(sql.charAt(j - 1)))) {
|
||||
return j;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int statementEnd(String sql, int from) {
|
||||
boolean inSingle = false;
|
||||
boolean inDouble = false;
|
||||
boolean inBacktick = false;
|
||||
for (int j = from; j < sql.length(); j++) {
|
||||
char c = sql.charAt(j);
|
||||
if (c == '\\' && (inSingle || inDouble)) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (c == '\'' && !inDouble && !inBacktick) {
|
||||
inSingle = !inSingle;
|
||||
} else if (c == '"' && !inSingle && !inBacktick) {
|
||||
inDouble = !inDouble;
|
||||
} else if (c == '`' && !inSingle && !inDouble) {
|
||||
inBacktick = !inBacktick;
|
||||
} else if (c == ';' && !inSingle && !inDouble && !inBacktick) {
|
||||
return j;
|
||||
}
|
||||
}
|
||||
return sql.length() - 1;
|
||||
}
|
||||
|
||||
private String splitAlterStatement(String stmt) {
|
||||
// strip trailing semicolon
|
||||
String body = stmt.trim();
|
||||
if (body.endsWith(";")) {
|
||||
body = body.substring(0, body.length() - 1);
|
||||
}
|
||||
List<String> parts = new ArrayList<>();
|
||||
int depth = 0;
|
||||
boolean inSingle = false;
|
||||
boolean inDouble = false;
|
||||
boolean inBacktick = false;
|
||||
StringBuilder current = new StringBuilder();
|
||||
for (int j = 0; j < body.length(); j++) {
|
||||
char c = body.charAt(j);
|
||||
if (c == '\\' && (inSingle || inDouble)) {
|
||||
current.append(c);
|
||||
if (j + 1 < body.length()) {
|
||||
current.append(body.charAt(++j));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c == '\'' && !inDouble && !inBacktick) {
|
||||
inSingle = !inSingle;
|
||||
} else if (c == '"' && !inSingle && !inBacktick) {
|
||||
inDouble = !inDouble;
|
||||
} else if (c == '`' && !inSingle && !inDouble) {
|
||||
inBacktick = !inBacktick;
|
||||
} else if (!inSingle && !inDouble && !inBacktick) {
|
||||
if (c == '(') {
|
||||
depth++;
|
||||
} else if (c == ')') {
|
||||
depth--;
|
||||
} else if (c == ',' && depth == 0) {
|
||||
parts.add(current.toString());
|
||||
current.setLength(0);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
current.append(c);
|
||||
}
|
||||
parts.add(current.toString());
|
||||
if (parts.size() <= 1) {
|
||||
return stmt;
|
||||
}
|
||||
Matcher header = Pattern.compile("(?is)^(ALTER\\s+TABLE\\s+`?\\w+`?)\\s*(.*)$")
|
||||
.matcher(parts.get(0).trim());
|
||||
if (!header.matches()) {
|
||||
return stmt;
|
||||
}
|
||||
String table = header.group(1);
|
||||
StringBuilder rebuilt = new StringBuilder();
|
||||
List<String> clauses = new ArrayList<>();
|
||||
if (!header.group(2).isBlank()) {
|
||||
clauses.add(header.group(2).trim());
|
||||
}
|
||||
for (int p = 1; p < parts.size(); p++) {
|
||||
if (!parts.get(p).trim().isEmpty()) {
|
||||
clauses.add(parts.get(p).trim());
|
||||
}
|
||||
}
|
||||
if (clauses.size() <= 1) {
|
||||
return stmt;
|
||||
}
|
||||
for (String clause : clauses) {
|
||||
rebuilt.append(table).append(' ').append(clause).append(";\n");
|
||||
}
|
||||
return rebuilt.toString();
|
||||
}
|
||||
|
||||
private String normalizeJsonSetCalls(String sql) {
|
||||
|
|
@ -526,7 +781,7 @@ WHERE r.sort = 0;
|
|||
|
||||
private void assertLatestChangeSetApplied(Connection connection) throws SQLException {
|
||||
String latestId = scalar(connection, "SELECT MAX(ID) FROM DATABASECHANGELOG");
|
||||
Assertions.assertEquals("202606131500", latestId, "latest downstream cleanup migration should be applied");
|
||||
Assertions.assertEquals("202610111200", latestId, "latest downstream cleanup migration should be applied");
|
||||
}
|
||||
|
||||
private void assertRequiredSeedCoverage(Connection connection) throws SQLException {
|
||||
|
|
@ -553,7 +808,7 @@ WHERE r.sort = 0;
|
|||
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_PLUGIN_WEATHER' AND name = 'Weather Service'");
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_PLUGIN_NEWS' AND name = 'News Service'");
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_PLUGIN_HA_GET_STATE' AND name = 'HomeAssistant Device Status Query'");
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_PLUGIN_HA_STATE' AND name = 'HomeAssistant Device Control'");
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_PLUGIN_HA_GET_CALENDAR' AND name = 'Standard Calendar Information Service'");
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_PLUGIN_CHANGE_ROLE' AND name = 'Role Switching Service'");
|
||||
assertExists(connection, "SELECT 1 FROM ai_model_provider WHERE id = 'SYSTEM_ASR_GeminiASR' AND name = 'Gemini Speech Recognition'");
|
||||
|
|
|
|||
Loading…
Reference in a new issue