|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/QuantumNous/new-api/common" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "gorm.io/gorm" |
| 13 | +) |
| 14 | + |
| 15 | +type MigrationIdentityFields struct { |
| 16 | + ID int `gorm:"primaryKey"` |
| 17 | + Name string `gorm:"size:64;unique"` |
| 18 | + Reference string `gorm:"size:64;uniqueIndex"` |
| 19 | + Provider string `gorm:"size:32;uniqueIndex:,composite:provider_subject"` |
| 20 | + Subject string `gorm:"size:64;uniqueIndex:,composite:provider_subject"` |
| 21 | +} |
| 22 | + |
| 23 | +type migrationIdentityV1 struct { |
| 24 | + MigrationIdentityFields |
| 25 | + Digest string `gorm:"type:char(32)"` |
| 26 | +} |
| 27 | + |
| 28 | +type migrationIdentityV2 struct { |
| 29 | + MigrationIdentityFields |
| 30 | + Digest string `gorm:"type:char(64)"` |
| 31 | + Note string `gorm:"size:128"` |
| 32 | +} |
| 33 | + |
| 34 | +type migrationConstraintV1 struct { |
| 35 | + ID int `gorm:"primaryKey"` |
| 36 | + Name string `gorm:"size:64"` |
| 37 | +} |
| 38 | + |
| 39 | +type migrationConstraintV2 struct { |
| 40 | + ID int `gorm:"primaryKey"` |
| 41 | + Name string `gorm:"size:64;unique"` |
| 42 | +} |
| 43 | + |
| 44 | +type migrationDecimalV1 struct { |
| 45 | + ID int `gorm:"primaryKey"` |
| 46 | + Price float64 `gorm:"type:decimal(10,6);default:0"` |
| 47 | +} |
| 48 | + |
| 49 | +type migrationDecimalV2 struct { |
| 50 | + ID int `gorm:"primaryKey"` |
| 51 | + Price float64 `gorm:"type:decimal(12,6);not null;default:0"` |
| 52 | +} |
| 53 | + |
| 54 | +type migrationDecimalV3 struct { |
| 55 | + ID int `gorm:"primaryKey"` |
| 56 | + Price float64 `gorm:"type:decimal(12,6);not null;default:1.25"` |
| 57 | +} |
| 58 | + |
| 59 | +func TestMigrationSchemaStability(t *testing.T) { |
| 60 | + for _, dialect := range []string{"sqlite", "mysql", "postgres"} { |
| 61 | + t.Run(dialect, func(t *testing.T) { |
| 62 | + var dsn string |
| 63 | + switch dialect { |
| 64 | + case "sqlite": |
| 65 | + dsn = "local" |
| 66 | + previousPath := common.SQLitePath |
| 67 | + common.SQLitePath = filepath.Join(t.TempDir(), "migration.db") |
| 68 | + t.Cleanup(func() { common.SQLitePath = previousPath }) |
| 69 | + case "mysql": |
| 70 | + dsn = os.Getenv("TEST_MYSQL_DSN") |
| 71 | + case "postgres": |
| 72 | + dsn = os.Getenv("TEST_POSTGRES_DSN") |
| 73 | + } |
| 74 | + if dsn == "" { |
| 75 | + t.Skip("test database DSN is not configured") |
| 76 | + } |
| 77 | + t.Setenv("MIGRATION_TEST_DSN", dsn) |
| 78 | + db, _, err := chooseDB("MIGRATION_TEST_DSN", false) |
| 79 | + require.NoError(t, err) |
| 80 | + sqlDB, err := db.DB() |
| 81 | + require.NoError(t, err) |
| 82 | + t.Cleanup(func() { _ = sqlDB.Close() }) |
| 83 | + recorder := &migrationSQLRecorder{} |
| 84 | + db = db.Session(&gorm.Session{Logger: recorder}) |
| 85 | + |
| 86 | + t.Run("identity_and_indexes", func(t *testing.T) { |
| 87 | + const table = "migration_identity_test" |
| 88 | + t.Cleanup(func() { _ = db.Migrator().DropTable(table) }) |
| 89 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationIdentityV1{})) |
| 90 | + row := migrationIdentityV1{ |
| 91 | + MigrationIdentityFields: MigrationIdentityFields{ID: 1, Name: "root", Reference: "token-reference", Provider: "oidc", Subject: "subject"}, |
| 92 | + Digest: "old-digest", |
| 93 | + } |
| 94 | + require.NoError(t, db.Table(table).Create(&row).Error) |
| 95 | + recorder.reset() |
| 96 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationIdentityV1{})) |
| 97 | + assert.Empty(t, recorder.schemaMutations()) |
| 98 | + |
| 99 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationIdentityV2{})) |
| 100 | + columns, err := db.Table(table).Migrator().ColumnTypes(&migrationIdentityV2{}) |
| 101 | + require.NoError(t, err) |
| 102 | + for _, column := range columns { |
| 103 | + if column.Name() == "digest" { |
| 104 | + length, ok := column.Length() |
| 105 | + require.True(t, ok) |
| 106 | + assert.EqualValues(t, 64, length) |
| 107 | + } |
| 108 | + } |
| 109 | + assert.True(t, db.Table(table).Migrator().HasColumn(&migrationIdentityV2{}, "note")) |
| 110 | + recorder.reset() |
| 111 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationIdentityV2{})) |
| 112 | + assert.Empty(t, recorder.schemaMutations()) |
| 113 | + var saved migrationIdentityV2 |
| 114 | + require.NoError(t, db.Table(table).First(&saved, 1).Error) |
| 115 | + assert.Equal(t, row.MigrationIdentityFields, saved.MigrationIdentityFields) |
| 116 | + expectedDigest := row.Digest |
| 117 | + if dialect == "postgres" { |
| 118 | + expectedDigest += strings.Repeat(" ", 64-len(row.Digest)) |
| 119 | + } |
| 120 | + assert.Equal(t, expectedDigest, saved.Digest) |
| 121 | + for _, duplicate := range []migrationIdentityV2{ |
| 122 | + {MigrationIdentityFields: MigrationIdentityFields{Name: "root", Reference: "other-1", Provider: "other", Subject: "1"}}, |
| 123 | + {MigrationIdentityFields: MigrationIdentityFields{Name: "other-2", Reference: "token-reference", Provider: "other", Subject: "2"}}, |
| 124 | + {MigrationIdentityFields: MigrationIdentityFields{Name: "other-3", Reference: "other-3", Provider: "oidc", Subject: "subject"}}, |
| 125 | + } { |
| 126 | + assert.Error(t, db.Table(table).Create(&duplicate).Error) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("unique_constraint_changes", func(t *testing.T) { |
| 131 | + const table = "migration_constraint_test" |
| 132 | + t.Cleanup(func() { _ = db.Migrator().DropTable(table) }) |
| 133 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationConstraintV1{})) |
| 134 | + require.NoError(t, db.Table(table).Create(&migrationConstraintV1{Name: "existing"}).Error) |
| 135 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationConstraintV2{})) |
| 136 | + assert.Error(t, db.Table(table).Create(&migrationConstraintV2{Name: "existing"}).Error) |
| 137 | + recorder.reset() |
| 138 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationConstraintV2{})) |
| 139 | + assert.Empty(t, recorder.schemaMutations()) |
| 140 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationConstraintV1{})) |
| 141 | + require.NoError(t, db.Table(table).Create(&migrationConstraintV1{Name: "existing"}).Error) |
| 142 | + }) |
| 143 | + |
| 144 | + if dialect == "mysql" { |
| 145 | + t.Run("decimal_default_and_real_changes", func(t *testing.T) { |
| 146 | + const table = "migration_decimal_test" |
| 147 | + t.Cleanup(func() { _ = db.Migrator().DropTable(table) }) |
| 148 | + require.NoError(t, db.Table(table).AutoMigrate(&migrationDecimalV1{})) |
| 149 | + require.NoError(t, db.Table(table).Create(&migrationDecimalV1{ID: 1, Price: 12.345678}).Error) |
| 150 | + for _, target := range []any{&migrationDecimalV1{}, &migrationDecimalV2{}, &migrationDecimalV3{}} { |
| 151 | + require.NoError(t, db.Table(table).AutoMigrate(target)) |
| 152 | + recorder.reset() |
| 153 | + require.NoError(t, db.Table(table).AutoMigrate(target)) |
| 154 | + assert.Empty(t, recorder.schemaMutations()) |
| 155 | + } |
| 156 | + columns, err := db.Table(table).Migrator().ColumnTypes(&migrationDecimalV3{}) |
| 157 | + require.NoError(t, err) |
| 158 | + for _, column := range columns { |
| 159 | + if column.Name() == "price" { |
| 160 | + precision, scale, ok := column.DecimalSize() |
| 161 | + require.True(t, ok) |
| 162 | + assert.EqualValues(t, 12, precision) |
| 163 | + assert.EqualValues(t, 6, scale) |
| 164 | + nullable, ok := column.Nullable() |
| 165 | + require.True(t, ok) |
| 166 | + assert.False(t, nullable) |
| 167 | + } |
| 168 | + } |
| 169 | + require.NoError(t, db.Table(table).Create(&map[string]any{"id": 2}).Error) |
| 170 | + var prices []float64 |
| 171 | + require.NoError(t, db.Table(table).Order("id").Pluck("price", &prices).Error) |
| 172 | + assert.Equal(t, []float64{12.345678, 1.25}, prices) |
| 173 | + }) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments