To test auto migrate crate indexes for dynamic table name https://github.com/go-gorm/gorm/issues/4752
(t *testing.T)
| 836 | // To test auto migrate crate indexes for dynamic table name |
| 837 | // https://github.com/go-gorm/gorm/issues/4752 |
| 838 | func TestMigrateIndexesWithDynamicTableName(t *testing.T) { |
| 839 | // Create primary table |
| 840 | if err := DB.AutoMigrate(&DynamicUser{}); err != nil { |
| 841 | t.Fatalf("AutoMigrate create table error: %#v", err) |
| 842 | } |
| 843 | |
| 844 | // Create sub tables |
| 845 | for _, v := range []string{"01", "02", "03"} { |
| 846 | tableName := "dynamic_users_" + v |
| 847 | m := DB.Scopes(func(db *gorm.DB) *gorm.DB { |
| 848 | return db.Table(tableName) |
| 849 | }).Migrator() |
| 850 | |
| 851 | if err := m.AutoMigrate(&DynamicUser{}); err != nil { |
| 852 | t.Fatalf("AutoMigrate create table error: %#v", err) |
| 853 | } |
| 854 | |
| 855 | if !m.HasTable(tableName) { |
| 856 | t.Fatalf("AutoMigrate expected %#v exist, but not.", tableName) |
| 857 | } |
| 858 | |
| 859 | if !m.HasIndex(&DynamicUser{}, "CompanyID") { |
| 860 | t.Fatalf("Should have index on %s", "CompanyI.") |
| 861 | } |
| 862 | |
| 863 | if !m.HasIndex(&DynamicUser{}, "DeletedAt") { |
| 864 | t.Fatalf("Should have index on deleted_at.") |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | // check column order after migration, flaky test |
| 870 | // https://github.com/go-gorm/gorm/issues/4351 |