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:
2026-07-29 16:26:04 +02:00
parent a3fd32ec91
commit ab9d0bfe0f
3 changed files with 68 additions and 5 deletions
+10 -3
View File
@@ -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")
}
+54
View File
@@ -151,3 +151,57 @@ func TestMySQLDDL_NoLeftoverSQLiteisms(t *testing.T) {
}
}
}
// A QUOTED column declaration must be translated like a bare one.
//
// The SQLite baseline dump quotes its identifiers, and mysqlDDL turns those
// double quotes into backticks before the TEXT rules run. The pattern only
// matched a bare name, so every column in the baseline stayed TEXT — and MySQL
// then refused the schema in two different ways, neither naming the real 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
//
// The operator saw only "MySQL is enabled but the connection failed at startup",
// and fell back to the local SQLite database.
func TestMySQLDDL_QuotedTextColumns(t *testing.T) {
cases := []struct {
name, in, wantHas string
}{
{
// An indexed column must become VARCHAR, quoted or not.
name: "backticked indexed column",
in: "CREATE TABLE qso (`state` TEXT);",
wantHas: "`state` VARCHAR(255)",
},
{
name: "bare indexed column still works",
in: "ALTER TABLE qso ADD COLUMN state TEXT;",
wantHas: "state VARCHAR(255)",
},
{
// A default-bearing column must become VARCHAR, or MySQL rejects it.
name: "double-quoted column with a default",
in: `CREATE TABLE station_profiles ("op_name" TEXT NOT NULL DEFAULT '');`,
wantHas: "`op_name` VARCHAR(255)",
},
{
// And a plain unindexed column stays TEXT: VARCHAR everywhere would
// blow past MySQL's row-size limit, which is why the rule exists.
name: "unindexed column stays TEXT",
in: "CREATE TABLE qso (`comment` TEXT);",
wantHas: "`comment` TEXT",
},
}
for _, c := range cases {
got := mysqlDDL(c.in)
if !strings.Contains(got, c.wantHas) {
t.Errorf("%s:\n in %s\n got %s\n want it to contain %q", c.name, c.in, got, c.wantHas)
}
// The quoting must survive: a lost backtick is a syntax error further on.
if strings.Count(got, "`")%2 != 0 {
t.Errorf("%s: unbalanced backticks in %s", c.name, got)
}
}
}