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:
Matthew Jacobson 2023-06-22 15:09:36 -04:00 committed by GitHub
parent 404afedc25
commit 44972d0cd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 10 deletions

View File

@ -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
}

View File

@ -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 {