Skip to content

Commit

Permalink
feat: ddl when migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Sep 26, 2023
1 parent 395de08 commit 9d82392
Showing 1 changed file with 112 additions and 2 deletions.
114 changes: 112 additions & 2 deletions cmd/migrate/danke_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,85 @@ func DankeV3() {
logger.Info("migrate danke v3")

err = db.Transaction(func(tx *gorm.DB) error {
logger.Info("migrate danke v3: get reviews")
err = db.Table("review").FindInBatches(&reviews, 1000, func(tx *gorm.DB, batch int) error {
m := tx.Migrator()

if m.HasConstraint(&model.Course{}, "course_ibfk_1") {
err = m.DropConstraint(&model.Course{}, "course_ibfk_1")
if err != nil {
return err
}
}

if m.HasConstraint(&model.Review{}, "review_ibfk_1") {
err = m.DropConstraint(&model.Review{}, "review_ibfk_1")
if err != nil {
return err
}
}

if m.HasTable("coursegroup") {
err = m.RenameTable("coursegroup", "course_group")
if err != nil {
return err
}
}

if m.HasColumn(&model.Course{}, "coursegroup_id") {
err = m.RenameColumn(&model.Course{}, "coursegroup_id", "course_group_id")
if err != nil {
return err
}
}

if m.HasColumn(&model.Review{}, "time_created") {
err = m.RenameColumn(&model.Review{}, "time_created", "created_at")
if err != nil {
return err
}
}

if m.HasColumn(&model.Review{}, "time_updated") {
err = m.RenameColumn(&model.Review{}, "time_updated", "updated_at")
if err != nil {
return err
}
}

if m.HasIndex(&model.Course{}, "coursegroup_id") {
err = m.RenameIndex(&model.Course{}, "coursegroup_id", "idx_course_course_group_id")
if err != nil {
return err
}
}

if m.HasIndex(&model.Review{}, "course_id") {
err = m.RenameIndex(&model.Review{}, "course_id", "idx_review_course_id")
if err != nil {
return err
}
}

if m.HasIndex(&model.Review{}, "reviewer_id") {
err = m.RenameIndex(&model.Review{}, "reviewer_id", "idx_review_reviewer_id")
if err != nil {
return err
}
}

err = m.AutoMigrate(
&model.Course{},
&model.Review{},
&model.ReviewHistory{},
&model.ReviewVote{},
&model.CourseGroup{},
&model.UserAchievement{},
)
if err != nil {
return err
}

logger.Info("migrate danke v3: update review history and vote")
err = tx.Table("review").FindInBatches(&reviews, 1000, func(tx *gorm.DB, batch int) error {
// update History
var (
newHistory []*model.ReviewHistory
Expand Down Expand Up @@ -156,6 +233,39 @@ where true`).Error
return err
}

if m.HasColumn(&model.Review{}, "upvoters") {
err = m.DropColumn(&model.Review{}, "upvoters")
if err != nil {
return err
}
}

if m.HasColumn(&model.Review{}, "downvoters") {
err = m.DropColumn(&model.Review{}, "downvoters")
if err != nil {
return err
}
}

if m.HasColumn(&model.Review{}, "rank") {
err = m.DropColumn(&model.Review{}, "rank")
if err != nil {
return err
}
}

if m.HasColumn(&model.Review{}, "history") {
err = m.DropColumn(&model.Review{}, "history")
if err != nil {
return err
}
}

err = m.DropTable("userextra", "aerich", "seaql_migrations")
if err != nil {
return err
}

return nil
})

Expand Down

0 comments on commit 9d82392

Please sign in to comment.