/
autoMigrate.go
70 lines (59 loc) · 1.45 KB
/
autoMigrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package migrate to migrate the schema
package migrate
import (
"fmt"
"github.com/pilinux/gorest/config"
"github.com/pilinux/gorest/database"
"github.com/pilinux/gorest/database/model"
)
// Load all the models
type auth model.Auth
type twoFA model.TwoFA
type twoFABackup model.TwoFABackup
type tempEmail model.TempEmail
// DropAllTables - careful! It will drop all the tables!
func DropAllTables() error {
db := database.GetDB()
if err := db.Migrator().DropTable(
&tempEmail{},
&twoFABackup{},
&twoFA{},
&auth{},
); err != nil {
return err
}
fmt.Println("old tables are deleted!")
return nil
}
// StartMigration - automatically migrate all the tables
//
// - Only create tables with missing columns and missing indexes
// - Will not change/delete any existing columns and their types
func StartMigration(configure config.Configuration) error {
db := database.GetDB()
configureDB := configure.Database.RDBMS
driver := configureDB.Env.Driver
if driver == "mysql" {
// db.Set() --> add table suffix during auto migration
if err := db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
&auth{},
&twoFA{},
&twoFABackup{},
&tempEmail{},
); err != nil {
return err
}
fmt.Println("new tables are migrated successfully!")
return nil
}
if err := db.AutoMigrate(
&auth{},
&twoFA{},
&twoFABackup{},
&tempEmail{},
); err != nil {
return err
}
fmt.Println("new tables are migrated successfully!")
return nil
}