rushlink/internal/db/migrate.go

47 lines
1.2 KiB
Go
Raw Normal View History

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{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable(&FileUpload{}, &Paste{})
},
},
})
}