2021-11-23 09:45:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-04-08 01:36:34 +00:00
|
|
|
"io/fs"
|
2021-11-23 09:45:38 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-12-01 12:11:29 +00:00
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2021-11-23 09:45:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed migrations/*
|
|
|
|
var migrationsFS embed.FS
|
|
|
|
|
|
|
|
func latestMigrationVersion(providerName string) (version int, err error) {
|
2023-04-08 01:36:34 +00:00
|
|
|
var (
|
|
|
|
entries []fs.DirEntry
|
|
|
|
migration model.SchemaMigration
|
|
|
|
)
|
|
|
|
|
|
|
|
if entries, err = migrationsFS.ReadDir("migrations"); err != nil {
|
2021-11-23 09:45:38 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if migration, err = scanMigration(entry.Name()); err != nil {
|
2021-11-23 09:45:38 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if migration.Provider != providerName && migration.Provider != providerAll {
|
2021-11-23 09:45:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if !migration.Up {
|
2021-11-23 09:45:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if migration.Version > version {
|
|
|
|
version = migration.Version
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadMigrations scans the migrations fs and loads the appropriate migrations for a given providerName, prior and
|
|
|
|
// target versions. If the target version is -1 this indicates the latest version. If the target version is 0
|
|
|
|
// this indicates the database zero state.
|
2022-03-06 05:47:40 +00:00
|
|
|
func loadMigrations(providerName string, prior, target int) (migrations []model.SchemaMigration, err error) {
|
2023-04-08 01:36:34 +00:00
|
|
|
if prior == target {
|
2021-11-25 01:56:58 +00:00
|
|
|
return nil, ErrMigrateCurrentVersionSameAsTarget
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
var (
|
|
|
|
migrationsAll []model.SchemaMigration
|
|
|
|
migration model.SchemaMigration
|
|
|
|
entries []fs.DirEntry
|
|
|
|
)
|
|
|
|
|
|
|
|
if entries, err = migrationsFS.ReadDir("migrations"); err != nil {
|
2021-11-23 09:45:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
up := prior < target
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if migration, err = scanMigration(entry.Name()); err != nil {
|
2021-11-23 09:45:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if skipMigration(providerName, up, target, prior, &migration) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if migration.Provider == providerAll {
|
|
|
|
migrationsAll = append(migrationsAll, migration)
|
|
|
|
} else {
|
|
|
|
migrations = append(migrations, migration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add "all" migrations for versions that don't exist.
|
|
|
|
for _, am := range migrationsAll {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, m := range migrations {
|
|
|
|
if m.Version == am.Version {
|
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
migrations = append(migrations, am)
|
|
|
|
}
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if up {
|
|
|
|
sort.Slice(migrations, func(i, j int) bool {
|
|
|
|
return migrations[i].Version < migrations[j].Version
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
sort.Slice(migrations, func(i, j int) bool {
|
|
|
|
return migrations[i].Version > migrations[j].Version
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrations, nil
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
func skipMigration(providerName string, up bool, target, prior int, migration *model.SchemaMigration) (skip bool) {
|
2021-11-23 09:45:38 +00:00
|
|
|
if migration.Provider != providerAll && migration.Provider != providerName {
|
|
|
|
// Skip if migration.Provider is not a match.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if up {
|
|
|
|
if !migration.Up {
|
|
|
|
// Skip if we wanted an Up migration but it isn't an Up migration.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
if migration.Version > target || migration.Version <= prior {
|
2021-11-23 09:45:38 +00:00
|
|
|
// Skip if the migration version is greater than the target or less than or equal to the previous version.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if migration.Up {
|
|
|
|
// Skip if we didn't want an Up migration but it is an Up migration.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if migration.Version <= target || migration.Version > prior {
|
|
|
|
// Skip the migration if we want to go down and the migration version is less than or equal to the target
|
|
|
|
// or greater than the previous version.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
func scanMigration(m string) (migration model.SchemaMigration, err error) {
|
2023-03-06 03:58:50 +00:00
|
|
|
if !reMigration.MatchString(m) {
|
2022-03-06 05:47:40 +00:00
|
|
|
return model.SchemaMigration{}, errors.New("invalid migration: could not parse the format")
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 03:58:50 +00:00
|
|
|
result := reMigration.FindStringSubmatch(m)
|
|
|
|
|
2022-03-06 05:47:40 +00:00
|
|
|
migration = model.SchemaMigration{
|
2023-03-06 03:58:50 +00:00
|
|
|
Name: strings.ReplaceAll(result[reMigration.SubexpIndex("Name")], "_", " "),
|
|
|
|
Provider: result[reMigration.SubexpIndex("Provider")],
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 01:36:34 +00:00
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if data, err = migrationsFS.ReadFile(fmt.Sprintf("migrations/%s", m)); err != nil {
|
2022-03-06 05:47:40 +00:00
|
|
|
return model.SchemaMigration{}, err
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
migration.Query = string(data)
|
|
|
|
|
2023-03-06 03:58:50 +00:00
|
|
|
switch direction := result[reMigration.SubexpIndex("Direction")]; direction {
|
2021-11-23 09:45:38 +00:00
|
|
|
case "up":
|
|
|
|
migration.Up = true
|
|
|
|
case "down":
|
|
|
|
migration.Up = false
|
|
|
|
default:
|
2023-03-06 03:58:50 +00:00
|
|
|
return model.SchemaMigration{}, fmt.Errorf("invalid migration: value in Direction group '%s' must be up or down", direction)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 03:58:50 +00:00
|
|
|
migration.Version, _ = strconv.Atoi(result[reMigration.SubexpIndex("Version")])
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
switch migration.Provider {
|
|
|
|
case providerAll, providerSQLite, providerMySQL, providerPostgres:
|
|
|
|
break
|
|
|
|
default:
|
2023-03-06 03:58:50 +00:00
|
|
|
return model.SchemaMigration{}, fmt.Errorf("invalid migration: value in Provider group '%s' must be all, sqlite, postgres, or mysql", migration.Provider)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return migration, nil
|
|
|
|
}
|