fix: MySQL schema build failed on quoted column declarations
Two errors from a shared MySQL logbook, both fatal to the schema and neither
naming its cause:
CREATE INDEX … ON qso(state) → 1170 BLOB/TEXT column used in key
specification without a key length
op_name TEXT … DEFAULT '' → 1101 TEXT column can't have a default value
One root cause. TEXT columns are rewritten to VARCHAR(255) when they are indexed
or carry a default — that is exactly what those two MySQL rules require — but the
pattern only matched a BARE column name. The SQLite baseline dump quotes its
identifiers, and mysqlDDL turns those quotes into backticks before the TEXT rules
run, so every column coming from the baseline kept its TEXT type and MySQL
rejected the schema.
The operator saw none of this: just "MySQL is enabled but the connection failed
at startup" and a silent fallback to local SQLite — which is the worst shape for
a shared logbook, because each operator then logs into their own copy.
The pattern now accepts a quoted or bare name and puts the quoting back. A test
covers both forms, plus the case that must NOT change: an unindexed column stays
TEXT, since VARCHAR everywhere would break MySQL's row-size limit. Reverting the
one-line change makes it fail.
This commit is contained in:
+10
-3
@@ -35,7 +35,13 @@ import (
|
||||
var (
|
||||
reStrftimeDefault = regexp.MustCompile(`\(strftime\('%Y-%m-%dT%H:%M:%fZ',\s*'now'\)\)`)
|
||||
reBareInteger = regexp.MustCompile(`\bINTEGER\b`)
|
||||
reColText = regexp.MustCompile(`(\w+)\s+TEXT\b`)
|
||||
// The column name may be bare or quoted. The SQLite baseline dump quotes its
|
||||
// identifiers, and those double quotes are already BACKTICKS by the time this
|
||||
// runs — so a pattern matching only a bare name left every column in the
|
||||
// baseline as TEXT. That produced both "BLOB/TEXT column 'state' used in key
|
||||
// specification without a key length" and "column 'op_name' can't have a
|
||||
// default value" on a MySQL logbook, neither of which names the real cause.
|
||||
reColText = regexp.MustCompile("(`?)(\\w+)(`?)\\s+TEXT\\b")
|
||||
reKeyColumn = regexp.MustCompile(`(?m)^(\s*)key\b`)
|
||||
// SQLite quotes identifiers with double quotes — e.g. a table renamed by
|
||||
// ALTER … RENAME TO ends up as CREATE TABLE "name" in sqlite_master. MySQL
|
||||
@@ -109,7 +115,8 @@ func translateTextColumns(s string) string {
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
col := line[m[2]:m[3]]
|
||||
// Groups: 1 = opening backtick (or empty), 2 = the name, 3 = closing one.
|
||||
openQ, col, closeQ := line[m[2]:m[3]], line[m[4]:m[5]], line[m[6]:m[7]]
|
||||
var typ string
|
||||
switch {
|
||||
case longTextColumns[col] != "":
|
||||
@@ -121,7 +128,7 @@ func translateTextColumns(s string) string {
|
||||
default:
|
||||
typ = "TEXT"
|
||||
}
|
||||
lines[i] = line[:m[0]] + col + " " + typ + line[m[1]:]
|
||||
lines[i] = line[:m[0]] + openQ + col + closeQ + " " + typ + line[m[1]:]
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user