2021-11-23 09:45:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
const (
|
|
|
|
// This is the latest schema version for the purpose of tests.
|
2023-02-11 15:47:03 +00:00
|
|
|
LatestVersion = 8
|
2022-10-20 02:16:36 +00:00
|
|
|
)
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
func TestShouldObtainCorrectUpMigrations(t *testing.T) {
|
|
|
|
ver, err := latestMigrationVersion(providerSQLite)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
assert.Equal(t, LatestVersion, ver)
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
migrations, err := loadMigrations(providerSQLite, 0, ver)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, migrations, ver)
|
|
|
|
|
|
|
|
for i := 0; i < len(migrations); i++ {
|
|
|
|
assert.Equal(t, i+1, migrations[i].Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldObtainCorrectDownMigrations(t *testing.T) {
|
|
|
|
ver, err := latestMigrationVersion(providerSQLite)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
assert.Equal(t, LatestVersion, ver)
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
migrations, err := loadMigrations(providerSQLite, ver, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, migrations, ver)
|
|
|
|
|
|
|
|
for i := 0; i < len(migrations); i++ {
|
|
|
|
assert.Equal(t, ver-i, migrations[i].Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrationsShouldNotBeDuplicatedPostgres(t *testing.T) {
|
|
|
|
migrations, err := loadMigrations(providerPostgres, 0, SchemaLatest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(migrations))
|
|
|
|
|
|
|
|
previousUp := make([]int, len(migrations))
|
|
|
|
|
|
|
|
for i, migration := range migrations {
|
|
|
|
assert.True(t, migration.Up)
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
for _, v := range previousUp {
|
|
|
|
assert.NotEqual(t, v, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousUp = append(previousUp, migration.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
migrations, err = loadMigrations(providerPostgres, SchemaLatest, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(migrations))
|
|
|
|
|
|
|
|
previousDown := make([]int, len(migrations))
|
|
|
|
|
|
|
|
for i, migration := range migrations {
|
|
|
|
assert.False(t, migration.Up)
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
for _, v := range previousDown {
|
|
|
|
assert.NotEqual(t, v, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousDown = append(previousDown, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrationsShouldNotBeDuplicatedMySQL(t *testing.T) {
|
|
|
|
migrations, err := loadMigrations(providerMySQL, 0, SchemaLatest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(migrations))
|
|
|
|
|
|
|
|
previousUp := make([]int, len(migrations))
|
|
|
|
|
|
|
|
for i, migration := range migrations {
|
|
|
|
assert.True(t, migration.Up)
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
for _, v := range previousUp {
|
|
|
|
assert.NotEqual(t, v, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousUp = append(previousUp, migration.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
migrations, err = loadMigrations(providerMySQL, SchemaLatest, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(migrations))
|
|
|
|
|
|
|
|
previousDown := make([]int, len(migrations))
|
|
|
|
|
|
|
|
for i, migration := range migrations {
|
|
|
|
assert.False(t, migration.Up)
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
for _, v := range previousDown {
|
|
|
|
assert.NotEqual(t, v, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousDown = append(previousDown, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrationsShouldNotBeDuplicatedSQLite(t *testing.T) {
|
|
|
|
migrations, err := loadMigrations(providerSQLite, 0, SchemaLatest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(migrations))
|
|
|
|
|
|
|
|
previousUp := make([]int, len(migrations))
|
|
|
|
|
|
|
|
for i, migration := range migrations {
|
|
|
|
assert.True(t, migration.Up)
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
for _, v := range previousUp {
|
|
|
|
assert.NotEqual(t, v, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousUp = append(previousUp, migration.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
migrations, err = loadMigrations(providerSQLite, SchemaLatest, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, 0, len(migrations))
|
|
|
|
|
|
|
|
previousDown := make([]int, len(migrations))
|
|
|
|
|
|
|
|
for i, migration := range migrations {
|
|
|
|
assert.False(t, migration.Up)
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
for _, v := range previousDown {
|
|
|
|
assert.NotEqual(t, v, migration.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousDown = append(previousDown, migration.Version)
|
|
|
|
}
|
|
|
|
}
|