Replace Paste.Content field by P.URL and FU.PasteID

This commit is contained in:
Daan Sprenkels
2021-05-31 09:27:09 +02:00
parent 29ee3dc6fd
commit a45705540c
5 changed files with 117 additions and 55 deletions

View File

@@ -35,7 +35,11 @@ var (
LogLevel: logger.Warn,
Colorful: true,
})
gormConfig = gorm.Config{Logger: gormLogger, PrepareStmt: true}
gormConfig = gorm.Config{
Logger: gormLogger,
PrepareStmt: true,
DisableForeignKeyConstraintWhenMigrating: true,
}
)
// OpenDBFromEnvironment tries to open an SQL database, described by

View File

@@ -38,6 +38,8 @@ type FileUpload struct {
// UUID publically identifies this FileUpload.
PubID uuid.UUID `gorm:"uniqueIndex"`
PasteID uint
// FileName contains the original filename of this FileUpload.
FileName string

View File

@@ -38,8 +38,69 @@ func Gormigrate(db *gorm.DB) *gormigrate.Gormigrate {
}
return tx.AutoMigrate(&FileUpload{}, &Paste{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable(&FileUpload{}, &Paste{})
},
{
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
},
},
})

View File

@@ -8,9 +8,9 @@ import (
"strings"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// PasteType describes the type of Paste (i.e. file, redirect, [...]).
@@ -24,7 +24,8 @@ 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
@@ -146,7 +147,7 @@ func ValidatePasteKey(key string) error {
// the key format first.
func GetPasteNoValidate(db *gorm.DB, key string) (*Paste, error) {
var ps []Paste
if err := db.Unscoped().Limit(1).Where("key = ?", key).Find(&ps).Error; err != nil {
if err := db.Unscoped().Preload(clause.Associations).Where("key = ?", key).Find(&ps).Error; err != nil {
return nil, err
}
if len(ps) == 0 {
@@ -163,16 +164,8 @@ func (p *Paste) Save(db *gorm.DB) error {
// Delete deletes this Paste from the database.
func (p *Paste) Delete(db *gorm.DB, fs *FileStore) error {
// Remove the (maybe) attached file
if p.Type == PasteTypeFileUpload {
fuID, err := uuid.FromBytes(p.Content)
if err != nil {
return errors.Wrap(err, "failed to parse uuid")
}
fu, err := GetFileUpload(db, fuID)
if err != nil {
return errors.Wrap(err, "failed to find file in database")
}
if err := fu.Delete(db, fs); err != nil {
if p.FileUpload != nil {
if err := p.FileUpload.Delete(db, fs); err != nil {
return errors.Wrap(err, "failed to remove file")
}
}
@@ -180,7 +173,9 @@ func (p *Paste) Delete(db *gorm.DB, fs *FileStore) error {
// Wipe the old paste
p.Type = PasteTypeUndef
p.State = PasteStateDeleted
p.Content = []byte{}
p.URL = ""
p.FileUpload = nil
if err := db.Save(&p).Error; err != nil {
return errors.Wrap(err, "failed to wipe paste in database")
}
@@ -199,10 +194,9 @@ func (p *Paste) RedirectURL() *url.URL {
if p.Type != PasteTypeRedirect {
panic("expected p.Type to be PasteTypeRedirect")
}
rawurl := string(p.Content)
urlParse, err := url.Parse(rawurl)
urlParse, err := url.Parse(p.URL)
if err != nil {
panic(errors.Wrapf(err, "invalid URL ('%v') in database for key '%v'", rawurl, p.Key))
panic(errors.Wrapf(err, "invalid URL ('%v') in database for key '%v'", p.URL, p.Key))
}
return urlParse
}