focalboard/server/services/store/sqlstore/sqlstore.go
Harshil Sharma 08db4fed61
Workspace switcher (#1052)
* API WIP

* WIP

* Finished changes

* Fixed colors:

* Don't enforce charset adn collation in migration, pick from database DSN

* Added MySQL query

* Updated mocks

* Added tests

* Lint fixes

* Fixed typo and removed unsed style

* Checked in a snapshot

* Updated snapshot

* Updated Cypress test

* Updated Cypress test

* Updated Cypress test

* Fixed review comments

* Fixed tests

* Added default collation for MySQL

* Added documentation for ensuring correct database collation

* Updated migrations

* Fixed a bug with collation

* Fixed lint errors

* Used correct collation

* debugging

* Updating css

* Minor UI changes

* USe inbuilt default collation

* Used only charset for mysql

* Fixed linter issue:

* Added migration for matching collation

* Reverted local config changes

* Reverted local config changes

* Handled the case of personal server running on MySQL

* WIP

* Now running collation matching migration onlyt for plugins

* Minor optimization

* Multiple review fixes

* Added group by clause to primary query

* Supported for subpacth

Co-authored-by: Asaad Mahmood <asaadmahmood@users.noreply.github.com>
2021-09-08 10:22:03 +05:30

87 lines
2.0 KiB
Go

package sqlstore
import (
"database/sql"
sq "github.com/Masterminds/squirrel"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
const (
mysqlDBType = "mysql"
sqliteDBType = "sqlite3"
postgresDBType = "postgres"
)
// SQLStore is a SQL database.
type SQLStore struct {
db *sql.DB
dbType string
tablePrefix string
connectionString string
isPlugin bool
logger *mlog.Logger
}
// New creates a new SQL implementation of the store.
func New(dbType, connectionString, tablePrefix string, logger *mlog.Logger, db *sql.DB, isPlugin bool) (*SQLStore, error) {
logger.Info("connectDatabase", mlog.String("dbType", dbType), mlog.String("connStr", connectionString))
store := &SQLStore{
// TODO: add replica DB support too.
db: db,
dbType: dbType,
tablePrefix: tablePrefix,
connectionString: connectionString,
logger: logger,
isPlugin: isPlugin,
}
err := store.Migrate()
if err != nil {
logger.Error(`Table creation / migration failed`, mlog.Err(err))
return nil, err
}
err = store.InitializeTemplates()
if err != nil {
logger.Error(`InitializeTemplates failed`, mlog.Err(err))
return nil, err
}
return store, nil
}
// Shutdown close the connection with the store.
func (s *SQLStore) Shutdown() error {
return s.db.Close()
}
// DBHandle returns the raw sql.DB handle.
// It is used by the mattermostauthlayer to run their own
// raw SQL queries.
func (s *SQLStore) DBHandle() *sql.DB {
return s.db
}
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 { //nolint:unparam
if s.dbType == mysqlDBType {
return "`" + fieldName + "`"
}
if s.dbType == postgresDBType || s.dbType == sqliteDBType {
return "\"" + fieldName + "\""
}
return fieldName
}