2020-10-16 22:27:16 +02:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2020-10-22 13:34:42 +02:00
|
|
|
"errors"
|
2020-11-19 17:51:39 +01:00
|
|
|
"os"
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 22:27:16 +02:00
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/postgres"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
|
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
2020-10-18 01:09:12 +02:00
|
|
|
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
|
2020-10-16 22:27:16 +02:00
|
|
|
_ "github.com/lib/pq"
|
2020-10-18 01:09:12 +02:00
|
|
|
pgmigrations "github.com/mattermost/mattermost-octo-tasks/server/services/store/sqlstore/migrations/postgres"
|
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/services/store/sqlstore/migrations/sqlite"
|
2020-10-16 22:27:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *SQLStore) Migrate() error {
|
2020-10-22 13:34:42 +02:00
|
|
|
var bresource *bindata.AssetSource
|
2020-10-16 22:27:16 +02:00
|
|
|
var driver database.Driver
|
|
|
|
var err error
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 22:27:16 +02:00
|
|
|
if s.dbType == "sqlite3" {
|
|
|
|
driver, err = sqlite3.WithInstance(s.db, &sqlite3.Config{})
|
2020-10-18 02:07:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
bresource = bindata.Resource(sqlite.AssetNames(), sqlite.Asset)
|
2020-10-16 22:27:16 +02:00
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 22:27:16 +02:00
|
|
|
if s.dbType == "postgres" {
|
|
|
|
driver, err = postgres.WithInstance(s.db, &postgres.Config{})
|
2020-10-18 02:07:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
bresource = bindata.Resource(pgmigrations.AssetNames(), pgmigrations.Asset)
|
2020-10-18 01:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d, err := bindata.WithInstance(bresource)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-16 22:27:16 +02:00
|
|
|
}
|
2020-10-18 01:09:12 +02:00
|
|
|
|
|
|
|
m, err := migrate.NewWithInstance("go-bindata", d, s.dbType, driver)
|
2020-10-16 22:27:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.Up()
|
2020-11-19 17:51:39 +01:00
|
|
|
if err != nil && !errors.Is(err, migrate.ErrNoChange) && !errors.Is(err, os.ErrNotExist) {
|
2020-10-16 22:27:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 22:27:16 +02:00
|
|
|
return nil
|
|
|
|
}
|