focalboard/server/services/store/sqlstore/sqlstore.go
Jesús Espino f1b8d88d6b
Changing mattermost-auth method to work based on shared database access (#335)
* 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>
2021-05-24 19:06:11 +02:00

89 lines
1.7 KiB
Go

package sqlstore
import (
"database/sql"
"log"
sq "github.com/Masterminds/squirrel"
)
const (
mysqlDBType = "mysql"
sqliteDBType = "sqlite3"
postgresDBType = "postgres"
)
// SQLStore is a SQL database.
type SQLStore struct {
db *sql.DB
dbType string
tablePrefix string
connectionString string
}
// New creates a new SQL implementation of the store.
func New(dbType, connectionString string, tablePrefix string) (*SQLStore, error) {
log.Println("connectDatabase", dbType, connectionString)
var err error
db, err := sql.Open(dbType, connectionString)
if err != nil {
log.Print("connectDatabase: ", err)
return nil, err
}
err = db.Ping()
if err != nil {
log.Printf(`Database Ping failed: %v`, err)
return nil, err
}
store := &SQLStore{
db: db,
dbType: dbType,
tablePrefix: tablePrefix,
connectionString: connectionString,
}
err = store.Migrate()
if err != nil {
log.Printf(`Table creation / migration failed: %v`, err)
return nil, err
}
err = store.InitializeTemplates()
if err != nil {
log.Printf(`InitializeTemplates failed: %v`, err)
return nil, err
}
return store, nil
}
// Shutdown close the connection with the store.
func (s *SQLStore) Shutdown() error {
return s.db.Close()
}
func (s *SQLStore) getQueryBuilder() sq.StatementBuilderType {
builder := sq.StatementBuilder
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
builder = builder.PlaceholderFormat(sq.Dollar)
}
return builder.RunWith(s.db)
}
func (s *SQLStore) escapeField(fieldName string) string {
if s.dbType == mysqlDBType {
return "`" + fieldName + "`"
}
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
return "\"" + fieldName + "\""
}
return fieldName
}