v1.2.0: back: add DropUnusedColumns
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
b7e44e85af
commit
82a057f131
1 changed files with 23 additions and 0 deletions
|
|
@ -11,6 +11,28 @@ type Users struct {
|
|||
Password string `gorm:""`
|
||||
}
|
||||
|
||||
func DropUnusedColumns(DB *gorm.DB, values ...interface{}) {
|
||||
|
||||
for _, dst := range values {
|
||||
stmt := &gorm.Statement{DB: DB}
|
||||
stmt.Parse(dst)
|
||||
fields := stmt.Schema.Fields
|
||||
columns, _ := DB.Debug().Migrator().ColumnTypes(dst)
|
||||
|
||||
for i := range columns {
|
||||
found := false
|
||||
for j := range fields {
|
||||
if columns[i].Name() == fields[j].DBName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
DB.Migrator().DropColumn(dst, columns[i].Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Init_database() {
|
||||
dsn := "postgres://acc:totor@postgres:5432/accounts?sslmode=disable"
|
||||
|
|
@ -18,4 +40,5 @@ func Init_database() {
|
|||
if err != nil {
|
||||
}
|
||||
db.AutoMigrate(&Users{})
|
||||
DropUnusedColumns(db, &Users{})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue