mattermost/server/config/utils_test.go
Jesse Hallam 41e5c7286b
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 21:01:59 +00:00

263 lines
9.9 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package config
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/utils"
)
func TestDesanitize(t *testing.T) {
actual := &model.Config{}
actual.SetDefaults()
// These setting should be ignored
actual.LdapSettings.Enable = model.NewPointer(false)
actual.FileSettings.DriverName = model.NewPointer("s3")
// These settings should be desanitized into target.
actual.LdapSettings.BindPassword = model.NewPointer("bind_password")
actual.FileSettings.PublicLinkSalt = model.NewPointer("public_link_salt")
actual.FileSettings.AmazonS3SecretAccessKey = model.NewPointer("amazon_s3_secret_access_key")
actual.EmailSettings.SMTPPassword = model.NewPointer("smtp_password")
actual.GitLabSettings.Secret = model.NewPointer("secret")
actual.OpenIdSettings.Secret = model.NewPointer("secret")
actual.SqlSettings.DataSource = model.NewPointer("data_source")
actual.SqlSettings.AtRestEncryptKey = model.NewPointer("at_rest_encrypt_key")
actual.ElasticsearchSettings.Password = model.NewPointer("password")
actual.SqlSettings.DataSourceReplicas = append(actual.SqlSettings.DataSourceReplicas, "replica0")
actual.SqlSettings.DataSourceReplicas = append(actual.SqlSettings.DataSourceReplicas, "replica1")
actual.SqlSettings.DataSourceSearchReplicas = append(actual.SqlSettings.DataSourceSearchReplicas, "search_replica0")
actual.SqlSettings.DataSourceSearchReplicas = append(actual.SqlSettings.DataSourceSearchReplicas, "search_replica1")
actual.PluginSettings.Plugins = map[string]map[string]any{
"plugin1": {
"secret": "value1",
"no_secret": "value2",
},
}
target := &model.Config{}
target.SetDefaults()
// These setting should be ignored
target.LdapSettings.Enable = model.NewPointer(true)
target.FileSettings.DriverName = model.NewPointer("file")
// These settings should be updated from actual
target.LdapSettings.BindPassword = model.NewPointer(model.FakeSetting)
target.FileSettings.PublicLinkSalt = model.NewPointer(model.FakeSetting)
target.FileSettings.AmazonS3SecretAccessKey = model.NewPointer(model.FakeSetting)
target.EmailSettings.SMTPPassword = model.NewPointer(model.FakeSetting)
target.GitLabSettings.Secret = model.NewPointer(model.FakeSetting)
target.OpenIdSettings.Secret = model.NewPointer(model.FakeSetting)
target.SqlSettings.DataSource = model.NewPointer(model.FakeSetting)
target.SqlSettings.AtRestEncryptKey = model.NewPointer(model.FakeSetting)
target.ElasticsearchSettings.Password = model.NewPointer(model.FakeSetting)
target.SqlSettings.DataSourceReplicas = []string{model.FakeSetting, model.FakeSetting}
target.SqlSettings.DataSourceSearchReplicas = []string{model.FakeSetting, model.FakeSetting}
target.PluginSettings.Plugins = map[string]map[string]any{
"plugin1": {
"secret": model.FakeSetting,
"no_secret": "value2",
},
}
actualClone := actual.Clone()
desanitize(actual, target)
assert.Equal(t, actualClone, actual, "actual should not have been changed")
// Verify the settings that should have been left untouched in target
assert.True(t, *target.LdapSettings.Enable, "LdapSettings.Enable should not have changed")
assert.Equal(t, "file", *target.FileSettings.DriverName, "FileSettings.DriverName should not have been changed")
// Verify the settings that should have been desanitized into target
assert.Equal(t, *actual.LdapSettings.BindPassword, *target.LdapSettings.BindPassword)
assert.Equal(t, *actual.FileSettings.PublicLinkSalt, *target.FileSettings.PublicLinkSalt)
assert.Equal(t, *actual.FileSettings.AmazonS3SecretAccessKey, *target.FileSettings.AmazonS3SecretAccessKey)
assert.Equal(t, *actual.EmailSettings.SMTPPassword, *target.EmailSettings.SMTPPassword)
assert.Equal(t, *actual.GitLabSettings.Secret, *target.GitLabSettings.Secret)
assert.Equal(t, *actual.OpenIdSettings.Secret, *target.OpenIdSettings.Secret)
assert.Equal(t, *actual.SqlSettings.DataSource, *target.SqlSettings.DataSource)
assert.Equal(t, *actual.SqlSettings.AtRestEncryptKey, *target.SqlSettings.AtRestEncryptKey)
assert.Equal(t, *actual.ElasticsearchSettings.Password, *target.ElasticsearchSettings.Password)
assert.Equal(t, actual.SqlSettings.DataSourceReplicas, target.SqlSettings.DataSourceReplicas)
assert.Equal(t, actual.SqlSettings.DataSourceSearchReplicas, target.SqlSettings.DataSourceSearchReplicas)
assert.Equal(t, actual.ServiceSettings.SplitKey, target.ServiceSettings.SplitKey)
assert.Equal(t, actual.PluginSettings.Plugins, target.PluginSettings.Plugins)
}
func TestFixInvalidLocales(t *testing.T) {
// utils.TranslationsPreInit errors when TestFixInvalidLocales is run as part of testing the package,
// but doesn't error when the test is run individually.
_ = utils.TranslationsPreInit()
cfg := &model.Config{}
cfg.SetDefaults()
*cfg.LocalizationSettings.DefaultServerLocale = "en"
*cfg.LocalizationSettings.DefaultClientLocale = "en"
*cfg.LocalizationSettings.AvailableLocales = ""
changed := fixInvalidLocales(cfg)
assert.False(t, changed)
*cfg.LocalizationSettings.DefaultServerLocale = "junk"
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultServerLocale)
*cfg.LocalizationSettings.DefaultServerLocale = ""
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultServerLocale)
*cfg.LocalizationSettings.AvailableLocales = "en"
*cfg.LocalizationSettings.DefaultServerLocale = "de"
changed = fixInvalidLocales(cfg)
assert.False(t, changed)
assert.NotContains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale, "DefaultServerLocale should not be added to AvailableLocales")
*cfg.LocalizationSettings.AvailableLocales = ""
*cfg.LocalizationSettings.DefaultClientLocale = "junk"
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultClientLocale)
*cfg.LocalizationSettings.DefaultClientLocale = ""
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Equal(t, "en", *cfg.LocalizationSettings.DefaultClientLocale)
*cfg.LocalizationSettings.AvailableLocales = "en"
*cfg.LocalizationSettings.DefaultClientLocale = "de"
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Contains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale, "DefaultClientLocale should have been added to AvailableLocales")
// validate AvailableLocales
*cfg.LocalizationSettings.DefaultServerLocale = "en"
*cfg.LocalizationSettings.DefaultClientLocale = "en"
*cfg.LocalizationSettings.AvailableLocales = "junk"
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Equal(t, "", *cfg.LocalizationSettings.AvailableLocales)
*cfg.LocalizationSettings.AvailableLocales = "en,de,junk"
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.Equal(t, "", *cfg.LocalizationSettings.AvailableLocales)
*cfg.LocalizationSettings.DefaultServerLocale = "fr"
*cfg.LocalizationSettings.DefaultClientLocale = "de"
*cfg.LocalizationSettings.AvailableLocales = "en"
changed = fixInvalidLocales(cfg)
assert.True(t, changed)
assert.NotContains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultServerLocale, "DefaultServerLocale should not be added to AvailableLocales")
assert.Contains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale, "DefaultClientLocale should have been added to AvailableLocales")
}
func TestIsDatabaseDSN(t *testing.T) {
testCases := []struct {
Name string
DSN string
Expected bool
}{
{
Name: "Postgresql 'postgres' DSN",
DSN: "postgres://localhost",
Expected: true,
},
{
Name: "Postgresql 'postgresql' DSN",
DSN: "postgresql://localhost",
Expected: true,
},
{
Name: "Empty DSN",
DSN: "",
Expected: false,
},
{
Name: "Default file DSN",
DSN: "config.json",
Expected: false,
},
{
Name: "Relative path DSN",
DSN: "configuration/config.json",
Expected: false,
},
{
Name: "Absolute path DSN",
DSN: "/opt/mattermost/configuration/config.json",
Expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
assert.Equal(t, tc.Expected, IsDatabaseDSN(tc.DSN))
})
}
}
func TestIsJSONMap(t *testing.T) {
tests := []struct {
name string
data string
want bool
}{
{name: "good json", data: `{"local_tcp": {
"Type": "tcp","Format": "json","Levels": [
{"ID": 5,"Name": "debug","Stacktrace": false}
],
"Options": {"ip": "localhost","port": 18065},
"MaxQueueSize": 1000}}
`, want: true,
},
{name: "empty json", data: "{}", want: true},
{name: "string json", data: `"test"`, want: false},
{name: "array json", data: `["test1", "test2"]`, want: false},
{name: "bad json", data: `{huh?}`, want: false},
{name: "filename", data: "/tmp/logger.conf", want: false},
{name: "postgres dsn", data: "postgres://mmuser:passwordlocalhost:5432/mattermost?sslmode=disable&connect_timeout=10", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isJSONMap([]byte(tt.data)); got != tt.want {
t.Errorf("isJSONMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestEqual(t *testing.T) {
t.Run("nil", func(t *testing.T) {
diff, err := equal(nil, nil)
require.NoError(t, err)
require.False(t, diff)
})
t.Run("no diff", func(t *testing.T) {
old := minimalConfig.Clone()
n := minimalConfig.Clone()
diff, err := equal(old, n)
require.NoError(t, err)
require.False(t, diff)
})
t.Run("diff", func(t *testing.T) {
old := minimalConfig.Clone()
n := minimalConfig.Clone()
n.SqlSettings = model.SqlSettings{}
diff, err := equal(old, n)
require.NoError(t, err)
require.True(t, diff)
})
}