2017-02-28 23:10:56 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash/crc32"
|
2018-11-05 12:03:54 +00:00
|
|
|
"strings"
|
2017-02-28 23:10:56 +00:00
|
|
|
)
|
|
|
|
|
2019-04-26 22:47:16 +00:00
|
|
|
const advisoryLockIDSalt uint = 1486364155
|
2017-02-28 23:10:56 +00:00
|
|
|
|
2018-07-25 00:22:26 +00:00
|
|
|
// GenerateAdvisoryLockId inspired by rails migrations, see https://goo.gl/8o9bCT
|
2019-04-26 22:47:16 +00:00
|
|
|
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 08:27:28 +00:00
|
|
|
}
|
2018-11-05 12:03:54 +00:00
|
|
|
sum := crc32.ChecksumIEEE([]byte(databaseName))
|
2019-04-26 22:47:16 +00:00
|
|
|
sum = sum * uint32(advisoryLockIDSalt)
|
|
|
|
return fmt.Sprint(sum), nil
|
2017-02-28 23:10:56 +00:00
|
|
|
}
|