focalboard/server/services/store/sqlstore/migrate.go

118 lines
3 KiB
Go
Raw Normal View History

2020-10-16 22:27:16 +02:00
package sqlstore
import (
2021-04-17 09:06:57 +02:00
"bytes"
"errors"
2021-04-17 09:06:57 +02:00
"fmt"
"io"
"io/ioutil"
"os"
2021-04-17 09:06:57 +02:00
"text/template"
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"
2021-04-17 09:06:57 +02:00
"github.com/golang-migrate/migrate/v4/source"
2020-10-16 22:27:16 +02:00
_ "github.com/golang-migrate/migrate/v4/source/file"
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
2020-10-16 22:27:16 +02:00
_ "github.com/lib/pq"
2021-01-26 23:13:46 +01:00
pgmigrations "github.com/mattermost/focalboard/server/services/store/sqlstore/migrations/postgres"
"github.com/mattermost/focalboard/server/services/store/sqlstore/migrations/sqlite"
2020-10-16 22:27:16 +02:00
)
2021-04-17 09:06:57 +02:00
type PrefixedMigration struct {
*bindata.Bindata
prefix string
}
func init() {
source.Register("prefixed-migrations", &PrefixedMigration{})
}
func (pm *PrefixedMigration) ReadUp(version uint) (io.ReadCloser, string, error) {
r, identifier, err := pm.Bindata.ReadUp(version)
if err != nil {
return nil, "", err
}
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, "", err
}
tmpl, err := template.New("sql").Parse(string(data))
if err != nil {
return nil, "", err
}
buffer := bytes.NewBufferString("")
err = tmpl.Execute(buffer, map[string]string{"prefix": pm.prefix})
if err != nil {
return nil, "", err
}
2021-04-17 10:05:07 +02:00
return ioutil.NopCloser(bytes.NewReader(buffer.Bytes())), identifier, nil
2021-04-17 09:06:57 +02:00
}
func (pm *PrefixedMigration) ReadDown(version uint) (io.ReadCloser, string, error) {
r, identifier, err := pm.Bindata.ReadDown(version)
if err != nil {
return nil, "", err
}
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, "", err
}
tmpl, err := template.New("sql").Parse(string(data))
if err != nil {
return nil, "", err
}
buffer := bytes.NewBufferString("")
err = tmpl.Execute(buffer, map[string]string{"prefix": pm.prefix})
if err != nil {
return nil, "", err
}
2021-04-17 10:05:07 +02:00
return ioutil.NopCloser(bytes.NewReader(buffer.Bytes())), identifier, nil
2021-04-17 09:06:57 +02:00
}
2020-10-16 22:27:16 +02:00
func (s *SQLStore) Migrate() error {
var bresource *bindata.AssetSource
2020-10-16 22:27:16 +02:00
var driver database.Driver
var err error
2021-04-17 09:06:57 +02:00
migrationsTable := fmt.Sprintf("%sschema_migrations", s.tablePrefix)
2020-10-16 22:27:16 +02:00
if s.dbType == "sqlite3" {
2021-04-17 09:06:57 +02:00
driver, err = sqlite3.WithInstance(s.db, &sqlite3.Config{MigrationsTable: migrationsTable})
2020-10-18 02:07:35 +02:00
if err != nil {
return err
}
bresource = bindata.Resource(sqlite.AssetNames(), sqlite.Asset)
2020-10-16 22:27:16 +02:00
}
2020-10-16 22:27:16 +02:00
if s.dbType == "postgres" {
2021-04-17 09:06:57 +02:00
driver, err = postgres.WithInstance(s.db, &postgres.Config{MigrationsTable: migrationsTable})
2020-10-18 02:07:35 +02:00
if err != nil {
return err
}
bresource = bindata.Resource(pgmigrations.AssetNames(), pgmigrations.Asset)
}
d, err := bindata.WithInstance(bresource)
if err != nil {
return err
2020-10-16 22:27:16 +02:00
}
2021-04-17 09:06:57 +02:00
prefixedData := &PrefixedMigration{
Bindata: d.(*bindata.Bindata),
prefix: s.tablePrefix,
}
2021-04-17 09:06:57 +02:00
m, err := migrate.NewWithInstance("prefixed-migration", prefixedData, s.dbType, driver)
2020-10-16 22:27:16 +02:00
if err != nil {
return err
}
err = m.Up()
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-16 22:27:16 +02:00
return nil
}