mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
* 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 <[email protected]>
251 lines
8.9 KiB
Go
251 lines
8.9 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"math/rand"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
|
|
"github.com/mattermost/mattermost/server/v8/config"
|
|
"github.com/mattermost/mattermost/server/v8/einterfaces/mocks"
|
|
)
|
|
|
|
// A copy of validTestLicense from channels/utils/license_test.go
|
|
var validTestLicense = []byte("eyJpZCI6InpvZ3c2NW44Z2lmajVkbHJoYThtYnUxcGl3IiwiaXNzdWVkX2F0IjoxNjg0Nzg3MzcxODY5LCJzdGFydHNfYXQiOjE2ODQ3ODczNzE4NjksImV4cGlyZXNfYXQiOjIwMDA0MDY1MzgwMDAsInNrdV9uYW1lIj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YW5jZWRfbG9nZ2luZyI6dHJ1ZSwiZnV0dXJlX2ZlYXR1cmVzIjpmYWxzZX0sImlzX3RyaWFsIjp0cnVlLCJpc19nb3Zfc2t1IjpmYWxzZX0bEOVk2GdE1kSWKJ3dENWnkj0htY6QyXTtNA5hqnQ71Uc6teqXc7htHAxrnT/hV42xu+G24OMrAIsQtX4NjFSX6jvehIMRL5II3RPXYhHKUd2wruQ5ITEh1htFb5DgOJW3tvBdMmXt09nXjLRS1UYJ7ZsX3mU0uQndt7qfMriGAkk71veYuUJgztB3MsV7lRWB+8ZTp6WJ7RH+uWnuDspiA8B85mLnyuoCDokYksF2uIb+CtPGBTUB6qSOgxBBJxu5qftQXISCDAWY4O8lCrN3p5HCA/zf/rSRRNtet06QFobbjUDI4B7ZEAescKBKoHpP6nZPhg4KmhnkUi/o04ox")
|
|
|
|
func TestSetLicenseOnStart(t *testing.T) {
|
|
oldValue := model.BuildEnterpriseReady
|
|
defer func() { model.BuildEnterpriseReady = oldValue }()
|
|
model.BuildEnterpriseReady = "true"
|
|
|
|
cfg := model.Config{}
|
|
cfg.SetDefaults()
|
|
|
|
f, err := os.CreateTemp("", "TestSetLicenseOnStart")
|
|
require.NoError(t, err)
|
|
defer os.Remove(f.Name())
|
|
require.NoError(t, os.WriteFile(f.Name(), validTestLicense, 0777))
|
|
|
|
*cfg.ServiceSettings.LicenseFileLocation = f.Name()
|
|
|
|
driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
|
|
if driverName == "" {
|
|
driverName = model.DatabaseDriverPostgres
|
|
}
|
|
cfg.SqlSettings = *storetest.MakeSqlSettings(driverName)
|
|
|
|
configStore := config.NewTestMemoryStore()
|
|
_, _, err = configStore.Set(&cfg)
|
|
require.NoError(t, err)
|
|
// It should not panic when ps.LoadLicense gets called from platform.New
|
|
_, err = New(
|
|
ServiceConfig{},
|
|
ConfigStore(configStore),
|
|
)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestReadReplicaDisabledBasedOnLicense(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
cfg := model.Config{}
|
|
cfg.SetDefaults()
|
|
driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
|
|
if driverName == "" {
|
|
driverName = model.DatabaseDriverPostgres
|
|
}
|
|
cfg.SqlSettings = *storetest.MakeSqlSettings(driverName)
|
|
cfg.SqlSettings.DataSourceReplicas = []string{*cfg.SqlSettings.DataSource}
|
|
cfg.SqlSettings.DataSourceSearchReplicas = []string{*cfg.SqlSettings.DataSource}
|
|
|
|
t.Run("Read Replicas with no License", func(t *testing.T) {
|
|
configStore := config.NewTestMemoryStore()
|
|
_, _, err := configStore.Set(&cfg)
|
|
require.NoError(t, err)
|
|
ps, err := New(
|
|
ServiceConfig{},
|
|
ConfigStore(configStore),
|
|
)
|
|
require.NoError(t, err)
|
|
require.Same(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetReplica())
|
|
require.Len(t, ps.Config().SqlSettings.DataSourceReplicas, 1)
|
|
})
|
|
|
|
t.Run("Read Replicas With License", func(t *testing.T) {
|
|
configStore := config.NewTestMemoryStore()
|
|
_, _, err := configStore.Set(&cfg)
|
|
require.NoError(t, err)
|
|
ps, err := New(
|
|
ServiceConfig{},
|
|
ConfigStore(configStore),
|
|
func(ps *PlatformService) error {
|
|
ps.licenseValue.Store(model.NewTestLicense())
|
|
return nil
|
|
},
|
|
)
|
|
require.NoError(t, err)
|
|
require.NotSame(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetReplica())
|
|
require.Len(t, ps.Config().SqlSettings.DataSourceReplicas, 1)
|
|
})
|
|
|
|
t.Run("Search Replicas with no License", func(t *testing.T) {
|
|
configStore := config.NewTestMemoryStore()
|
|
_, _, err := configStore.Set(&cfg)
|
|
require.NoError(t, err)
|
|
ps, err := New(
|
|
ServiceConfig{},
|
|
ConfigStore(configStore),
|
|
)
|
|
require.NoError(t, err)
|
|
require.Same(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetSearchReplicaX())
|
|
require.Len(t, ps.Config().SqlSettings.DataSourceSearchReplicas, 1)
|
|
})
|
|
|
|
t.Run("Search Replicas With License", func(t *testing.T) {
|
|
configStore := config.NewTestMemoryStore()
|
|
_, _, err := configStore.Set(&cfg)
|
|
require.NoError(t, err)
|
|
ps, err := New(
|
|
ServiceConfig{},
|
|
ConfigStore(configStore),
|
|
func(ps *PlatformService) error {
|
|
ps.licenseValue.Store(model.NewTestLicense())
|
|
return nil
|
|
},
|
|
)
|
|
require.NoError(t, err)
|
|
require.NotSame(t, ps.sqlStore.GetMaster(), ps.sqlStore.GetSearchReplicaX())
|
|
require.Len(t, ps.Config().SqlSettings.DataSourceSearchReplicas, 1)
|
|
})
|
|
}
|
|
|
|
func TestMetrics(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
t.Run("ensure the metrics server is not started by default", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t)
|
|
|
|
require.Nil(t, th.Service.metrics)
|
|
})
|
|
|
|
t.Run("ensure the metrics server is started", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t, StartMetrics())
|
|
|
|
// there is no config listener for the metrics
|
|
// we handle it on config save step
|
|
cfg := th.Service.Config().Clone()
|
|
cfg.MetricsSettings.Enable = model.NewPointer(true)
|
|
_, _, appErr := th.Service.SaveConfig(cfg, false)
|
|
require.Nil(t, appErr)
|
|
|
|
require.NotNil(t, th.Service.metrics)
|
|
metricsAddr := strings.Replace(th.Service.metrics.listenAddr, "[::]", "http://localhost", 1)
|
|
metricsAddr = strings.Replace(metricsAddr, "127.0.0.1", "http://localhost", 1)
|
|
|
|
resp, err := http.Get(metricsAddr)
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
cfg.MetricsSettings.Enable = model.NewPointer(false)
|
|
_, _, appErr = th.Service.SaveConfig(cfg, false)
|
|
require.Nil(t, appErr)
|
|
|
|
_, err = http.Get(metricsAddr)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("ensure the metrics server is started with advanced metrics", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t, StartMetrics())
|
|
|
|
mockMetricsImpl := &mocks.MetricsInterface{}
|
|
mockMetricsImpl.On("Register").Return()
|
|
|
|
th.Service.metricsIFace = mockMetricsImpl
|
|
err := th.Service.resetMetrics()
|
|
require.NoError(t, err)
|
|
|
|
mockMetricsImpl.AssertExpectations(t)
|
|
})
|
|
|
|
t.Run("ensure advanced metrics have database metrics", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
mockMetricsImpl := &mocks.MetricsInterface{}
|
|
mockMetricsImpl.On("Register").Return()
|
|
mockMetricsImpl.On("ObserveStoreMethodDuration", mock.Anything, mock.Anything, mock.Anything).Return()
|
|
mockMetricsImpl.On("RegisterDBCollector", mock.AnythingOfType("*sql.DB"), "master")
|
|
|
|
th := Setup(t, StartMetrics(), func(ps *PlatformService) error {
|
|
ps.metricsIFace = mockMetricsImpl
|
|
return nil
|
|
})
|
|
|
|
_ = th.CreateUserOrGuest(t, false)
|
|
|
|
mockMetricsImpl.AssertExpectations(t)
|
|
})
|
|
}
|
|
|
|
func TestShutdown(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
t.Run("should shutdown gracefully", func(t *testing.T) {
|
|
th := Setup(t)
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
// we create plenty of go routines to make sure we wait for all of them
|
|
// to finish before shutting down
|
|
for range 1000 {
|
|
th.Service.Go(func() {
|
|
time.Sleep(time.Millisecond * time.Duration(rand.Intn(20)))
|
|
})
|
|
}
|
|
|
|
th.Shutdown(t)
|
|
|
|
// assert that there are no more go routines running
|
|
require.Zero(t, atomic.LoadInt32(&th.Service.goroutineCount))
|
|
})
|
|
}
|
|
|
|
func TestSetTelemetryId(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
t.Run("ensure client config is regenerated after setting the telemetry id", func(t *testing.T) {
|
|
th := Setup(t)
|
|
|
|
clientConfig := th.Service.LimitedClientConfig()
|
|
require.Empty(t, clientConfig["DiagnosticId"])
|
|
|
|
id := model.NewId()
|
|
th.Service.SetTelemetryId(id)
|
|
|
|
clientConfig = th.Service.LimitedClientConfig()
|
|
require.Equal(t, clientConfig["DiagnosticId"], id)
|
|
})
|
|
}
|
|
|
|
func TestDatabaseTypeAndMattermostVersion(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t)
|
|
|
|
databaseType, schemaVersion, err := th.Service.DatabaseTypeAndSchemaVersion()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "postgres", databaseType)
|
|
|
|
// It's hard to check whether the schema version is correct or not.
|
|
// So, we just check if it's greater than 1.
|
|
assert.GreaterOrEqual(t, schemaVersion, strconv.Itoa(1))
|
|
}
|