generated from CubeCraft-Creations/Tracehound
Merge branch 'dev' into agent/dex/CUB-239-hub-dedup-replay
This commit is contained in:
+35
-18
@@ -8,6 +8,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
@@ -95,12 +96,11 @@ func Open(path string) (*DB, error) {
|
||||
return &DB{db}, nil
|
||||
}
|
||||
|
||||
// migrate executes a SQL migration string.
|
||||
// migrate executes a SQL migration string by splitting on semicolons.
|
||||
func migrate(db *sql.DB, sql string) error {
|
||||
// Split on semicolons to handle multiple statements
|
||||
statements := splitSQL(sql)
|
||||
for _, stmt := range statements {
|
||||
stmt = stripWhitespace(stmt)
|
||||
stmt = strings.TrimSpace(stmt)
|
||||
if stmt == "" {
|
||||
continue
|
||||
}
|
||||
@@ -111,8 +111,13 @@ func migrate(db *sql.DB, sql string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// splitSQL splits a SQL string on semicolons, respecting quoted strings.
|
||||
// splitSQL splits a SQL string on semicolons, respecting quoted strings
|
||||
// and stripping SQL line comments (--).
|
||||
func splitSQL(sql string) []string {
|
||||
// First, strip all line comments (--) to prevent them from swallowing
|
||||
// subsequent SQL statements when newlines are collapsed.
|
||||
sql = stripSQLLineComments(sql)
|
||||
|
||||
var stmts []string
|
||||
var current string
|
||||
inQuote := false
|
||||
@@ -134,30 +139,42 @@ func splitSQL(sql string) []string {
|
||||
case ';':
|
||||
stmts = append(stmts, current)
|
||||
current = ""
|
||||
case '\r', '\n', '\t':
|
||||
current += " "
|
||||
default:
|
||||
current += string(r)
|
||||
}
|
||||
}
|
||||
if len(current) > 0 {
|
||||
if strings.TrimSpace(current) != "" {
|
||||
stmts = append(stmts, current)
|
||||
}
|
||||
return stmts
|
||||
}
|
||||
|
||||
// stripWhitespace removes leading/trailing whitespace and normalizes newlines.
|
||||
func stripWhitespace(s string) string {
|
||||
result := ""
|
||||
runningSpace := false
|
||||
for _, r := range s {
|
||||
if r == ' ' || r == '\t' || r == '\n' || r == '\r' {
|
||||
if !runningSpace {
|
||||
result += " "
|
||||
runningSpace = true
|
||||
// stripSQLLineComments removes all -- single-line comments from SQL text.
|
||||
func stripSQLLineComments(sql string) string {
|
||||
var result strings.Builder
|
||||
i := 0
|
||||
runes := []rune(sql)
|
||||
|
||||
for i < len(runes) {
|
||||
r := runes[i]
|
||||
|
||||
// Check for -- comment start
|
||||
if r == '-' && i+1 < len(runes) && runes[i+1] == '-' {
|
||||
// Skip to end of line
|
||||
i += 2
|
||||
for i < len(runes) && runes[i] != '\n' && runes[i] != '\r' {
|
||||
i++
|
||||
}
|
||||
} else {
|
||||
result += string(r)
|
||||
runningSpace = false
|
||||
// Replace comment with a newline (preserves statement boundaries)
|
||||
result.WriteRune('\n')
|
||||
continue
|
||||
}
|
||||
|
||||
result.WriteRune(r)
|
||||
i++
|
||||
}
|
||||
return result
|
||||
|
||||
return result.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user