|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "gorm.io/gorm" |
| 7 | + "gorm.io/gorm/clause" |
| 8 | + "gorm.io/gorm/migrator" |
| 9 | + "gorm.io/gorm/schema" |
| 10 | +) |
| 11 | + |
| 12 | +type Migrator struct { |
| 13 | + migrator.Migrator |
| 14 | +} |
| 15 | + |
| 16 | +func (m Migrator) CurrentDatabase() (name string) { |
| 17 | + m.DB.Raw("SELECT CURRENT_DATABASE()").Row().Scan(&name) |
| 18 | + return |
| 19 | +} |
| 20 | + |
| 21 | +func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{}) { |
| 22 | + for _, opt := range opts { |
| 23 | + str := stmt.Quote(opt.DBName) |
| 24 | + if opt.Expression != "" { |
| 25 | + str = opt.Expression |
| 26 | + } |
| 27 | + |
| 28 | + if opt.Collate != "" { |
| 29 | + str += " COLLATE " + opt.Collate |
| 30 | + } |
| 31 | + |
| 32 | + if opt.Sort != "" { |
| 33 | + str += " " + opt.Sort |
| 34 | + } |
| 35 | + results = append(results, clause.Expr{SQL: str}) |
| 36 | + } |
| 37 | + return |
| 38 | +} |
| 39 | + |
| 40 | +func (m Migrator) HasIndex(value interface{}, name string) bool { |
| 41 | + var count int64 |
| 42 | + m.RunWithValue(value, func(stmt *gorm.Statement) error { |
| 43 | + if idx := stmt.Schema.LookIndex(name); idx != nil { |
| 44 | + name = idx.Name |
| 45 | + } |
| 46 | + |
| 47 | + return m.DB.Raw( |
| 48 | + "SELECT count(*) FROM pg_indexes WHERE tablename = ? AND indexname = ? AND schemaname = CURRENT_SCHEMA()", stmt.Table, name, |
| 49 | + ).Row().Scan(&count) |
| 50 | + }) |
| 51 | + |
| 52 | + return count > 0 |
| 53 | +} |
| 54 | + |
| 55 | +func (m Migrator) CreateIndex(value interface{}, name string) error { |
| 56 | + return m.RunWithValue(value, func(stmt *gorm.Statement) error { |
| 57 | + if idx := stmt.Schema.LookIndex(name); idx != nil { |
| 58 | + opts := m.BuildIndexOptions(idx.Fields, stmt) |
| 59 | + values := []interface{}{clause.Column{Name: idx.Name}, clause.Table{Name: stmt.Table}, opts} |
| 60 | + |
| 61 | + createIndexSQL := "CREATE " |
| 62 | + if idx.Class != "" { |
| 63 | + createIndexSQL += idx.Class + " " |
| 64 | + } |
| 65 | + createIndexSQL += "INDEX ?" |
| 66 | + |
| 67 | + if idx.Type != "" { |
| 68 | + createIndexSQL += " USING " + idx.Type |
| 69 | + } |
| 70 | + createIndexSQL += " ON ??" |
| 71 | + |
| 72 | + if idx.Where != "" { |
| 73 | + createIndexSQL += " WHERE " + idx.Where |
| 74 | + } |
| 75 | + |
| 76 | + return m.DB.Exec(createIndexSQL, values...).Error |
| 77 | + } |
| 78 | + |
| 79 | + return fmt.Errorf("failed to create index with name %v", name) |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error { |
| 84 | + return m.RunWithValue(value, func(stmt *gorm.Statement) error { |
| 85 | + return m.DB.Exec( |
| 86 | + "ALTER INDEX ? RENAME TO ?", |
| 87 | + clause.Column{Name: oldName}, clause.Column{Name: newName}, |
| 88 | + ).Error |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +func (m Migrator) DropIndex(value interface{}, name string) error { |
| 93 | + return m.RunWithValue(value, func(stmt *gorm.Statement) error { |
| 94 | + if idx := stmt.Schema.LookIndex(name); idx != nil { |
| 95 | + name = idx.Name |
| 96 | + } |
| 97 | + |
| 98 | + return m.DB.Exec("DROP INDEX ?", clause.Column{Name: name}).Error |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func (m Migrator) HasTable(value interface{}) bool { |
| 103 | + var count int64 |
| 104 | + m.RunWithValue(value, func(stmt *gorm.Statement) error { |
| 105 | + return m.DB.Raw("SELECT count(*) FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA() AND table_name = ? AND table_type = ?", stmt.Table, "BASE TABLE").Row().Scan(&count) |
| 106 | + }) |
| 107 | + |
| 108 | + return count > 0 |
| 109 | +} |
| 110 | + |
| 111 | +func (m Migrator) DropTable(values ...interface{}) error { |
| 112 | + values = m.ReorderModels(values, false) |
| 113 | + tx := m.DB.Session(&gorm.Session{}) |
| 114 | + for i := len(values) - 1; i >= 0; i-- { |
| 115 | + if err := m.RunWithValue(values[i], func(stmt *gorm.Statement) error { |
| 116 | + return tx.Exec("DROP TABLE IF EXISTS ? CASCADE", clause.Table{Name: stmt.Table}).Error |
| 117 | + }); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (m Migrator) HasColumn(value interface{}, field string) bool { |
| 125 | + var count int64 |
| 126 | + m.RunWithValue(value, func(stmt *gorm.Statement) error { |
| 127 | + name := field |
| 128 | + if field := stmt.Schema.LookUpField(field); field != nil { |
| 129 | + name = field.DBName |
| 130 | + } |
| 131 | + |
| 132 | + return m.DB.Raw( |
| 133 | + "SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_schema = CURRENT_SCHEMA() AND table_name = ? AND column_name = ?", |
| 134 | + stmt.Table, name, |
| 135 | + ).Row().Scan(&count) |
| 136 | + }) |
| 137 | + |
| 138 | + return count > 0 |
| 139 | +} |
0 commit comments