Add Support for Advance Labels ( LabelV2)

Add Support for Custom Migration to keep supporting two database( sqlite and postgres)
Migration from Label to LabelV2
This commit is contained in:
Mo Tarbin 2024-11-23 20:29:53 -05:00
parent 6dc8092ff4
commit 0c07b33359
12 changed files with 730 additions and 49 deletions

View file

@ -0,0 +1,123 @@
package migrations
import (
"context"
"strings"
"donetick.com/core/logging"
"gorm.io/gorm"
)
type MigrateLabels20241123 struct{}
func (m MigrateLabels20241123) ID() string {
return "20241123_migrate_label_to_labels_v2"
}
func (m MigrateLabels20241123) Description() string {
return `Migrate label to labels v2 table, Allow more advanced features with labels like assign color`
}
func (m MigrateLabels20241123) Down(ctx context.Context, db *gorm.DB) error {
return nil
}
func (m MigrateLabels20241123) Up(ctx context.Context, db *gorm.DB) error {
log := logging.FromContext(ctx)
type Label struct {
ID int `gorm:"column:id;primary_key"`
Name string `gorm:"column:name"`
CreatedBy int `gorm:"column:created_by"`
CircleID *int `gorm:"column:circle_id"`
ChoresId []int `gorm:"-"`
}
type Chore struct {
Labels *string `gorm:"column:labels"`
ID int `gorm:"column:id;primary_key"`
CircleID int `gorm:"column:circle_id"`
CreatedBy int `gorm:"column:created_by"`
}
type ChoreLabel struct {
ChoreID int `gorm:"column:chore_id"`
LabelID int `gorm:"column:label_id"`
UserID int `gorm:"column:user_id"`
}
// Start a transaction
return db.Transaction(func(tx *gorm.DB) error {
// Get all chores with labels
var choreRecords []Chore
if err := tx.Table("chores").Select("id, labels, circle_id, created_by").Find(&choreRecords).Error; err != nil {
log.Errorf("Failed to fetch chores with label: %v", err)
return err
}
// Map to store new labels
newLabelsMap := make(map[string]Label)
for _, choreRecord := range choreRecords {
if choreRecord.Labels != nil {
labels := strings.Split(*choreRecord.Labels, ",")
for _, label := range labels {
label = strings.TrimSpace(label)
if _, ok := newLabelsMap[label]; !ok {
newLabelsMap[label] = Label{
Name: label,
CreatedBy: choreRecord.CreatedBy,
CircleID: &choreRecord.CircleID,
ChoresId: []int{choreRecord.ID},
}
} else {
labelToUpdate := newLabelsMap[label]
labelToUpdate.ChoresId = append(labelToUpdate.ChoresId, choreRecord.ID)
newLabelsMap[label] = labelToUpdate
}
}
}
}
// Insert new labels and update chore_labels
for labelName, label := range newLabelsMap {
// Check if the label already exists
var existingLabel Label
if err := tx.Table("labels").Where("name = ? AND created_by = ? AND circle_id = ?", labelName, label.CreatedBy, label.CircleID).First(&existingLabel).Error; err != nil {
if err == gorm.ErrRecordNotFound {
// Insert new label
if err := tx.Table("labels").Create(&label).Error; err != nil {
log.Errorf("Failed to insert new label: %v", err)
return err
}
existingLabel = label
} else {
log.Errorf("Failed to check existing label: %v", err)
return err
}
}
// Prepare chore_labels for batch insertion
var choreLabels []ChoreLabel
for _, choreId := range label.ChoresId {
choreLabels = append(choreLabels, ChoreLabel{
ChoreID: choreId,
LabelID: existingLabel.ID,
UserID: label.CreatedBy,
})
}
// Batch insert chore_labels
if err := tx.Table("chore_labels").Create(&choreLabels).Error; err != nil {
log.Errorf("Failed to insert chore labels: %v", err)
return err
}
}
return nil
})
}
// Register this migration
func init() {
Register(MigrateLabels20241123{})
}

100
migrations/base.go Normal file
View file

@ -0,0 +1,100 @@
package migrations
import (
"context"
"time"
"donetick.com/core/logging"
"gorm.io/gorm"
)
type Migration struct {
// DB Representation for migration table, mainly used for tracking the migration status.
ID string `json:"id" gorm:"primary_key"`
Description string `json:"description" gorm:"column:description"`
AppliedAt time.Time `json:"appliedAt" gorm:"column:applied_at"`
}
type MigrationScript interface {
// Actual migration script interface which needs to be implemented by each migration script.
Description() string
ID() string
Up(ctx context.Context, db *gorm.DB) error
Down(ctx context.Context, db *gorm.DB) error
}
var registry []MigrationScript
// Register a migration
func Register(migration MigrationScript) {
registry = append(registry, migration)
}
// Sort the migrations by ID
func sortMigrations(migrations []MigrationScript) []MigrationScript {
for i := 0; i < len(migrations); i++ {
for j := i + 1; j < len(migrations); j++ {
if migrations[i].ID() > migrations[j].ID() {
migrations[i], migrations[j] = migrations[j], migrations[i]
}
}
}
return migrations
}
// Run pending migrations
func Run(ctx context.Context, db *gorm.DB) error {
log := logging.FromContext(ctx)
// Confirm the migrations table exists :)
if err := db.AutoMigrate(&Migration{}); err != nil {
return err
}
// Sort the registry by ID
registry = sortMigrations(registry)
var successfulCount int
var skippedCount int
for _, migration := range registry {
// Check if migration is already applied
var count int64
db.Model(&Migration{}).Where("id = ?", migration.ID()).Count(&count)
if count > 0 {
skippedCount++
log.Debug("Skipping migration %s as it is already applied", migration.ID())
continue
}
// Run the migration
log.Infof("Applying migration: %s", migration.ID())
if err := migration.Up(ctx, db); err != nil {
log.Errorf("Failed to apply migration %s: %s", migration.ID(), err)
if err := migration.Down(ctx, db); err != nil {
log.Errorf("Failed to rollback migration %s: %s\n", migration.ID(), err)
}
return err
}
// Record the migration
record := Migration{
ID: migration.ID(),
Description: migration.Description(),
AppliedAt: time.Now().UTC(),
}
if err := db.Create(&record).Error; err != nil {
log.Errorf("Failed to record migration %s: %s\n", migration.ID(), err)
return err
}
successfulCount++
}
if len(registry) == 0 {
log.Info("Migratons: No pending migrations")
} else {
var failedCount = len(registry) - successfulCount - skippedCount
log.Infof("Migrations: %d successful, %d failed, %d skipped, %d total in registry", successfulCount, failedCount, skippedCount, len(registry))
}
return nil
}