snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
312
server/channels/store/sqlstore/schema_dump.go
Normal file
312
server/channels/store/sqlstore/schema_dump.go
Normal file
@@ -0,0 +1,312 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
sq "github.com/mattermost/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
)
|
||||
|
||||
// GetSchemaDefinition dumps the database schema.
|
||||
func (ss *SqlStore) GetSchemaDefinition() (*model.SupportPacketDatabaseSchema, error) {
|
||||
var schemaInfo model.SupportPacketDatabaseSchema
|
||||
var rErr *multierror.Error
|
||||
|
||||
// Get the database collation
|
||||
dbCollation, err := ss.getDatabaseCollation()
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, err)
|
||||
} else {
|
||||
schemaInfo.DatabaseCollation = dbCollation
|
||||
}
|
||||
|
||||
// Get the database encoding
|
||||
dbEncoding, err := ss.getDatabaseEncoding()
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, err)
|
||||
} else {
|
||||
schemaInfo.DatabaseEncoding = dbEncoding
|
||||
}
|
||||
|
||||
// Get table options
|
||||
tableOptions, err := ss.getTableOptions()
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, err)
|
||||
}
|
||||
|
||||
// Get table schema information
|
||||
tablesMap, tableCollations, err := ss.getTableSchemaInformation()
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, err)
|
||||
}
|
||||
|
||||
// Get table indexes
|
||||
tableIndexes, err := ss.getTableIndexes()
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, err)
|
||||
}
|
||||
|
||||
// Process and combine table metadata
|
||||
for _, table := range tablesMap {
|
||||
// Add table collation if it exists
|
||||
if collation, ok := tableCollations[table.Name]; ok {
|
||||
table.Collation = collation
|
||||
}
|
||||
|
||||
// Add table options if they exist
|
||||
if options, ok := tableOptions[table.Name]; ok && len(options) > 0 {
|
||||
table.Options = options
|
||||
}
|
||||
|
||||
// Add table indexes if they exist
|
||||
if indexes, ok := tableIndexes[table.Name]; ok {
|
||||
table.Indexes = indexes
|
||||
}
|
||||
|
||||
schemaInfo.Tables = append(schemaInfo.Tables, *table)
|
||||
}
|
||||
|
||||
return &schemaInfo, rErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
// getDatabaseCollation retrieves the database collation for PostgreSQL
|
||||
func (ss *SqlStore) getDatabaseCollation() (string, error) {
|
||||
var dbCollation sql.NullString
|
||||
collationQuery := sq.Select("datcollate").
|
||||
From("pg_database").
|
||||
Where(sq.Expr("datname = current_database()"))
|
||||
|
||||
sqlString, args, err := collationQuery.PlaceholderFormat(sq.Dollar).ToSql()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to build database collation query")
|
||||
}
|
||||
|
||||
err = ss.GetMaster().QueryRowContext(ss.noTimeoutContext(), sqlString, args...).Scan(&dbCollation)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to get database collation")
|
||||
}
|
||||
|
||||
if !dbCollation.Valid {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return dbCollation.String, nil
|
||||
}
|
||||
|
||||
// getDatabaseEncoding retrieves the database encoding for PostgreSQL
|
||||
func (ss *SqlStore) getDatabaseEncoding() (string, error) {
|
||||
var dbEncoding sql.NullString
|
||||
encodingQuery := sq.Select("pg_encoding_to_char(encoding)").
|
||||
From("pg_database").
|
||||
Where(sq.Expr("datname = current_database()"))
|
||||
|
||||
sqlString, args, err := encodingQuery.PlaceholderFormat(sq.Dollar).ToSql()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to build database encoding query")
|
||||
}
|
||||
|
||||
err = ss.GetMaster().QueryRowContext(ss.noTimeoutContext(), sqlString, args...).Scan(&dbEncoding)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to get database encoding")
|
||||
}
|
||||
|
||||
if !dbEncoding.Valid {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return dbEncoding.String, nil
|
||||
}
|
||||
|
||||
// getTableOptions retrieves table-specific options from PostgreSQL system catalogs
|
||||
func (ss *SqlStore) getTableOptions() (map[string]map[string]string, error) {
|
||||
tableOptions := make(map[string]map[string]string)
|
||||
|
||||
optionsQuery := sq.Select("c.relname as table_name", "unnest(c.reloptions) as option_value").
|
||||
From("pg_class c").
|
||||
Join("pg_namespace n ON n.oid = c.relnamespace").
|
||||
Where(sq.And{
|
||||
sq.Expr("n.nspname = current_schema()"),
|
||||
sq.Eq{"c.relkind": "r"},
|
||||
sq.NotEq{"c.reloptions": nil},
|
||||
})
|
||||
|
||||
optionsSql, optionsArgs, err := optionsQuery.PlaceholderFormat(sq.Dollar).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to build table options query")
|
||||
}
|
||||
|
||||
optionsRows, err := ss.GetMaster().QueryContext(ss.noTimeoutContext(), optionsSql, optionsArgs...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to query table options")
|
||||
}
|
||||
defer optionsRows.Close()
|
||||
|
||||
// Process table options
|
||||
var rErr *multierror.Error
|
||||
for optionsRows.Next() {
|
||||
var tableName string
|
||||
var optionValue string
|
||||
|
||||
err = optionsRows.Scan(&tableName, &optionValue)
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, errors.Wrap(err, "failed to scan database schema row"))
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse option in format key=value
|
||||
parts := strings.SplitN(optionValue, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := parts[0]
|
||||
value := parts[1]
|
||||
|
||||
// Initialize the options map for this table if needed
|
||||
if _, ok := tableOptions[tableName]; !ok {
|
||||
tableOptions[tableName] = make(map[string]string)
|
||||
}
|
||||
|
||||
// Add option to the table
|
||||
tableOptions[tableName][key] = value
|
||||
}
|
||||
if err := optionsRows.Err(); err != nil {
|
||||
rErr = multierror.Append(rErr, errors.Wrap(err, "error iterating table options rows"))
|
||||
}
|
||||
|
||||
return tableOptions, rErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
// getTableSchemaInformation retrieves table and column information from information_schema
|
||||
func (ss *SqlStore) getTableSchemaInformation() (map[string]*model.DatabaseTable, map[string]string, error) {
|
||||
tablesMap := make(map[string]*model.DatabaseTable)
|
||||
tableCollations := make(map[string]string)
|
||||
|
||||
schemaQuery := sq.Select(
|
||||
"t.table_name",
|
||||
"c.column_name",
|
||||
"c.data_type",
|
||||
"c.character_maximum_length",
|
||||
"c.is_nullable",
|
||||
"c.collation_name",
|
||||
).
|
||||
From("information_schema.tables t").
|
||||
LeftJoin("information_schema.columns c ON t.table_name = c.table_name AND t.table_schema = c.table_schema").
|
||||
Where(sq.Expr("t.table_schema = current_schema()")).
|
||||
OrderBy("t.table_name", "c.ordinal_position")
|
||||
|
||||
schemaSql, schemaArgs, err := schemaQuery.PlaceholderFormat(sq.Dollar).ToSql()
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "failed to build schema information query")
|
||||
}
|
||||
|
||||
rows, err := ss.GetMaster().QueryContext(ss.noTimeoutContext(), schemaSql, schemaArgs...)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "failed to query schema information")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var rErr *multierror.Error
|
||||
for rows.Next() {
|
||||
var tableName, columnName, dataType, isNullable string
|
||||
var characterMaxLength sql.NullInt64
|
||||
var collationName sql.NullString
|
||||
|
||||
err = rows.Scan(&tableName, &columnName, &dataType, &characterMaxLength, &isNullable, &collationName)
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, errors.Wrap(err, "failed to scan database schema row"))
|
||||
continue
|
||||
}
|
||||
|
||||
// Track collation names for tables.
|
||||
// Only the first non-null collation encountered per table is stored in tableCollations.
|
||||
if collationName.Valid && collationName.String != "" {
|
||||
if _, ok := tableCollations[tableName]; !ok {
|
||||
tableCollations[tableName] = collationName.String
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize table in map if it doesn't exist
|
||||
if _, ok := tablesMap[tableName]; !ok {
|
||||
tablesMap[tableName] = &model.DatabaseTable{
|
||||
Name: tableName,
|
||||
Columns: []model.DatabaseColumn{},
|
||||
}
|
||||
}
|
||||
|
||||
// Add column to table
|
||||
if columnName != "" {
|
||||
maxLength := int64(0)
|
||||
if characterMaxLength.Valid {
|
||||
maxLength = characterMaxLength.Int64
|
||||
}
|
||||
|
||||
tablesMap[tableName].Columns = append(tablesMap[tableName].Columns, model.DatabaseColumn{
|
||||
Name: columnName,
|
||||
DataType: dataType,
|
||||
MaxLength: maxLength,
|
||||
IsNullable: isNullable == "YES",
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
rErr = multierror.Append(rErr, errors.Wrap(err, "error iterating schema rows"))
|
||||
}
|
||||
|
||||
return tablesMap, tableCollations, rErr.ErrorOrNil()
|
||||
}
|
||||
|
||||
// getTableIndexes retrieves index information for all tables
|
||||
func (ss *SqlStore) getTableIndexes() (map[string][]model.DatabaseIndex, error) {
|
||||
tableIndexes := make(map[string][]model.DatabaseIndex)
|
||||
|
||||
// Query pg_indexes for index information
|
||||
indexQuery := sq.Select(
|
||||
"tablename",
|
||||
"indexname",
|
||||
"indexdef",
|
||||
).
|
||||
From("pg_indexes").
|
||||
Where(sq.Expr("schemaname = current_schema()"))
|
||||
|
||||
indexSql, indexArgs, err := indexQuery.PlaceholderFormat(sq.Dollar).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to build index query")
|
||||
}
|
||||
|
||||
rows, err := ss.GetMaster().QueryContext(ss.noTimeoutContext(), indexSql, indexArgs...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to query index information")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var rErr *multierror.Error
|
||||
for rows.Next() {
|
||||
var tableName, indexName, indexDef string
|
||||
|
||||
err = rows.Scan(&tableName, &indexName, &indexDef)
|
||||
if err != nil {
|
||||
rErr = multierror.Append(rErr, errors.Wrap(err, "failed to scan index row"))
|
||||
continue
|
||||
}
|
||||
|
||||
index := model.DatabaseIndex{
|
||||
Name: indexName,
|
||||
Definition: indexDef,
|
||||
}
|
||||
|
||||
tableIndexes[tableName] = append(tableIndexes[tableName], index)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
rErr = multierror.Append(rErr, errors.Wrap(err, "error iterating index rows"))
|
||||
}
|
||||
|
||||
return tableIndexes, rErr.ErrorOrNil()
|
||||
}
|
||||
Reference in New Issue
Block a user