migrate/database/util.go

20 lines
531 B
Go
Raw Normal View History

package database
import (
"fmt"
"hash/crc32"
2018-11-05 12:03:54 +00:00
"strings"
)
const advisoryLockIDSalt uint = 1486364155
// GenerateAdvisoryLockId inspired by rails migrations, see https://goo.gl/8o9bCT
func GenerateAdvisoryLockId(databaseName string, additionalNames ...string) (string, error) { // nolint: golint
2018-11-05 12:03:54 +00:00
if len(additionalNames) > 0 {
databaseName = strings.Join(append(additionalNames, databaseName), "\x00")
}
2018-11-05 12:03:54 +00:00
sum := crc32.ChecksumIEEE([]byte(databaseName))
sum = sum * uint32(advisoryLockIDSalt)
return fmt.Sprint(sum), nil
}