diff --git a/core/database/check.go b/core/database/check.go index 517c41d0..21cb5464 100644 --- a/core/database/check.go +++ b/core/database/check.go @@ -22,11 +22,6 @@ import ( "github.com/documize/community/server/web" ) -// sql variantsa -const sqlVariantMySQL string = "MySQL" -const sqlVariantPercona string = "Percona" -const sqlVariantMariaDB string = "MariaDB" - var dbCheckOK bool // default false // Check that the database is configured correctly and that all the required tables exist. @@ -66,8 +61,8 @@ func Check(runtime *env.Runtime) bool { // Get SQL variant as this affects minimum version checking logic. // MySQL and Percona share same version scheme (e..g 5.7.10). // MariaDB starts at 10.2.x - sqlVariant := GetSQLVariant(dbComment) - runtime.Log.Info("Database checks: SQL variant " + sqlVariant) + runtime.DbVariant = GetSQLVariant(dbComment) + runtime.Log.Info(fmt.Sprintf("Database checks: SQL variant %v", runtime.DbVariant)) runtime.Log.Info("Database checks: SQL version " + version) verNums, err := GetSQLVersion(version) @@ -77,7 +72,7 @@ func Check(runtime *env.Runtime) bool { // Check minimum MySQL version as we need JSON column type. verInts := []int{5, 7, 10} // Minimum MySQL version - if sqlVariant == sqlVariantMariaDB { + if runtime.DbVariant == env.DBVariantMariaDB { verInts = []int{10, 2, 0} // Minimum MariaDB version } @@ -93,8 +88,8 @@ func Check(runtime *env.Runtime) bool { { // check the MySQL character set and collation if charset != "utf8" && charset != "utf8mb4" { - runtime.Log.Error("MySQL character set not utf8:", errors.New(charset)) - web.SiteInfo.Issue = "MySQL character set not utf8: " + charset + runtime.Log.Error("MySQL character set not utf8/utf8mb4:", errors.New(charset)) + web.SiteInfo.Issue = "MySQL character set not utf8/utf8mb4: " + charset runtime.Flags.SiteMode = env.SiteModeBadDB return false } @@ -147,15 +142,15 @@ func Check(runtime *env.Runtime) bool { } // GetSQLVariant uses database value form @@version_comment to deduce MySQL variant. -func GetSQLVariant(vc string) string { +func GetSQLVariant(vc string) env.DbVariant { vc = strings.ToLower(vc) if strings.Contains(vc, "mariadb") { - return sqlVariantMariaDB + return env.DBVariantMariaDB } else if strings.Contains(vc, "percona") { - return sqlVariantPercona + return env.DBVariantPercona } else if strings.Contains(vc, "mysql") { - return sqlVariantMySQL + return env.DbVariantMySQL } return "UNKNOWN" diff --git a/core/database/migrate.go b/core/database/migrate.go index 17b4dc73..dc945d8b 100644 --- a/core/database/migrate.go +++ b/core/database/migrate.go @@ -78,7 +78,7 @@ func (m migrationsT) migrate(runtime *env.Runtime, tx *sqlx.Tx) error { return err } - err = processSQLfile(tx, buf) + err = processSQLfile(tx, runtime.DbVariant, buf) if err != nil { return err } @@ -247,10 +247,15 @@ func Migrate(runtime *env.Runtime, ConfigTableExists bool) error { return migrateEnd(runtime, tx, nil, amLeader) } -func processSQLfile(tx *sqlx.Tx, buf []byte) error { +func processSQLfile(tx *sqlx.Tx, v env.DbVariant, buf []byte) error { stmts := getStatements(buf) for _, stmt := range stmts { + // MariaDB has no specific JSON column type (but has JSON queries) + if v == env.DBVariantMariaDB { + stmt = strings.Replace(stmt, "` JSON", "` TEXT", -1) + } + _, err := tx.Exec(stmt) if err != nil { return err diff --git a/core/env/runtime.go b/core/env/runtime.go index 3dde6652..9e5588b5 100644 --- a/core/env/runtime.go +++ b/core/env/runtime.go @@ -17,10 +17,11 @@ import "github.com/jmoiron/sqlx" // Runtime provides access to database, logger and other server-level scoped objects. // Use Context for per-request values. type Runtime struct { - Flags Flags - Db *sqlx.DB - Log Logger - Product ProdInfo + Flags Flags + Db *sqlx.DB + DbVariant DbVariant + Log Logger + Product ProdInfo } const ( @@ -33,3 +34,15 @@ const ( // SiteModeBadDB redirects to db-error.html page SiteModeBadDB = "3" ) + +// DbVariant details SQL database variant +type DbVariant string + +const ( + // DbVariantMySQL is MySQL + DbVariantMySQL DbVariant = "MySQL" + // DBVariantPercona is Percona + DBVariantPercona DbVariant = "Percona" + // DBVariantMariaDB is MariaDB + DBVariantMariaDB DbVariant = "MariaDB" +)