mirror of
https://github.com/mattermost/focalboard.git
synced 2025-01-23 18:34:02 +02:00
Use name
column to determine if schema_migrations table is correct format (#4557)
* Use name column to determine if schema_migrations table is correct format * fix result when table missing * fix result when table missing * really fix result when table missing * fix linter * more linter --------- Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
parent
f1a190d4d6
commit
935e5c68fd
3
mattermost-plugin/server/manifest.go
generated
3
mattermost-plugin/server/manifest.go
generated
@ -45,8 +45,7 @@ const manifestStr = `
|
|||||||
"type": "bool",
|
"type": "bool",
|
||||||
"help_text": "This allows board editors to share boards that can be accessed by anyone with the link.",
|
"help_text": "This allows board editors to share boards that can be accessed by anyone with the link.",
|
||||||
"placeholder": "",
|
"placeholder": "",
|
||||||
"default": false,
|
"default": false
|
||||||
"hosting": ""
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,13 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
sq "github.com/Masterminds/squirrel"
|
sq "github.com/Masterminds/squirrel"
|
||||||
"github.com/mattermost/focalboard/server/model"
|
"github.com/mattermost/focalboard/server/model"
|
||||||
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
||||||
"github.com/mattermost/morph/models"
|
"github.com/mattermost/morph/models"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnsureSchemaMigrationFormat checks the schema migrations table
|
// EnsureSchemaMigrationFormat checks the schema migrations table
|
||||||
@ -21,6 +23,7 @@ func (s *SQLStore) EnsureSchemaMigrationFormat() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !migrationNeeded {
|
if !migrationNeeded {
|
||||||
|
s.logger.Info("Schema migration table is correct format")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,8 +108,8 @@ func filterMigrations(migrations []*models.Migration, legacySchemaVersion uint32
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) isSchemaMigrationNeeded() (bool, error) {
|
func (s *SQLStore) isSchemaMigrationNeeded() (bool, error) {
|
||||||
// Check if `dirty` column exists on schema version table.
|
// Check if `name` column exists on schema version table.
|
||||||
// This column exists only for the old schema version table.
|
// This column exists only for the new schema version table.
|
||||||
|
|
||||||
// SQLite needs a bit of a special handling
|
// SQLite needs a bit of a special handling
|
||||||
if s.dbType == model.SqliteDBType {
|
if s.dbType == model.SqliteDBType {
|
||||||
@ -114,22 +117,46 @@ func (s *SQLStore) isSchemaMigrationNeeded() (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query := s.getQueryBuilder(s.db).
|
query := s.getQueryBuilder(s.db).
|
||||||
Select("count(*)").
|
Select("COLUMN_NAME").
|
||||||
From("information_schema.COLUMNS").
|
From("information_schema.COLUMNS").
|
||||||
Where(sq.Eq{
|
Where(sq.Eq{
|
||||||
"TABLE_NAME": s.tablePrefix + "schema_migrations",
|
"TABLE_NAME": s.tablePrefix + "schema_migrations",
|
||||||
"COLUMN_NAME": "dirty",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
row := query.QueryRow()
|
rows, err := query.Query()
|
||||||
|
if err != nil {
|
||||||
var count int
|
s.logger.Error("failed to fetch columns in schema_migrations table", mlog.Err(err))
|
||||||
if err := row.Scan(&count); err != nil {
|
|
||||||
s.logger.Error("failed to check for columns of schema_migrations table", mlog.Err(err))
|
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return count == 1, nil
|
defer s.CloseRows(rows)
|
||||||
|
|
||||||
|
data := []string{}
|
||||||
|
for rows.Next() {
|
||||||
|
var columnName string
|
||||||
|
|
||||||
|
err := rows.Scan(&columnName)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Error("error scanning rows from schema_migrations table definition", mlog.Err(err))
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
data = append(data, columnName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(data) == 0 {
|
||||||
|
// if no data then table does not exist and therefore a schema migration is not needed.
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, columnName := range data {
|
||||||
|
// look for a column named 'name', if found then no migration is needed
|
||||||
|
if strings.ToLower(columnName) == "name" {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) isSchemaMigrationNeededSQLite() (bool, error) {
|
func (s *SQLStore) isSchemaMigrationNeededSQLite() (bool, error) {
|
||||||
@ -145,18 +172,27 @@ func (s *SQLStore) isSchemaMigrationNeededSQLite() (bool, error) {
|
|||||||
|
|
||||||
defer s.CloseRows(rows)
|
defer s.CloseRows(rows)
|
||||||
|
|
||||||
|
const (
|
||||||
|
idxCid = iota
|
||||||
|
idxName
|
||||||
|
idxType
|
||||||
|
idxNotnull
|
||||||
|
idxDfltValue
|
||||||
|
idxPk
|
||||||
|
)
|
||||||
|
|
||||||
data := [][]*string{}
|
data := [][]*string{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
// PRAGMA returns 6 columns
|
// PRAGMA returns 6 columns
|
||||||
row := make([]*string, 6)
|
row := make([]*string, 6)
|
||||||
|
|
||||||
err := rows.Scan(
|
err := rows.Scan(
|
||||||
&row[0],
|
&row[idxCid],
|
||||||
&row[1],
|
&row[idxName],
|
||||||
&row[2],
|
&row[idxType],
|
||||||
&row[3],
|
&row[idxNotnull],
|
||||||
&row[4],
|
&row[idxDfltValue],
|
||||||
&row[5],
|
&row[idxPk],
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("error scanning rows from SQLite schema_migrations table definition", mlog.Err(err))
|
s.logger.Error("error scanning rows from SQLite schema_migrations table definition", mlog.Err(err))
|
||||||
@ -166,15 +202,19 @@ func (s *SQLStore) isSchemaMigrationNeededSQLite() (bool, error) {
|
|||||||
data = append(data, row)
|
data = append(data, row)
|
||||||
}
|
}
|
||||||
|
|
||||||
nameColumnFound := false
|
if len(data) == 0 {
|
||||||
|
// if no data then table does not exist and therefore a schema migration is not needed.
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
for _, row := range data {
|
for _, row := range data {
|
||||||
if len(row) >= 2 && *row[1] == "dirty" {
|
// look for a column named 'name', if found then no migration is needed
|
||||||
nameColumnFound = true
|
if len(row) >= 2 && strings.ToLower(*row[idxName]) == "name" {
|
||||||
break
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nameColumnFound, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) getLegacySchemaVersion() (uint32, error) {
|
func (s *SQLStore) getLegacySchemaVersion() (uint32, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user