Files
new-api/model/migration_dialector.go
CaIon 14a9acd368 feat(custom): CNY native billing, daily check-in button, subscription plans page, usage docs and promo banner
Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed).

- CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels
- New CheckinButton in both authenticated and public headers (Turnstile supported)
- New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset
- New /docs 12-section beginner guide with anchor navigation
- New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi)
- README: document custom features, live demo https://ai.88531.cn
2026-09-23 16:28:52 +08:00

72 lines
2.6 KiB
Go

package model
import (
"strings"
"github.com/shopspring/decimal"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// Embed the concrete dialectors to retain their transaction/savepoint and other
// optional GORM interfaces. Only schema comparison needs normalization.
type mysqlMigrationDialector struct{ mysql.Dialector }
func (d mysqlMigrationDialector) Migrator(db *gorm.DB) gorm.Migrator {
return mysqlSchemaMigrator{d.Dialector.Migrator(db).(mysql.Migrator)}
}
type mysqlSchemaMigrator struct{ mysql.Migrator }
func (m mysqlSchemaMigrator) MigrateColumn(value any, field *schema.Field, column gorm.ColumnType) error {
if !field.HasDefaultValue || !strings.EqualFold(column.DatabaseTypeName(), "decimal") {
return m.Migrator.MigrateColumn(value, field, column)
}
stored, ok := column.DefaultValue()
if !ok {
return m.Migrator.MigrateColumn(value, field, column)
}
storedNumber, storedErr := decimal.NewFromString(stored)
modelNumber, modelErr := decimal.NewFromString(field.DefaultValue)
if storedErr != nil || modelErr != nil || !storedNumber.Equal(modelNumber) {
return m.Migrator.MigrateColumn(value, field, column)
}
// MySQL pads decimal defaults (0 -> 0.000000). Skip only the equivalent
// default comparison, retaining type/size/null checks. AlterColumn still
// reads the original model, including its default.
comparisonField := *field
comparisonField.HasDefaultValue = false
comparisonField.DefaultValue = ""
comparisonField.DefaultValueInterface = nil
return m.Migrator.MigrateColumn(value, &comparisonField, columnWithoutDefault{column})
}
type migrationColumnType interface{ gorm.ColumnType }
type columnWithoutDefault struct{ migrationColumnType }
func (columnWithoutDefault) DefaultValue() (string, bool) { return "", false }
type postgresMigrationDialector struct{ postgres.Dialector }
func (d postgresMigrationDialector) Migrator(db *gorm.DB) gorm.Migrator {
return postgresSchemaMigrator{d.Dialector.Migrator(db).(postgres.Migrator)}
}
type postgresSchemaMigrator struct{ postgres.Migrator }
func (m postgresSchemaMigrator) MigrateColumn(value any, field *schema.Field, column gorm.ColumnType) error {
if column.DatabaseTypeName() == "bpchar" && strings.HasPrefix(strings.ToLower(string(field.DataType)), "char(") {
// PostgreSQL reports CHAR(n) as bpchar. Normalize the name, retaining
// Length() so a real CHAR length change still triggers migration.
column = charColumnType{column}
}
return m.Migrator.MigrateColumn(value, field, column)
}
type charColumnType struct{ migrationColumnType }
func (charColumnType) DatabaseTypeName() string { return "char" }