mirror of
https://github.com/mattermost/focalboard.git
synced 2024-12-27 13:48:52 +02:00
f1b8d88d6b
* Improving mattermost auth implementation * Making mattermost-auth based on shared database access * Reverting unneeded changes in the config.json file * Fixing tiny problems * Removing the need of using the mattermost session token * Fixing some bugs and allowing to not-bind the server to any port * Small fix to correctly get the templates * Adding the mattermost-plugin code inside focalboard repo * Adding a not working code part of the cluster websocket communication * Updating the mattermost version * Adding the cluster messages for the websockets * Updating to the new node version * Making it compatible with S3 * Addressing some tiny problems * Fixing server tests * Adds support for MySQL migrations and initialization Co-authored-by: Miguel de la Cruz <miguel@mcrx.me>
151 lines
3.7 KiB
Go
151 lines
3.7 KiB
Go
package sqlstore
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"text/template"
|
|
|
|
mysqldriver "github.com/go-sql-driver/mysql"
|
|
"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)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
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 {
|
|
db, err := s.getMySQLMigrationConnection()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
driver, err = mysql.WithInstance(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
|
|
}
|