mirror of
https://github.com/status-im/migrate.git
synced 2025-02-23 08:18:07 +00:00
There is lock conflict on parallel migrations in different postgres schemas. To avoid this conflicts function GenerateAdvisoryLockId added variadic params to change lock id with schema name. Schema name taked with postgres CURRENT_SCHEMA function. Null byte used as separator between database and schema name, because any other symbol may be used in both of it. Closes #118
36 lines
892 B
Go
36 lines
892 B
Go
package database
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestGenerateAdvisoryLockId(t *testing.T) {
|
|
testcases := []struct {
|
|
dbname string
|
|
schema string
|
|
expectedID string // empty string signifies that an error is expected
|
|
}{
|
|
{dbname: "database_name", expectedID: "1764327054"},
|
|
{dbname: "database_name", schema: "schema_name_1", expectedID: "3244152297"},
|
|
{dbname: "database_name", schema: "schema_name_2", expectedID: "810103531"},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.dbname, func(t *testing.T) {
|
|
names := []string{}
|
|
if len(tc.schema) > 0 {
|
|
names = append(names, tc.schema)
|
|
}
|
|
if id, err := GenerateAdvisoryLockId(tc.dbname, names...); err == nil {
|
|
if id != tc.expectedID {
|
|
t.Error("Generated incorrect ID:", id, "!=", tc.expectedID)
|
|
}
|
|
} else {
|
|
if tc.expectedID != "" {
|
|
t.Error("Got unexpected error:", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|