2019-02-15 09:05:29 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2019-11-29 06:59:40 -05:00
|
|
|
// See LICENSE.txt for license information.
|
2019-02-15 09:05:29 -05:00
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2022-03-08 07:07:37 -05:00
|
|
|
"context"
|
2022-04-14 14:43:14 -04:00
|
|
|
"crypto/sha256"
|
2019-02-15 09:05:29 -05:00
|
|
|
"database/sql"
|
2022-03-11 06:14:50 -05:00
|
|
|
"embed"
|
2022-04-14 14:43:14 -04:00
|
|
|
"encoding/hex"
|
2020-10-29 18:54:39 -04:00
|
|
|
"encoding/json"
|
2022-03-08 07:07:37 -05:00
|
|
|
"fmt"
|
|
|
|
|
"path/filepath"
|
2019-02-15 09:05:29 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
|
|
// Load the Postgres driver
|
|
|
|
|
_ "github.com/lib/pq"
|
2021-01-07 12:12:43 -05:00
|
|
|
|
2022-03-08 07:07:37 -05:00
|
|
|
"github.com/mattermost/morph"
|
|
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
2022-03-08 07:07:37 -05:00
|
|
|
"github.com/mattermost/morph/drivers"
|
|
|
|
|
ps "github.com/mattermost/morph/drivers/postgres"
|
2022-04-04 07:03:39 -04:00
|
|
|
mbindata "github.com/mattermost/morph/sources/embedded"
|
2019-02-15 09:05:29 -05:00
|
|
|
)
|
|
|
|
|
|
2022-03-11 06:14:50 -05:00
|
|
|
//go:embed migrations
|
|
|
|
|
var assets embed.FS
|
|
|
|
|
|
2022-03-08 07:07:37 -05:00
|
|
|
// We use the something different from the default migration table name of morph
|
|
|
|
|
const migrationsTableName = "db_config_migrations"
|
|
|
|
|
|
|
|
|
|
// The timeout value for each migration file to run.
|
|
|
|
|
const migrationsTimeoutInSeconds = 100000
|
|
|
|
|
|
2019-02-15 09:05:29 -05:00
|
|
|
// DatabaseStore is a config store backed by a database.
|
2020-10-29 18:54:39 -04:00
|
|
|
// Not to be used directly. Only to be used as a backing store for config.Store
|
2019-02-15 09:05:29 -05:00
|
|
|
type DatabaseStore struct {
|
|
|
|
|
originalDsn string
|
|
|
|
|
driverName string
|
|
|
|
|
dataSourceName string
|
|
|
|
|
db *sqlx.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewDatabaseStore creates a new instance of a config store backed by the given database.
|
|
|
|
|
func NewDatabaseStore(dsn string) (ds *DatabaseStore, err error) {
|
|
|
|
|
driverName, dataSourceName, err := parseDSN(dsn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "invalid DSN")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := sqlx.Open(driverName, dataSourceName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(err, "failed to connect to %s database", driverName)
|
|
|
|
|
}
|
2020-12-08 11:44:30 -05:00
|
|
|
// Set conservative connection configuration for configuration database.
|
|
|
|
|
db.SetMaxIdleConns(0)
|
|
|
|
|
db.SetMaxOpenConns(2)
|
2019-02-15 09:05:29 -05:00
|
|
|
|
2020-12-03 10:09:31 -05:00
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
db.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2019-02-15 09:05:29 -05:00
|
|
|
ds = &DatabaseStore{
|
|
|
|
|
driverName: driverName,
|
|
|
|
|
originalDsn: dsn,
|
|
|
|
|
dataSourceName: dataSourceName,
|
|
|
|
|
db: db,
|
|
|
|
|
}
|
2022-03-08 07:07:37 -05:00
|
|
|
if err = ds.initializeConfigurationsTable(); err != nil {
|
2020-12-03 10:09:31 -05:00
|
|
|
err = errors.Wrap(err, "failed to initialize")
|
|
|
|
|
return nil, err
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ds, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initializeConfigurationsTable ensures the requisite tables in place to form the backing store.
|
2022-03-08 07:07:37 -05:00
|
|
|
func (ds *DatabaseStore) initializeConfigurationsTable() error {
|
2022-03-11 06:14:50 -05:00
|
|
|
assetsList, err := assets.ReadDir(filepath.Join("migrations", ds.driverName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assetNamesForDriver := make([]string, len(assetsList))
|
|
|
|
|
for i, entry := range assetsList {
|
|
|
|
|
assetNamesForDriver[i] = entry.Name()
|
2019-12-20 18:31:03 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-08 07:07:37 -05:00
|
|
|
src, err := mbindata.WithInstance(&mbindata.AssetSource{
|
|
|
|
|
Names: assetNamesForDriver,
|
|
|
|
|
AssetFunc: func(name string) ([]byte, error) {
|
2022-03-11 06:14:50 -05:00
|
|
|
return assets.ReadFile(filepath.Join("migrations", ds.driverName, name))
|
2022-03-08 07:07:37 -05:00
|
|
|
},
|
|
|
|
|
})
|
2019-02-15 09:05:29 -05:00
|
|
|
if err != nil {
|
2022-03-08 07:07:37 -05:00
|
|
|
return err
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-08 07:07:37 -05:00
|
|
|
var driver drivers.Driver
|
|
|
|
|
switch ds.driverName {
|
|
|
|
|
case model.DatabaseDriverPostgres:
|
2022-11-17 11:36:08 -05:00
|
|
|
driver, err = ps.WithInstance(ds.db.DB)
|
2022-03-08 07:07:37 -05:00
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("unsupported database type %s for migration", ds.driverName)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := []morph.EngineOption{
|
|
|
|
|
morph.WithLock("mm-config-lock-key"),
|
2022-11-17 11:36:08 -05:00
|
|
|
morph.SetMigrationTableName(migrationsTableName),
|
|
|
|
|
morph.SetStatementTimeoutInSeconds(migrationsTimeoutInSeconds),
|
2022-03-08 07:07:37 -05:00
|
|
|
}
|
|
|
|
|
engine, err := morph.New(context.Background(), driver, src, opts...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2019-09-27 08:10:38 -04:00
|
|
|
}
|
2022-03-08 07:07:37 -05:00
|
|
|
defer engine.Close()
|
2019-09-27 08:10:38 -04:00
|
|
|
|
2022-03-08 07:07:37 -05:00
|
|
|
return engine.ApplyAll()
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
Remove vestigial MySQL support (#34865)
* Remove legacy quoteColumnName() utility
Since Mattermost only supports PostgreSQL, the quoteColumnName() helper
that was designed to handle database-specific column quoting is no longer
needed. The function was a no-op that simply returned the column name
unchanged.
Remove the function from utils.go and update status_store.go to use
the "Manual" column name directly.
* Remove legacy driver checks from store.go
Since Mattermost only supports PostgreSQL, remove conditional checks
for different database drivers:
- Simplify specialSearchChars() to always return PostgreSQL-compatible chars
- Remove driver check from computeBinaryParam()
- Remove driver check from computeDefaultTextSearchConfig()
- Simplify GetDbVersion() to use PostgreSQL syntax directly
- Remove switch statement from ensureMinimumDBVersion()
- Remove unused driver parameter from versionString()
* Remove MySQL alternatives for batch delete operations
Since Mattermost only supports PostgreSQL, remove the MySQL-specific
DELETE...LIMIT syntax and keep only the PostgreSQL array-based approach:
- reaction_store.go: Use PostgreSQL array syntax for PermanentDeleteBatch
- file_info_store.go: Use PostgreSQL array syntax for PermanentDeleteBatch
- preference_store.go: Use PostgreSQL tuple IN subquery for DeleteInvalidVisibleDmsGms
* Remove MySQL alternatives for UPDATE...FROM syntax
Since Mattermost only supports PostgreSQL, remove the MySQL-specific
UPDATE syntax that joins tables differently:
- thread_store.go: Use PostgreSQL UPDATE...FROM syntax in
MarkAllAsReadByChannels and MarkAllAsReadByTeam
- post_store.go: Use PostgreSQL UPDATE...FROM syntax in deleteThreadFiles
* Remove MySQL alternatives for JSON and subquery operations
Since Mattermost only supports PostgreSQL, remove the MySQL-specific
JSON and subquery syntax:
- thread_store.go: Use PostgreSQL JSONB operators for updating participants
- access_control_policy_store.go: Use PostgreSQL JSONB @> operator for
querying JSON imports
- session_store.go: Use PostgreSQL subquery syntax for Cleanup
- job_store.go: Use PostgreSQL subquery syntax for Cleanup
* Remove MySQL alternatives for CTE queries
Since Mattermost only supports PostgreSQL, simplify code that
uses CTEs (Common Table Expressions):
- channel_store.go: Remove MySQL CASE-based fallback in
UpdateLastViewedAt and use PostgreSQL CTE exclusively
- draft_store.go: Remove driver checks in DeleteEmptyDraftsByCreateAtAndUserId,
DeleteOrphanDraftsByCreateAtAndUserId, and determineMaxDraftSize
* Remove driver checks in migrate.go and schema_dump.go
Simplify migration code to use PostgreSQL driver directly since
PostgreSQL is the only supported database.
* Remove driver checks in sqlx_wrapper.go
Always apply lowercase named parameter transformation since PostgreSQL
is the only supported database.
* Remove driver checks in user_store.go
Simplify user store functions to use PostgreSQL-only code paths:
- Remove isPostgreSQL parameter from helper functions
- Use LEFT JOIN pattern instead of subqueries for bot filtering
- Always use case-insensitive LIKE with lower() for search
- Remove MySQL-specific role filtering alternatives
* Remove driver checks in post_store.go
Simplify post_store.go to use PostgreSQL-only code paths:
- Inline getParentsPostsPostgreSQL into getParentsPosts
- Use PostgreSQL TO_CHAR/TO_TIMESTAMP for date formatting in analytics
- Use PostgreSQL array syntax for batch deletes
- Simplify determineMaxPostSize to always use information_schema
- Use PostgreSQL jsonb subtraction for thread participants
- Always execute RefreshPostStats (PostgreSQL materialized views)
- Use materialized views for AnalyticsPostCountsByDay
- Simplify AnalyticsPostCountByTeam to always use countByTeam
* Remove driver checks in channel_store.go
Simplify channel_store.go to use PostgreSQL-only code paths:
- Always use sq.Dollar.ReplacePlaceholders for UNION queries
- Use PostgreSQL LEFT JOIN for retention policy exclusion
- Use PostgreSQL jsonb @> operator for access control policy imports
- Simplify buildLIKEClause to always use LOWER() for case-insensitive search
- Simplify buildFulltextClauseX to always use PostgreSQL to_tsvector/to_tsquery
- Simplify searchGroupChannelsQuery to use ARRAY_TO_STRING/ARRAY_AGG
* Remove driver checks in file_info_store.go
Simplify file_info_store.go to use PostgreSQL-only code paths:
- Always use PostgreSQL to_tsvector/to_tsquery for file search
- Use file_stats materialized view for CountAll()
- Use file_stats materialized view for GetStorageUsage() when not including deleted
- Always execute RefreshFileStats() for materialized view refresh
* Remove driver checks in attributes_store.go
Simplify attributes_store.go to use PostgreSQL-only code paths:
- Always execute RefreshAttributes() for materialized view refresh
- Remove isPostgreSQL parameter from generateSearchQueryForExpression
- Always use PostgreSQL LOWER() LIKE LOWER() syntax for case-insensitive search
* Remove driver checks in retention_policy_store.go
Simplify retention_policy_store.go to use PostgreSQL-only code paths:
- Remove isPostgres parameter from scanRetentionIdsForDeletion
- Always use pq.Array for scanning retention IDs
- Always use pq.Array for inserting retention IDs
- Remove unused json import
* Remove driver checks in property stores
Simplify property_field_store.go and property_value_store.go to use
PostgreSQL-only code paths:
- Always use PostgreSQL type casts (::text, ::jsonb, ::bigint, etc.)
- Remove isPostgres variable and conditionals
* Remove driver checks in channel_member_history_store.go
Simplify PermanentDeleteBatch to use PostgreSQL-only code path:
- Always use ctid-based subquery for DELETE with LIMIT
* Remove remaining driver checks in user_store.go
Simplify user_store.go to use PostgreSQL-only code paths:
- Use LEFT JOIN for bot exclusion in AnalyticsActiveCountForPeriod
- Use LEFT JOIN for bot exclusion in IsEmpty
* Simplify fulltext search by consolidating buildFulltextClause functions
Remove convertMySQLFullTextColumnsToPostgres and consolidate
buildFulltextClause and buildFulltextClauseX into a single function
that takes variadic column arguments and returns sq.Sqlizer.
* Simplify SQL stores leveraging PostgreSQL-only support
- Simplify UpdateMembersRole in channel_store.go and team_store.go
to use UPDATE...RETURNING instead of SELECT + UPDATE
- Simplify GetPostReminders in post_store.go to use DELETE...RETURNING
- Simplify DeleteOrphanedRows queries by removing MySQL workarounds
for subquery locking issues
- Simplify UpdateUserLastSyncAt to use UPDATE...FROM...RETURNING
instead of fetching user first then updating
- Remove MySQL index hint workarounds in ORDER BY clauses
- Update outdated comments referencing MySQL
- Consolidate buildFulltextClause and remove convertMySQLFullTextColumnsToPostgres
* Remove MySQL-specific test artifacts
- Delete unused MySQLStopWords variable and stop_word.go file
- Remove redundant testSearchEmailAddressesWithQuotes test
(already covered by testSearchEmailAddresses)
- Update comment that referenced MySQL query planning
* Remove MySQL references from server code outside sqlstore
- Update config example and DSN parsing docs to reflect PostgreSQL-only support
- Remove mysql:// scheme check from IsDatabaseDSN
- Simplify SanitizeDataSource to only handle PostgreSQL
- Remove outdated MySQL comments from model and plugin code
* Remove MySQL references from test files
- Update test DSNs to use PostgreSQL format
- Remove dead mysql-replica flag and replicaFlag variable
- Simplify tests that had MySQL/PostgreSQL branches
* Update docs and test config to use PostgreSQL
- Update mmctl config set example to use postgres driver
- Update test-config.json to use PostgreSQL DSN format
* Remove MySQL migration scripts, test data, and docker image
Delete MySQL-related files that are no longer needed:
- ESR upgrade scripts (esr.*.mysql.*.sql)
- MySQL schema dumps (mattermost-mysql-*.sql)
- MySQL replication test scripts (replica-*.sh, mysql-migration-test.sh)
- MySQL test warmup data (mysql_migration_warmup.sql)
- MySQL docker image reference from mirror-docker-images.json
* Remove MySQL references from webapp
- Simplify minimumHashtagLength description to remove MySQL-specific configuration note
- Remove unused HIDE_MYSQL_STATS_NOTIFICATION preference constant
- Update en.json i18n source file
* clean up e2e-tests
* rm server/tests/template.load
* Use teamMemberSliceColumns() in UpdateMembersRole RETURNING clause
Refactor to use the existing helper function instead of hardcoding
the column names, ensuring consistency if the columns are updated.
* u.id -> u.Id
* address code review feedback
---------
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-01-20 16:01:59 -05:00
|
|
|
// parseDSN parses a PostgreSQL connection string and validates the scheme.
|
2019-02-15 09:05:29 -05:00
|
|
|
//
|
Remove vestigial MySQL support (#34865)
* Remove legacy quoteColumnName() utility
Since Mattermost only supports PostgreSQL, the quoteColumnName() helper
that was designed to handle database-specific column quoting is no longer
needed. The function was a no-op that simply returned the column name
unchanged.
Remove the function from utils.go and update status_store.go to use
the "Manual" column name directly.
* Remove legacy driver checks from store.go
Since Mattermost only supports PostgreSQL, remove conditional checks
for different database drivers:
- Simplify specialSearchChars() to always return PostgreSQL-compatible chars
- Remove driver check from computeBinaryParam()
- Remove driver check from computeDefaultTextSearchConfig()
- Simplify GetDbVersion() to use PostgreSQL syntax directly
- Remove switch statement from ensureMinimumDBVersion()
- Remove unused driver parameter from versionString()
* Remove MySQL alternatives for batch delete operations
Since Mattermost only supports PostgreSQL, remove the MySQL-specific
DELETE...LIMIT syntax and keep only the PostgreSQL array-based approach:
- reaction_store.go: Use PostgreSQL array syntax for PermanentDeleteBatch
- file_info_store.go: Use PostgreSQL array syntax for PermanentDeleteBatch
- preference_store.go: Use PostgreSQL tuple IN subquery for DeleteInvalidVisibleDmsGms
* Remove MySQL alternatives for UPDATE...FROM syntax
Since Mattermost only supports PostgreSQL, remove the MySQL-specific
UPDATE syntax that joins tables differently:
- thread_store.go: Use PostgreSQL UPDATE...FROM syntax in
MarkAllAsReadByChannels and MarkAllAsReadByTeam
- post_store.go: Use PostgreSQL UPDATE...FROM syntax in deleteThreadFiles
* Remove MySQL alternatives for JSON and subquery operations
Since Mattermost only supports PostgreSQL, remove the MySQL-specific
JSON and subquery syntax:
- thread_store.go: Use PostgreSQL JSONB operators for updating participants
- access_control_policy_store.go: Use PostgreSQL JSONB @> operator for
querying JSON imports
- session_store.go: Use PostgreSQL subquery syntax for Cleanup
- job_store.go: Use PostgreSQL subquery syntax for Cleanup
* Remove MySQL alternatives for CTE queries
Since Mattermost only supports PostgreSQL, simplify code that
uses CTEs (Common Table Expressions):
- channel_store.go: Remove MySQL CASE-based fallback in
UpdateLastViewedAt and use PostgreSQL CTE exclusively
- draft_store.go: Remove driver checks in DeleteEmptyDraftsByCreateAtAndUserId,
DeleteOrphanDraftsByCreateAtAndUserId, and determineMaxDraftSize
* Remove driver checks in migrate.go and schema_dump.go
Simplify migration code to use PostgreSQL driver directly since
PostgreSQL is the only supported database.
* Remove driver checks in sqlx_wrapper.go
Always apply lowercase named parameter transformation since PostgreSQL
is the only supported database.
* Remove driver checks in user_store.go
Simplify user store functions to use PostgreSQL-only code paths:
- Remove isPostgreSQL parameter from helper functions
- Use LEFT JOIN pattern instead of subqueries for bot filtering
- Always use case-insensitive LIKE with lower() for search
- Remove MySQL-specific role filtering alternatives
* Remove driver checks in post_store.go
Simplify post_store.go to use PostgreSQL-only code paths:
- Inline getParentsPostsPostgreSQL into getParentsPosts
- Use PostgreSQL TO_CHAR/TO_TIMESTAMP for date formatting in analytics
- Use PostgreSQL array syntax for batch deletes
- Simplify determineMaxPostSize to always use information_schema
- Use PostgreSQL jsonb subtraction for thread participants
- Always execute RefreshPostStats (PostgreSQL materialized views)
- Use materialized views for AnalyticsPostCountsByDay
- Simplify AnalyticsPostCountByTeam to always use countByTeam
* Remove driver checks in channel_store.go
Simplify channel_store.go to use PostgreSQL-only code paths:
- Always use sq.Dollar.ReplacePlaceholders for UNION queries
- Use PostgreSQL LEFT JOIN for retention policy exclusion
- Use PostgreSQL jsonb @> operator for access control policy imports
- Simplify buildLIKEClause to always use LOWER() for case-insensitive search
- Simplify buildFulltextClauseX to always use PostgreSQL to_tsvector/to_tsquery
- Simplify searchGroupChannelsQuery to use ARRAY_TO_STRING/ARRAY_AGG
* Remove driver checks in file_info_store.go
Simplify file_info_store.go to use PostgreSQL-only code paths:
- Always use PostgreSQL to_tsvector/to_tsquery for file search
- Use file_stats materialized view for CountAll()
- Use file_stats materialized view for GetStorageUsage() when not including deleted
- Always execute RefreshFileStats() for materialized view refresh
* Remove driver checks in attributes_store.go
Simplify attributes_store.go to use PostgreSQL-only code paths:
- Always execute RefreshAttributes() for materialized view refresh
- Remove isPostgreSQL parameter from generateSearchQueryForExpression
- Always use PostgreSQL LOWER() LIKE LOWER() syntax for case-insensitive search
* Remove driver checks in retention_policy_store.go
Simplify retention_policy_store.go to use PostgreSQL-only code paths:
- Remove isPostgres parameter from scanRetentionIdsForDeletion
- Always use pq.Array for scanning retention IDs
- Always use pq.Array for inserting retention IDs
- Remove unused json import
* Remove driver checks in property stores
Simplify property_field_store.go and property_value_store.go to use
PostgreSQL-only code paths:
- Always use PostgreSQL type casts (::text, ::jsonb, ::bigint, etc.)
- Remove isPostgres variable and conditionals
* Remove driver checks in channel_member_history_store.go
Simplify PermanentDeleteBatch to use PostgreSQL-only code path:
- Always use ctid-based subquery for DELETE with LIMIT
* Remove remaining driver checks in user_store.go
Simplify user_store.go to use PostgreSQL-only code paths:
- Use LEFT JOIN for bot exclusion in AnalyticsActiveCountForPeriod
- Use LEFT JOIN for bot exclusion in IsEmpty
* Simplify fulltext search by consolidating buildFulltextClause functions
Remove convertMySQLFullTextColumnsToPostgres and consolidate
buildFulltextClause and buildFulltextClauseX into a single function
that takes variadic column arguments and returns sq.Sqlizer.
* Simplify SQL stores leveraging PostgreSQL-only support
- Simplify UpdateMembersRole in channel_store.go and team_store.go
to use UPDATE...RETURNING instead of SELECT + UPDATE
- Simplify GetPostReminders in post_store.go to use DELETE...RETURNING
- Simplify DeleteOrphanedRows queries by removing MySQL workarounds
for subquery locking issues
- Simplify UpdateUserLastSyncAt to use UPDATE...FROM...RETURNING
instead of fetching user first then updating
- Remove MySQL index hint workarounds in ORDER BY clauses
- Update outdated comments referencing MySQL
- Consolidate buildFulltextClause and remove convertMySQLFullTextColumnsToPostgres
* Remove MySQL-specific test artifacts
- Delete unused MySQLStopWords variable and stop_word.go file
- Remove redundant testSearchEmailAddressesWithQuotes test
(already covered by testSearchEmailAddresses)
- Update comment that referenced MySQL query planning
* Remove MySQL references from server code outside sqlstore
- Update config example and DSN parsing docs to reflect PostgreSQL-only support
- Remove mysql:// scheme check from IsDatabaseDSN
- Simplify SanitizeDataSource to only handle PostgreSQL
- Remove outdated MySQL comments from model and plugin code
* Remove MySQL references from test files
- Update test DSNs to use PostgreSQL format
- Remove dead mysql-replica flag and replicaFlag variable
- Simplify tests that had MySQL/PostgreSQL branches
* Update docs and test config to use PostgreSQL
- Update mmctl config set example to use postgres driver
- Update test-config.json to use PostgreSQL DSN format
* Remove MySQL migration scripts, test data, and docker image
Delete MySQL-related files that are no longer needed:
- ESR upgrade scripts (esr.*.mysql.*.sql)
- MySQL schema dumps (mattermost-mysql-*.sql)
- MySQL replication test scripts (replica-*.sh, mysql-migration-test.sh)
- MySQL test warmup data (mysql_migration_warmup.sql)
- MySQL docker image reference from mirror-docker-images.json
* Remove MySQL references from webapp
- Simplify minimumHashtagLength description to remove MySQL-specific configuration note
- Remove unused HIDE_MYSQL_STATS_NOTIFICATION preference constant
- Update en.json i18n source file
* clean up e2e-tests
* rm server/tests/template.load
* Use teamMemberSliceColumns() in UpdateMembersRole RETURNING clause
Refactor to use the existing helper function instead of hardcoding
the column names, ensuring consistency if the columns are updated.
* u.id -> u.Id
* address code review feedback
---------
Co-authored-by: Mattermost Build <build@mattermost.com>
2026-01-20 16:01:59 -05:00
|
|
|
// Accepts postgres:// or postgresql:// schemes and returns the DSN unmodified.
|
2019-02-15 09:05:29 -05:00
|
|
|
func parseDSN(dsn string) (string, string, error) {
|
|
|
|
|
// Treat the DSN as the URL that it is.
|
2019-09-20 00:22:40 -04:00
|
|
|
s := strings.SplitN(dsn, "://", 2)
|
|
|
|
|
if len(s) != 2 {
|
2020-03-03 11:42:17 -05:00
|
|
|
return "", "", errors.New("failed to parse DSN as URL")
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
2019-09-20 00:22:40 -04:00
|
|
|
scheme := s[0]
|
2019-02-15 09:05:29 -05:00
|
|
|
switch scheme {
|
2022-01-12 07:23:56 -05:00
|
|
|
case "postgres", "postgresql":
|
2019-02-15 09:05:29 -05:00
|
|
|
// No changes required
|
|
|
|
|
|
|
|
|
|
default:
|
2019-09-20 00:22:40 -04:00
|
|
|
return "", "", errors.Errorf("unsupported scheme %s", scheme)
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return scheme, dsn, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 10:51:42 -05:00
|
|
|
// Set replaces the current configuration in its entirety and updates the backing store.
|
2020-10-29 18:54:39 -04:00
|
|
|
func (ds *DatabaseStore) Set(newCfg *model.Config) error {
|
|
|
|
|
return ds.persist(newCfg)
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// persist writes the configuration to the configured database.
|
|
|
|
|
func (ds *DatabaseStore) persist(cfg *model.Config) error {
|
|
|
|
|
b, err := marshalConfig(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to serialize")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value := string(b)
|
2022-04-14 14:43:14 -04:00
|
|
|
sum := sha256.Sum256(b)
|
|
|
|
|
|
|
|
|
|
// Skip the persist altogether if we're effectively writing the same configuration.
|
|
|
|
|
var oldValue string
|
2025-07-22 11:10:55 -04:00
|
|
|
row := ds.db.QueryRow("SELECT SHA FROM Configurations WHERE Active")
|
2022-04-14 14:43:14 -04:00
|
|
|
if err = row.Scan(&oldValue); err != nil && err != sql.ErrNoRows {
|
|
|
|
|
return errors.Wrap(err, "failed to query active configuration")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// postgres retruns blank-padded therefore we trim the space
|
|
|
|
|
oldSum, err := hex.DecodeString(strings.TrimSpace(oldValue))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "could not encode value")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compare checksums, it's more efficient rather than comparing entire config itself
|
|
|
|
|
if bytes.Equal(oldSum, sum[0:]) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 09:05:29 -05:00
|
|
|
tx, err := ds.db.Beginx()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to begin transaction")
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
// Rollback after Commit just returns sql.ErrTxDone.
|
2022-04-14 14:43:14 -04:00
|
|
|
if err = tx.Rollback(); err != nil && err != sql.ErrTxDone {
|
2019-02-15 09:05:29 -05:00
|
|
|
mlog.Error("Failed to rollback configuration transaction", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2025-07-22 11:10:55 -04:00
|
|
|
if _, err := tx.Exec("UPDATE Configurations SET Active = NULL WHERE Active"); err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to deactivate current configuration")
|
2022-04-14 14:43:14 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
params := map[string]any{
|
2022-04-14 14:43:14 -04:00
|
|
|
"id": model.NewId(),
|
2019-02-15 09:05:29 -05:00
|
|
|
"value": value,
|
2022-04-14 14:43:14 -04:00
|
|
|
"create_at": model.GetMillis(),
|
2019-02-15 09:05:29 -05:00
|
|
|
"key": "ConfigurationId",
|
2022-04-14 14:43:14 -04:00
|
|
|
"sha": hex.EncodeToString(sum[0:]),
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
2022-04-14 14:43:14 -04:00
|
|
|
if _, err := tx.NamedExec("INSERT INTO Configurations (Id, Value, CreateAt, Active, SHA) VALUES (:id, :value, :create_at, TRUE, :sha)", params); err != nil {
|
2019-02-15 09:05:29 -05:00
|
|
|
return errors.Wrap(err, "failed to record new configuration")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
|
return errors.Wrap(err, "failed to commit transaction")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load updates the current configuration from the backing store.
|
2020-10-29 18:54:39 -04:00
|
|
|
func (ds *DatabaseStore) Load() ([]byte, error) {
|
2019-02-15 09:05:29 -05:00
|
|
|
var configurationData []byte
|
|
|
|
|
|
|
|
|
|
row := ds.db.QueryRow("SELECT Value FROM Configurations WHERE Active")
|
2020-10-29 18:54:39 -04:00
|
|
|
if err := row.Scan(&configurationData); err != nil && err != sql.ErrNoRows {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to query active configuration")
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize from the default config if no active configuration could be found.
|
|
|
|
|
if len(configurationData) == 0 {
|
2020-10-29 18:54:39 -04:00
|
|
|
configWithDB := model.Config{}
|
2024-08-05 23:45:00 -04:00
|
|
|
configWithDB.SqlSettings.DriverName = model.NewPointer(ds.driverName)
|
|
|
|
|
configWithDB.SqlSettings.DataSource = model.NewPointer(ds.dataSourceName)
|
2020-10-29 18:54:39 -04:00
|
|
|
return json.Marshal(configWithDB)
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-29 18:54:39 -04:00
|
|
|
return configurationData, nil
|
2019-03-06 15:06:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFile fetches the contents of a previously persisted configuration file.
|
|
|
|
|
func (ds *DatabaseStore) GetFile(name string) ([]byte, error) {
|
2022-07-05 02:46:50 -04:00
|
|
|
query, args, err := sqlx.Named("SELECT Data FROM ConfigurationFiles WHERE Name = :name", map[string]any{
|
2019-03-06 15:06:45 -05:00
|
|
|
"name": name,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data []byte
|
2020-02-06 09:15:18 -05:00
|
|
|
row := ds.db.QueryRowx(ds.db.Rebind(query), args...)
|
2019-03-06 15:06:45 -05:00
|
|
|
if err = row.Scan(&data); err != nil {
|
|
|
|
|
return nil, errors.Wrapf(err, "failed to scan data from row for %s", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFile sets or replaces the contents of a configuration file.
|
|
|
|
|
func (ds *DatabaseStore) SetFile(name string, data []byte) error {
|
2022-07-05 02:46:50 -04:00
|
|
|
params := map[string]any{
|
2019-03-06 15:06:45 -05:00
|
|
|
"name": name,
|
|
|
|
|
"data": data,
|
|
|
|
|
"create_at": model.GetMillis(),
|
|
|
|
|
"update_at": model.GetMillis(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := ds.db.NamedExec("UPDATE ConfigurationFiles SET Data = :data, UpdateAt = :update_at WHERE Name = :name", params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to update row for %s", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count, err := result.RowsAffected()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to count rows affected for %s", name)
|
|
|
|
|
} else if count > 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = ds.db.NamedExec("INSERT INTO ConfigurationFiles (Name, Data, CreateAt, UpdateAt) VALUES (:name, :data, :create_at, :update_at)", params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to insert row for %s", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HasFile returns true if the given file was previously persisted.
|
|
|
|
|
func (ds *DatabaseStore) HasFile(name string) (bool, error) {
|
2022-07-05 02:46:50 -04:00
|
|
|
query, args, err := sqlx.Named("SELECT COUNT(*) FROM ConfigurationFiles WHERE Name = :name", map[string]any{
|
2019-03-06 15:06:45 -05:00
|
|
|
"name": name,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 09:15:18 -05:00
|
|
|
var count int64
|
|
|
|
|
row := ds.db.QueryRowx(ds.db.Rebind(query), args...)
|
2019-03-06 15:06:45 -05:00
|
|
|
if err = row.Scan(&count); err != nil {
|
|
|
|
|
return false, errors.Wrapf(err, "failed to scan count of rows for %s", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count != 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemoveFile remoevs a previously persisted configuration file.
|
|
|
|
|
func (ds *DatabaseStore) RemoveFile(name string) error {
|
2022-07-05 02:46:50 -04:00
|
|
|
_, err := ds.db.NamedExec("DELETE FROM ConfigurationFiles WHERE Name = :name", map[string]any{
|
2019-03-06 15:06:45 -05:00
|
|
|
"name": name,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to remove row for %s", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String returns the path to the database backing the config, masking the password.
|
|
|
|
|
func (ds *DatabaseStore) String() string {
|
2023-04-03 14:11:51 -04:00
|
|
|
// This is called during the running of MM, so we expect the parsing of DSN
|
|
|
|
|
// to be successful.
|
2025-06-06 09:07:54 -04:00
|
|
|
sanitized, _ := model.SanitizeDataSource(ds.driverName, ds.originalDsn)
|
2023-04-03 14:11:51 -04:00
|
|
|
return sanitized
|
2019-02-15 09:05:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close cleans up resources associated with the store.
|
|
|
|
|
func (ds *DatabaseStore) Close() error {
|
|
|
|
|
return ds.db.Close()
|
|
|
|
|
}
|
2022-04-15 03:31:10 -04:00
|
|
|
|
2025-10-29 08:24:42 -04:00
|
|
|
// removes configurations from database if they are older than threshold,
|
|
|
|
|
// keeping the active configuration and the last 5 most recent ones.
|
|
|
|
|
func (ds *DatabaseStore) cleanUp(thresholdCreateAt int64) error {
|
|
|
|
|
query := `
|
|
|
|
|
DELETE FROM Configurations
|
|
|
|
|
WHERE CreateAt < :timestamp
|
|
|
|
|
AND (Active IS NULL OR Active = false)
|
|
|
|
|
AND ID NOT IN (
|
|
|
|
|
SELECT ID
|
|
|
|
|
FROM Configurations
|
|
|
|
|
ORDER BY CreateAt DESC
|
|
|
|
|
LIMIT 5
|
|
|
|
|
)
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
if _, err := ds.db.NamedExec(query, map[string]any{"timestamp": thresholdCreateAt}); err != nil {
|
2022-04-15 03:31:10 -04:00
|
|
|
return errors.Wrap(err, "unable to clean Configurations table")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|