2020-10-25 17:33:51 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gormigrate "github.com/go-gormigrate/gormigrate/v2"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gormigrate returns a Gormigrate migrator for the database.
|
|
|
|
func Gormigrate(db *gorm.DB) *gormigrate.Gormigrate {
|
|
|
|
return gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
|
|
|
|
{
|
|
|
|
ID: "202010251337",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
type FileUpload struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
State FileUploadState `gorm:"index"`
|
|
|
|
PubID uuid.UUID `gorm:"uniqueIndex"`
|
|
|
|
FileName string
|
|
|
|
ContentType string
|
|
|
|
Checksum uint32
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt gorm.DeletedAt
|
|
|
|
}
|
|
|
|
type Paste struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
Type PasteType `gorm:"index"`
|
|
|
|
State PasteState `gorm:"index"`
|
|
|
|
Content []byte
|
|
|
|
Key string `gorm:"uniqueIndex"`
|
|
|
|
DeleteToken string
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt gorm.DeletedAt
|
|
|
|
}
|
|
|
|
return tx.AutoMigrate(&FileUpload{}, &Paste{})
|
|
|
|
},
|
2021-05-31 09:27:09 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "202107231722",
|
|
|
|
Migrate: func(tx *gorm.DB) error {
|
|
|
|
// Update the schema
|
|
|
|
type FileUpload struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
State FileUploadState `gorm:"index"`
|
|
|
|
PubID uuid.UUID `gorm:"uniqueIndex"`
|
|
|
|
PasteID uint
|
|
|
|
FileName string
|
|
|
|
ContentType string
|
|
|
|
Checksum uint32
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt gorm.DeletedAt
|
|
|
|
}
|
|
|
|
type Paste struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
Type PasteType `gorm:"index"`
|
|
|
|
State PasteState `gorm:"index"`
|
|
|
|
Content []byte
|
|
|
|
URL string
|
|
|
|
FileUpload *FileUpload
|
|
|
|
Key string `gorm:"uniqueIndex"`
|
|
|
|
DeleteToken string
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt gorm.DeletedAt
|
|
|
|
}
|
|
|
|
err := tx.AutoMigrate(&FileUpload{}, &Paste{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate the data
|
|
|
|
pastes := make([]Paste, 0)
|
|
|
|
if err := tx.Model(&Paste{}).Find(&pastes).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, p := range pastes {
|
|
|
|
switch p.Type {
|
|
|
|
case PasteTypeRedirect:
|
|
|
|
p.URL = string(p.Content)
|
|
|
|
tx.Model(p).Select("Content", "URL").Updates(p)
|
|
|
|
case PasteTypeFileUpload:
|
|
|
|
var id uuid.UUID
|
|
|
|
var fus []FileUpload
|
|
|
|
copy(id[:], p.Content)
|
|
|
|
if err := db.Unscoped().Limit(1).Where("pub_id = ?", id).Find(&fus).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.FileUpload = &fus[0]
|
|
|
|
tx.Model(p).Select("Content", "FileUpload").Updates(p)
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently there is a bug in GORM, which causes a nil ptr
|
|
|
|
// dereference when you try dropping a column in an sqlite3
|
|
|
|
// database.
|
|
|
|
return nil
|
2020-10-25 17:33:51 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|