Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

UB-1757 Fix for error: concurrent map writes #285

Merged
merged 1 commit into from
Dec 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@ package database

import (
"fmt"
"sync"
"github.com/IBM/ubiquity/utils/logs"
)

var migrations = make(map[string]interface{})
var migrations = new(sync.Map)

func RegisterMigration(obj interface{}) {
defer logs.GetLogger().Trace(logs.DEBUG)()

migrations[fmt.Sprintf("%v", obj)] = obj
migrations.Store(fmt.Sprintf("%v", obj), obj)
}

func UnregisterAllMigrations() {
defer logs.GetLogger().Trace(logs.DEBUG)()

migrations = make(map[string]interface{})
migrations = new(sync.Map)
}

func doMigrations(connection Connection) error {
defer logs.GetLogger().Trace(logs.DEBUG)()

logger := logs.GetLogger()
for k, v := range migrations {

migrations.Range(func(k, v interface{}) bool {
logger.Info("migrating", logs.Args{{"migration", k}})
if err := connection.GetDb().AutoMigrate(v).Error; err != nil {
logger.ErrorRet(err, "failed")
}
delete(migrations, k)
}
migrations.Delete(k)
return true
})

return nil
}