mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 21:19:28 -06:00
SQLStore: Fix Postgres dialect treating "false" migrator default as true (#69353)
* SQLStore: Fix Postgres dialect treating "false" migrator default as true Previously, when creating a migration you could choose a default value for a new boolean column that looked correct but would be interpreted incorrectly by the Postgres dialect. For example, values such as "false" or "FALSE" would be treated as true by the Postgres dialect. This refactors how migration dialects determine the Default column value for boolean type columns. Each dialect now uses the same base code to parse the Default literal and panics if an unknown value is encountered. So, now AddColumnMigration and AddTableMigration will ensure that across dialects: - The exact same Default literals will be allowed. - The literals are converted to equivalent defaults in their DDL. - An error will be thrown if an invalid literal is provided.
This commit is contained in:
parent
404afedc25
commit
44972d0cd5
@ -2,6 +2,7 @@ package migrator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xorm.io/xorm"
|
||||
@ -129,6 +130,14 @@ func (b *BaseDialect) EqStr() string {
|
||||
}
|
||||
|
||||
func (b *BaseDialect) Default(col *Column) string {
|
||||
if col.Type == DB_Bool {
|
||||
// Ensure that all dialects support the same literals in the same way.
|
||||
bl, err := strconv.ParseBool(col.Default)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to create default value for column '%s': invalid boolean default value '%s'", col.Name, col.Default))
|
||||
}
|
||||
return b.dialect.BooleanStr(bl)
|
||||
}
|
||||
return col.Default
|
||||
}
|
||||
|
||||
|
@ -46,16 +46,6 @@ func (db *PostgresDialect) BatchSize() int {
|
||||
return 1000
|
||||
}
|
||||
|
||||
func (db *PostgresDialect) Default(col *Column) string {
|
||||
if col.Type == DB_Bool {
|
||||
if col.Default == "0" {
|
||||
return "FALSE"
|
||||
}
|
||||
return "TRUE"
|
||||
}
|
||||
return col.Default
|
||||
}
|
||||
|
||||
func (db *PostgresDialect) SQLType(c *Column) string {
|
||||
var res string
|
||||
switch t := c.Type; t {
|
||||
|
Loading…
Reference in New Issue
Block a user