2020-10-16 22:27:16 +02:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2021-04-17 09:06:57 +02:00
|
|
|
"bytes"
|
2021-05-24 19:06:11 +02:00
|
|
|
"database/sql"
|
2020-10-22 13:34:42 +02:00
|
|
|
"errors"
|
2021-04-17 09:06:57 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-11-19 17:51:39 +01:00
|
|
|
"os"
|
2021-04-17 09:06:57 +02:00
|
|
|
"text/template"
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
mysqldriver "github.com/go-sql-driver/mysql"
|
2020-10-16 22:27:16 +02:00
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database"
|
2021-04-22 22:53:01 +02:00
|
|
|
"github.com/golang-migrate/migrate/v4/database/mysql"
|
2020-10-16 22:27:16 +02:00
|
|
|
"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"
|
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"
|
2021-04-20 11:27:20 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/store/sqlstore/migrations"
|
2020-10-16 22:27:16 +02:00
|
|
|
)
|
|
|
|
|
2021-04-17 09:06:57 +02:00
|
|
|
type PrefixedMigration struct {
|
|
|
|
*bindata.Bindata
|
2021-04-20 11:27:20 +02:00
|
|
|
prefix string
|
|
|
|
postgres bool
|
|
|
|
sqlite bool
|
2021-04-22 22:53:01 +02:00
|
|
|
mysql bool
|
2021-04-17 09:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register("prefixed-migrations", &PrefixedMigration{})
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
func (pm *PrefixedMigration) executeTemplate(r io.ReadCloser, identifier string) (io.ReadCloser, string, error) {
|
2021-04-17 09:06:57 +02:00
|
|
|
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("")
|
2021-04-22 22:53:01 +02:00
|
|
|
err = tmpl.Execute(buffer, map[string]interface{}{"prefix": pm.prefix, "postgres": pm.postgres, "sqlite": pm.sqlite, "mysql": pm.mysql})
|
2021-04-17 09:06:57 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
func (pm *PrefixedMigration) ReadUp(version uint) (io.ReadCloser, string, error) {
|
|
|
|
r, identifier, err := pm.Bindata.ReadUp(version)
|
2021-04-17 09:06:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
return pm.executeTemplate(r, identifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *PrefixedMigration) ReadDown(version uint) (io.ReadCloser, string, error) {
|
|
|
|
r, identifier, err := pm.Bindata.ReadDown(version)
|
2021-04-17 09:06:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2021-04-22 22:53:01 +02:00
|
|
|
return pm.executeTemplate(r, identifier)
|
2021-04-17 09:06:57 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
// migrations in MySQL need to run with the multiStatements flag
|
|
|
|
// enabled, so this method creates a new connection ensuring that it's
|
|
|
|
// enabled
|
|
|
|
func (s *SQLStore) getMySQLMigrationConnection() (*sql.DB, error) {
|
|
|
|
config, err := mysqldriver.ParseDSN(s.connectionString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Params["multiStatements"] = "true"
|
|
|
|
connectionString := config.FormatDSN()
|
|
|
|
|
|
|
|
db, err := sql.Open(s.dbType, connectionString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:27:16 +02:00
|
|
|
func (s *SQLStore) Migrate() error {
|
|
|
|
var driver database.Driver
|
|
|
|
var err error
|
2021-04-17 09:06:57 +02:00
|
|
|
migrationsTable := fmt.Sprintf("%sschema_migrations", s.tablePrefix)
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
if s.dbType == sqliteDBType {
|
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
|
|
|
|
}
|
2020-10-16 22:27:16 +02:00
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
if s.dbType == postgresDBType {
|
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
|
|
|
|
}
|
2020-10-18 01:09:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 22:53:01 +02:00
|
|
|
if s.dbType == mysqlDBType {
|
2021-05-24 19:06:11 +02:00
|
|
|
db, err := s.getMySQLMigrationConnection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
driver, err = mysql.WithInstance(db, &mysql.Config{MigrationsTable: migrationsTable})
|
2021-04-22 22:53:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 11:27:20 +02:00
|
|
|
bresource := bindata.Resource(migrations.AssetNames(), migrations.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
|
|
|
}
|
2021-04-17 09:06:57 +02:00
|
|
|
prefixedData := &PrefixedMigration{
|
2021-04-20 11:27:20 +02:00
|
|
|
Bindata: d.(*bindata.Bindata),
|
|
|
|
prefix: s.tablePrefix,
|
2021-04-22 22:53:01 +02:00
|
|
|
postgres: s.dbType == postgresDBType,
|
|
|
|
sqlite: s.dbType == sqliteDBType,
|
|
|
|
mysql: s.dbType == mysqlDBType,
|
2021-04-17 09:06:57 +02:00
|
|
|
}
|
2020-10-18 01:09:12 +02:00
|
|
|
|
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()
|
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
|
|
|
|
}
|