You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
824 B
44 lines
824 B
4 years ago
|
package db
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type localMigration struct {
|
||
|
Name string
|
||
|
Query string
|
||
|
}
|
||
|
|
||
|
var migrations = []localMigration{
|
||
|
{
|
||
|
Name: "2020_11_03_04_42_SetDefaultDownloadStatus",
|
||
|
Query: "update podcast_items set download_status=2 where download_path!='' and download_status=0",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func RunMigrations() {
|
||
|
for _, mig := range migrations {
|
||
|
ExecuteAndSaveMigration(mig.Name, mig.Query)
|
||
|
}
|
||
|
}
|
||
|
func ExecuteAndSaveMigration(name string, query string) error {
|
||
|
var migration Migration
|
||
|
result := DB.Where("name=?", name).First(&migration)
|
||
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||
|
fmt.Println(query)
|
||
|
result = DB.Debug().Exec(query)
|
||
|
if result.Error == nil {
|
||
|
DB.Save(&Migration{
|
||
|
Date: time.Now(),
|
||
|
Name: name,
|
||
|
})
|
||
|
}
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|