Migrating roles and schemes to new Cache Layer (#11936)

* Migrating roles and schemes to new Cache Layer

* Adding missed license headers

* Updating cache tests

* Adding the cache layer to the testlib helper

* Fixing cyclic dependency

* fixing a bit of not-idiomatic error handling

* Another small fix arrount idiomatic error handling
Этот коммит содержится в:
Jesús Espino
2019-09-12 18:52:45 +02:00
коммит произвёл GitHub
родитель e58aeb90a8
Коммит 28cc7e7e36
18 изменённых файлов: 410 добавлений и 510 удалений

Просмотреть файл

@@ -4,7 +4,6 @@
package sqlstore
import (
"context"
"database/sql"
"fmt"
"net/http"
@@ -15,6 +14,10 @@ import (
"github.com/mattermost/mattermost-server/store"
)
type SqlRoleStore struct {
SqlStore
}
type Role struct {
Id string
Name string
@@ -68,7 +71,9 @@ func (role Role) ToModel() *model.Role {
}
}
func initSqlSupplierRoles(sqlStore SqlStore) {
func NewSqlRoleStore(sqlStore SqlStore) store.RoleStore {
s := &SqlRoleStore{sqlStore}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(Role{}, "Roles").SetKeys(false, "Id")
table.ColMap("Id").SetMaxSize(26)
@@ -77,9 +82,13 @@ func initSqlSupplierRoles(sqlStore SqlStore) {
table.ColMap("Description").SetMaxSize(1024)
table.ColMap("Permissions").SetMaxSize(4096)
}
return s
}
func (s *SqlSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...store.LayeredStoreHint) (*model.Role, *model.AppError) {
func (s SqlRoleStore) CreateIndexesIfNotExists() {
}
func (s *SqlRoleStore) Save(role *model.Role) (*model.Role, *model.AppError) {
// Check the role is valid before proceeding.
if !role.IsValidWithoutId() {
return nil, model.NewAppError("SqlRoleStore.Save", "store.sql_role.save.invalid_role.app_error", nil, "", http.StatusBadRequest)
@@ -91,7 +100,7 @@ func (s *SqlSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...s
return nil, model.NewAppError("SqlRoleStore.RoleSave", "store.sql_role.save.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
}
defer finalizeTransaction(transaction)
createdRole, appErr := s.createRole(ctx, role, transaction, hints...)
createdRole, appErr := s.createRole(role, transaction)
if appErr != nil {
transaction.Rollback()
return nil, appErr
@@ -112,7 +121,7 @@ func (s *SqlSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...s
return dbRole.ToModel(), nil
}
func (s *SqlSupplier) createRole(ctx context.Context, role *model.Role, transaction *gorp.Transaction, hints ...store.LayeredStoreHint) (*model.Role, *model.AppError) {
func (s *SqlRoleStore) createRole(role *model.Role, transaction *gorp.Transaction) (*model.Role, *model.AppError) {
// Check the role is valid before proceeding.
if !role.IsValidWithoutId() {
return nil, model.NewAppError("SqlRoleStore.Save", "store.sql_role.save.invalid_role.app_error", nil, "", http.StatusBadRequest)
@@ -131,7 +140,7 @@ func (s *SqlSupplier) createRole(ctx context.Context, role *model.Role, transact
return dbRole.ToModel(), nil
}
func (s *SqlSupplier) RoleGet(ctx context.Context, roleId string, hints ...store.LayeredStoreHint) (*model.Role, *model.AppError) {
func (s *SqlRoleStore) Get(roleId string) (*model.Role, *model.AppError) {
var dbRole Role
if err := s.GetReplica().SelectOne(&dbRole, "SELECT * from Roles WHERE Id = :Id", map[string]interface{}{"Id": roleId}); err != nil {
@@ -144,7 +153,7 @@ func (s *SqlSupplier) RoleGet(ctx context.Context, roleId string, hints ...store
return dbRole.ToModel(), nil
}
func (s *SqlSupplier) RoleGetAll(ctx context.Context, hints ...store.LayeredStoreHint) ([]*model.Role, *model.AppError) {
func (s *SqlRoleStore) GetAll() ([]*model.Role, *model.AppError) {
var dbRoles []Role
if _, err := s.GetReplica().Select(&dbRoles, "SELECT * from Roles", map[string]interface{}{}); err != nil {
@@ -161,7 +170,7 @@ func (s *SqlSupplier) RoleGetAll(ctx context.Context, hints ...store.LayeredStor
return roles, nil
}
func (s *SqlSupplier) RoleGetByName(ctx context.Context, name string, hints ...store.LayeredStoreHint) (*model.Role, *model.AppError) {
func (s *SqlRoleStore) GetByName(name string) (*model.Role, *model.AppError) {
var dbRole Role
if err := s.GetReplica().SelectOne(&dbRole, "SELECT * from Roles WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
@@ -174,7 +183,7 @@ func (s *SqlSupplier) RoleGetByName(ctx context.Context, name string, hints ...s
return dbRole.ToModel(), nil
}
func (s *SqlSupplier) RoleGetByNames(ctx context.Context, names []string, hints ...store.LayeredStoreHint) ([]*model.Role, *model.AppError) {
func (s *SqlRoleStore) GetByNames(names []string) ([]*model.Role, *model.AppError) {
var dbRoles []*Role
if len(names) == 0 {
@@ -202,7 +211,7 @@ func (s *SqlSupplier) RoleGetByNames(ctx context.Context, names []string, hints
return roles, nil
}
func (s *SqlSupplier) RoleDelete(ctx context.Context, roleId string, hints ...store.LayeredStoreHint) (*model.Role, *model.AppError) {
func (s *SqlRoleStore) Delete(roleId string) (*model.Role, *model.AppError) {
// Get the role.
var role *Role
if err := s.GetReplica().SelectOne(&role, "SELECT * from Roles WHERE Id = :Id", map[string]interface{}{"Id": roleId}); err != nil {
@@ -224,7 +233,7 @@ func (s *SqlSupplier) RoleDelete(ctx context.Context, roleId string, hints ...st
return role.ToModel(), nil
}
func (s *SqlSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...store.LayeredStoreHint) *model.AppError {
func (s *SqlRoleStore) PermanentDeleteAll() *model.AppError {
if _, err := s.GetMaster().Exec("DELETE FROM Roles"); err != nil {
return model.NewAppError("SqlRoleStore.PermanentDeleteAll", "store.sql_role.permanent_delete_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}

Просмотреть файл

@@ -4,7 +4,6 @@
package sqlstore
import (
"context"
"database/sql"
"fmt"
"net/http"
@@ -16,7 +15,13 @@ import (
"github.com/mattermost/mattermost-server/store"
)
func initSqlSupplierSchemes(sqlStore SqlStore) {
type SqlSchemeStore struct {
SqlStore
}
func NewSqlSchemeStore(sqlStore SqlStore) store.SchemeStore {
s := &SqlSchemeStore{sqlStore}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(model.Scheme{}, "Schemes").SetKeys(false, "Id")
table.ColMap("Id").SetMaxSize(26)
@@ -31,9 +36,14 @@ func initSqlSupplierSchemes(sqlStore SqlStore) {
table.ColMap("DefaultChannelUserRole").SetMaxSize(64)
table.ColMap("DefaultChannelGuestRole").SetMaxSize(64)
}
return s
}
func (s *SqlSupplier) SchemeSave(ctx context.Context, scheme *model.Scheme, hints ...store.LayeredStoreHint) (*model.Scheme, *model.AppError) {
func (s SqlSchemeStore) CreateIndexesIfNotExists() {
}
func (s *SqlSchemeStore) Save(scheme *model.Scheme) (*model.Scheme, *model.AppError) {
if len(scheme.Id) == 0 {
transaction, err := s.GetMaster().Begin()
if err != nil {
@@ -41,7 +51,7 @@ func (s *SqlSupplier) SchemeSave(ctx context.Context, scheme *model.Scheme, hint
}
defer finalizeTransaction(transaction)
newScheme, appErr := s.createScheme(ctx, scheme, transaction, hints...)
newScheme, appErr := s.createScheme(scheme, transaction)
if appErr != nil {
return nil, appErr
}
@@ -68,11 +78,11 @@ func (s *SqlSupplier) SchemeSave(ctx context.Context, scheme *model.Scheme, hint
return scheme, nil
}
func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, transaction *gorp.Transaction, hints ...store.LayeredStoreHint) (*model.Scheme, *model.AppError) {
func (s *SqlSchemeStore) createScheme(scheme *model.Scheme, transaction *gorp.Transaction) (*model.Scheme, *model.AppError) {
// Fetch the default system scheme roles to populate default permissions.
defaultRoleNames := []string{model.TEAM_ADMIN_ROLE_ID, model.TEAM_USER_ROLE_ID, model.TEAM_GUEST_ROLE_ID, model.CHANNEL_ADMIN_ROLE_ID, model.CHANNEL_USER_ROLE_ID, model.CHANNEL_GUEST_ROLE_ID}
defaultRoles := make(map[string]*model.Role)
roles, err := s.RoleGetByNames(ctx, defaultRoleNames)
roles, err := s.SqlStore.Role().GetByNames(defaultRoleNames)
if err != nil {
return nil, err
}
@@ -108,7 +118,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
SchemeManaged: true,
}
savedRole, err := s.createRole(ctx, teamAdminRole, transaction)
savedRole, err := s.SqlStore.Role().(*SqlRoleStore).createRole(teamAdminRole, transaction)
if err != nil {
return nil, err
}
@@ -122,7 +132,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
SchemeManaged: true,
}
savedRole, err = s.createRole(ctx, teamUserRole, transaction)
savedRole, err = s.SqlStore.Role().(*SqlRoleStore).createRole(teamUserRole, transaction)
if err != nil {
return nil, err
}
@@ -136,7 +146,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
SchemeManaged: true,
}
savedRole, err = s.createRole(ctx, teamGuestRole, transaction)
savedRole, err = s.SqlStore.Role().(*SqlRoleStore).createRole(teamGuestRole, transaction)
if err != nil {
return nil, err
}
@@ -151,7 +161,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
SchemeManaged: true,
}
savedRole, err := s.createRole(ctx, channelAdminRole, transaction)
savedRole, err := s.SqlStore.Role().(*SqlRoleStore).createRole(channelAdminRole, transaction)
if err != nil {
return nil, err
}
@@ -165,7 +175,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
SchemeManaged: true,
}
savedRole, err = s.createRole(ctx, channelUserRole, transaction)
savedRole, err = s.SqlStore.Role().(*SqlRoleStore).createRole(channelUserRole, transaction)
if err != nil {
return nil, err
}
@@ -179,7 +189,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
SchemeManaged: true,
}
savedRole, err = s.createRole(ctx, channelGuestRole, transaction)
savedRole, err = s.SqlStore.Role().(*SqlRoleStore).createRole(channelGuestRole, transaction)
if err != nil {
return nil, err
}
@@ -205,7 +215,7 @@ func (s *SqlSupplier) createScheme(ctx context.Context, scheme *model.Scheme, tr
return scheme, nil
}
func (s *SqlSupplier) SchemeGet(ctx context.Context, schemeId string, hints ...store.LayeredStoreHint) (*model.Scheme, *model.AppError) {
func (s *SqlSchemeStore) Get(schemeId string) (*model.Scheme, *model.AppError) {
var scheme model.Scheme
if err := s.GetReplica().SelectOne(&scheme, "SELECT * from Schemes WHERE Id = :Id", map[string]interface{}{"Id": schemeId}); err != nil {
if err == sql.ErrNoRows {
@@ -217,7 +227,7 @@ func (s *SqlSupplier) SchemeGet(ctx context.Context, schemeId string, hints ...s
return &scheme, nil
}
func (s *SqlSupplier) SchemeGetByName(ctx context.Context, schemeName string, hints ...store.LayeredStoreHint) (*model.Scheme, *model.AppError) {
func (s *SqlSchemeStore) GetByName(schemeName string) (*model.Scheme, *model.AppError) {
var scheme model.Scheme
if err := s.GetReplica().SelectOne(&scheme, "SELECT * from Schemes WHERE Name = :Name", map[string]interface{}{"Name": schemeName}); err != nil {
@@ -230,7 +240,7 @@ func (s *SqlSupplier) SchemeGetByName(ctx context.Context, schemeName string, hi
return &scheme, nil
}
func (s *SqlSupplier) SchemeDelete(ctx context.Context, schemeId string, hints ...store.LayeredStoreHint) (*model.Scheme, *model.AppError) {
func (s *SqlSchemeStore) Delete(schemeId string) (*model.Scheme, *model.AppError) {
// Get the scheme
var scheme model.Scheme
if err := s.GetReplica().SelectOne(&scheme, "SELECT * from Schemes WHERE Id = :Id", map[string]interface{}{"Id": schemeId}); err != nil {
@@ -290,7 +300,7 @@ func (s *SqlSupplier) SchemeDelete(ctx context.Context, schemeId string, hints .
return &scheme, nil
}
func (s *SqlSupplier) SchemeGetAllPage(ctx context.Context, scope string, offset int, limit int, hints ...store.LayeredStoreHint) ([]*model.Scheme, *model.AppError) {
func (s *SqlSchemeStore) GetAllPage(scope string, offset int, limit int) ([]*model.Scheme, *model.AppError) {
var schemes []*model.Scheme
scopeClause := ""
@@ -305,7 +315,7 @@ func (s *SqlSupplier) SchemeGetAllPage(ctx context.Context, scope string, offset
return schemes, nil
}
func (s *SqlSupplier) SchemePermanentDeleteAll(ctx context.Context, hints ...store.LayeredStoreHint) *model.AppError {
func (s *SqlSchemeStore) PermanentDeleteAll() *model.AppError {
if _, err := s.GetMaster().Exec("DELETE from Schemes"); err != nil {
return model.NewAppError("SqlSchemeStore.PermanentDeleteAll", "store.sql_scheme.permanent_delete_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}

Просмотреть файл

@@ -152,11 +152,10 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter
supplier.oldStores.UserTermsOfService = NewSqlUserTermsOfServiceStore(supplier)
supplier.oldStores.linkMetadata = NewSqlLinkMetadataStore(supplier)
supplier.oldStores.reaction = NewSqlReactionStore(supplier)
supplier.oldStores.role = NewSqlRoleStore(supplier)
supplier.oldStores.scheme = NewSqlSchemeStore(supplier)
supplier.oldStores.group = NewSqlGroupStore(supplier)
initSqlSupplierRoles(supplier)
initSqlSupplierSchemes(supplier)
err := supplier.GetMaster().CreateTablesIfNotExists()
if err != nil {
mlog.Critical("Error creating database tables.", mlog.Err(err))