focalboard/server/services/store/sqlstore/migrate.go
Jesús Espino 2d261fde59
Adding mysql support to the database (#301)
* Adding mysql support

* Fixing other test cases

* Tests passing on mysql

* Fix the row number generation

* Adding blocks_history table

* merging the migrations in one single set of files

* Passing mysql tests

* Fixing default encoding on mysql

* Simplifying things

* Removing from the blocks table all deleted blocks

* Better indentation

* Moving db types to constants to make it less error prone

* reducing duplicated code

* Removing log line

* Now sql tests are running properly in mysql, sqlite and postgres
2021-04-22 22:53:01 +02:00

119 lines
3 KiB
Go

package sqlstore
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"text/template"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"github.com/golang-migrate/migrate/v4/database/mysql"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source"
_ "github.com/golang-migrate/migrate/v4/source/file"
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
_ "github.com/lib/pq"
"github.com/mattermost/focalboard/server/services/store/sqlstore/migrations"
)
type PrefixedMigration struct {
*bindata.Bindata
prefix string
postgres bool
sqlite bool
mysql bool
}
func init() {
source.Register("prefixed-migrations", &PrefixedMigration{})
}
func (pm *PrefixedMigration) executeTemplate(r io.ReadCloser, identifier string) (io.ReadCloser, string, error) {
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]interface{}{"prefix": pm.prefix, "postgres": pm.postgres, "sqlite": pm.sqlite, "mysql": pm.mysql})
if err != nil {
return nil, "", err
}
return ioutil.NopCloser(bytes.NewReader(buffer.Bytes())), identifier, nil
}
func (pm *PrefixedMigration) ReadUp(version uint) (io.ReadCloser, string, error) {
r, identifier, err := pm.Bindata.ReadUp(version)
if err != nil {
return nil, "", err
}
return pm.executeTemplate(r, identifier)
}
func (pm *PrefixedMigration) ReadDown(version uint) (io.ReadCloser, string, error) {
r, identifier, err := pm.Bindata.ReadDown(version)
if err != nil {
return nil, "", err
}
return pm.executeTemplate(r, identifier)
}
func (s *SQLStore) Migrate() error {
var driver database.Driver
var err error
migrationsTable := fmt.Sprintf("%sschema_migrations", s.tablePrefix)
if s.dbType == sqliteDBType {
driver, err = sqlite3.WithInstance(s.db, &sqlite3.Config{MigrationsTable: migrationsTable})
if err != nil {
return err
}
}
if s.dbType == postgresDBType {
driver, err = postgres.WithInstance(s.db, &postgres.Config{MigrationsTable: migrationsTable})
if err != nil {
return err
}
}
if s.dbType == mysqlDBType {
driver, err = mysql.WithInstance(s.db, &mysql.Config{MigrationsTable: migrationsTable})
if err != nil {
return err
}
}
bresource := bindata.Resource(migrations.AssetNames(), migrations.Asset)
d, err := bindata.WithInstance(bresource)
if err != nil {
return err
}
prefixedData := &PrefixedMigration{
Bindata: d.(*bindata.Bindata),
prefix: s.tablePrefix,
postgres: s.dbType == postgresDBType,
sqlite: s.dbType == sqliteDBType,
mysql: s.dbType == mysqlDBType,
}
m, err := migrate.NewWithInstance("prefixed-migration", prefixedData, s.dbType, driver)
if err != nil {
return err
}
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}