[MM-41576] Revamp database schema version (#19586)

* revamp db version and add applied migrations endpoint

* replace old schema version with new

* add db version subcommand

* add to local api

* reflect review comments

* log errors

* remove setting the version from model.CurrentVersion

* fix a test
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-03-08 18:13:37 +03:00
коммит произвёл GitHub
родитель 05503440a6
Коммит 645fee3fe3
37 изменённых файлов: 379 добавлений и 46 удалений

Просмотреть файл

@@ -353,10 +353,10 @@ func (ss *SqlStore) DriverName() string {
return *ss.settings.DriverName
}
func (ss *SqlStore) GetCurrentSchemaVersion() string {
func (ss *SqlStore) getCurrentSchemaVersion() (string, error) {
var version string
_ = ss.GetMasterX().Get(&version, "SELECT Value FROM Systems WHERE Name='Version'")
return version
err := ss.GetMasterX().Get(&version, "SELECT Value FROM Systems WHERE Name='Version'")
return version, err
}
// GetDbVersion returns the version of the database being used.
@@ -948,6 +948,7 @@ func (ss *SqlStore) DropAllTables() {
FROM pg_class
WHERE relkind = 'r' -- only tables
AND relnamespace = 'public'::regnamespace
AND NOT relname = 'db_migrations'
);
END
$func$;`)
@@ -955,7 +956,9 @@ func (ss *SqlStore) DropAllTables() {
tables := []string{}
ss.masterX.Select(&tables, `show tables`)
for _, t := range tables {
ss.masterX.Exec(`TRUNCATE TABLE ` + t)
if t != "db_migrations" {
ss.masterX.Exec(`TRUNCATE TABLE ` + t)
}
}
}
}
@@ -1195,3 +1198,20 @@ func (ss *SqlStore) toReserveCase(str string) string {
return fmt.Sprintf("`%s`", strings.Title(str))
}
func (ss *SqlStore) GetDBSchemaVersion() (int, error) {
var version int
if err := ss.GetMasterX().Get(&version, "SELECT Version FROM db_migrations ORDER BY Version DESC LIMIT 1"); err != nil {
return 0, errors.Wrap(err, "unable to select from db_migrations")
}
return version, nil
}
func (ss *SqlStore) GetAppliedMigrations() ([]model.AppliedMigration, error) {
migrations := []model.AppliedMigration{}
if err := ss.GetMasterX().Select(&migrations, "SELECT Version, Name FROM db_migrations ORDER BY Version DESC"); err != nil {
return nil, errors.Wrap(err, "unable to select from db_migrations")
}
return migrations, nil
}