func (s *Manager) SetReloadSchema(ctx context.Context, dbAlias, project string, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	schemaMod, _ := s.modules.GetSchemaModuleForSyncMan(project)
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, errors.New("specified database not present in config")
	}
	parsedSchama, _ := helpers2.Parser(projectConfig.DatabaseSchemas)
	for _, dbSchema := range projectConfig.DatabaseSchemas {
		if dbSchema.Table == "default" || dbSchema.DbAlias != dbAlias {
			continue
		}
		result, err := schemaMod.SchemaInspection(ctx, dbAlias, projectConfig.DatabaseConfigs[config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)].DBName, dbSchema.Table, parsedSchama[dbAlias])
		if err != nil {
			return http.StatusInternalServerError, err
		}
		dbSchema.Schema = result
	}
	if err := s.modules.SetDatabaseSchemaConfig(ctx, project, projectConfig.DatabaseSchemas); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	for resourceID, v := range projectConfig.DatabaseSchemas {
		if err := s.store.SetResource(ctx, resourceID, v); err != nil {
			return http.StatusInternalServerError, err
		}
	}
	return http.StatusOK, nil
}
SetReloadSchema reloads of the schema
package syncman
import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"time"
	"github.com/spaceuptech/helpers"
	"github.com/spaceuptech/space-cloud/gateway/model"
	"github.com/spaceuptech/space-cloud/gateway/modules/crud"
	helpers2 "github.com/spaceuptech/space-cloud/gateway/modules/schema/helpers"
	"github.com/spaceuptech/space-cloud/gateway/config"
)
func (s *Manager) SetDeleteCollection(ctx context.Context, project, dbAlias, col string, module *crud.Module, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to get prepared query provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseSchema, dbAlias, col)
	delete(projectConfig.DatabaseSchemas, resourceID)
	if err := s.modules.SetDatabaseSchemaConfig(ctx, project, projectConfig.DatabaseSchemas); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := module.DeleteTable(ctx, dbAlias, col); err != nil {
		return http.StatusInternalServerError, err
	}
	dbRulesResourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseRule, dbAlias, col, "rule")
	delete(projectConfig.DatabaseRules, dbRulesResourceID)
	if err := s.modules.SetDatabaseRulesConfig(ctx, project, projectConfig.DatabaseRules); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := s.store.DeleteResource(ctx, resourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	if err := s.store.DeleteResource(ctx, dbRulesResourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) SetDatabaseConnection(ctx context.Context, project, dbAlias string, v *config.DatabaseConfig, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	v.DbAlias = dbAlias
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)
	if projectConfig.DatabaseConfigs == nil {
		projectConfig.DatabaseConfigs = config.DatabaseConfigs{resourceID: v}
	} else {
		projectConfig.DatabaseConfigs[resourceID] = v
	}
	if err := s.modules.SetDatabaseConfig(ctx, project, projectConfig.DatabaseConfigs, projectConfig.DatabaseSchemas, projectConfig.DatabaseRules, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := s.store.SetResource(ctx, resourceID, v); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) RemoveDatabaseConfig(ctx context.Context, project, dbAlias string, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	resourceIDs := make([]string, 0)
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)
	resourceIDs = append(resourceIDs, resourceID)
	delete(projectConfig.DatabaseConfigs, resourceID)
	for _, databaseRule := range projectConfig.DatabaseRules {
		if databaseRule.DbAlias == dbAlias {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseRule, dbAlias, databaseRule.Table, "rule")
			resourceIDs = append(resourceIDs, resourceID)
			delete(projectConfig.DatabaseRules, resourceID)
		}
	}
	for _, databaseSchema := range projectConfig.DatabaseSchemas {
		if databaseSchema.DbAlias == dbAlias {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseSchema, dbAlias, databaseSchema.Table)
			resourceIDs = append(resourceIDs, resourceID)
			delete(projectConfig.DatabaseSchemas, resourceID)
		}
	}
	for _, preparedQuery := range projectConfig.DatabasePreparedQueries {
		if preparedQuery.DbAlias == dbAlias {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, preparedQuery.ID)
			resourceIDs = append(resourceIDs, resourceID)
			delete(projectConfig.DatabasePreparedQueries, resourceID)
		}
	}
	if err := s.modules.SetDatabaseConfig(ctx, project, projectConfig.DatabaseConfigs, projectConfig.DatabaseSchemas, projectConfig.DatabaseRules, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	for _, resourceID := range resourceIDs {
		if err := s.store.DeleteResource(ctx, resourceID); err != nil {
			return http.StatusInternalServerError, err
		}
	}
	_ = s.modules.Caching().PurgeCache(ctx, project, &model.CachePurgeRequest{Resource: config.ResourceDatabaseSchema, DbAlias: dbAlias, ID: "*"})
	return http.StatusOK, nil
}
func (s *Manager) GetLogicalDatabaseName(ctx context.Context, project, dbAlias string) (string, error) {
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return "", err
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)
	dbConfig, ok := projectConfig.DatabaseConfigs[resourceID]
	if !ok {
		return "", helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to get logical database name provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	return dbConfig.DBName, nil
}
func (s *Manager) GetPreparedQuery(ctx context.Context, project, dbAlias, id string, params model.RequestParams) (int, []interface{}, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), nil, err
		}
		return hookResponse.Status(), hookResponse.Result().([]interface{}), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, nil, err
	}
	if dbAlias != "*" {
		if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
			return http.StatusBadRequest, nil, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to get prepared query as provided db alias (%s) does not exists", dbAlias), nil, nil)
		}
		if id != "*" {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, id)
			preparedQuery, ok := projectConfig.DatabasePreparedQueries[resourceID]
			if !ok {
				return http.StatusBadRequest, nil, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Prepared query with id (%s) not present in config", id), nil, nil)
			}
			return http.StatusOK, []interface{}{preparedQuery}, nil
		}
		coll := make([]interface{}, 0)
		for _, value := range projectConfig.DatabasePreparedQueries {
			if value.DbAlias == dbAlias {
				coll = append(coll, value)
			}
		}
		return http.StatusOK, coll, nil
	}
	coll := make([]interface{}, 0)
	for _, dbPreparedQuery := range projectConfig.DatabasePreparedQueries {
		coll = append(coll, dbPreparedQuery)
	}
	return http.StatusOK, coll, nil
}
func (s *Manager) SetPreparedQueries(ctx context.Context, project, dbAlias, id string, v *config.DatbasePreparedQuery, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	v.ID = id
	v.DbAlias = dbAlias
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to set prepared query as provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, id)
	if projectConfig.DatabasePreparedQueries == nil {
		projectConfig.DatabasePreparedQueries = config.DatabasePreparedQueries{resourceID: v}
	} else {
		projectConfig.DatabasePreparedQueries[resourceID] = v
	}
	if err := s.modules.SetDatabasePreparedQueryConfig(ctx, project, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set database prepared query config", err, nil)
	}
	if err := s.store.SetResource(ctx, resourceID, v); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) RemovePreparedQueries(ctx context.Context, project, dbAlias, id string, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to remove prepared query as provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, id)
	delete(projectConfig.DatabasePreparedQueries, resourceID)
	if err := s.modules.SetDatabasePreparedQueryConfig(ctx, project, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set database prepared query config", err, nil)
	}
	if err := s.store.DeleteResource(ctx, resourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) SetModifySchema(ctx context.Context, project, dbAlias, col string, v *config.DatabaseSchema, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to modify schema provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseSchema, dbAlias, col)
	v.DbAlias = dbAlias
	v.Table = col
	schemaMod, _ := s.modules.GetSchemaModuleForSyncMan(project)
	if err := schemaMod.SchemaModifyAll(ctx, dbAlias, projectConfig.DatabaseConfigs[config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)].DBName, config.DatabaseSchemas{resourceID: v}); err != nil {
		return http.StatusInternalServerError, err
	}
	if projectConfig.DatabaseSchemas == nil {
		projectConfig.DatabaseSchemas = config.DatabaseSchemas{resourceID: v}
	} else {
		projectConfig.DatabaseSchemas[resourceID] = v
	}
	if err := s.modules.SetDatabaseSchemaConfig(ctx, project, projectConfig.DatabaseSchemas); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := s.store.SetResource(ctx, resourceID, v); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) SetCollectionRules(ctx context.Context, project, dbAlias, col string, v *config.DatabaseRule, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	return s.setCollectionRules(ctx, projectConfig, project, dbAlias, col, v)
}
func (s *Manager) setCollectionRules(ctx context.Context, projectConfig *config.Project, project, dbAlias, col string, v *config.DatabaseRule) (int, error) {
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to set collection/table rules as provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseRule, dbAlias, col, "rule")
	v.Table = col
	v.DbAlias = dbAlias
	if projectConfig.DatabaseRules == nil {
		projectConfig.DatabaseRules = config.DatabaseRules{resourceID: v}
	} else {
		projectConfig.DatabaseRules[resourceID] = v
	}
	if err := s.modules.SetDatabaseRulesConfig(ctx, project, projectConfig.DatabaseRules); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set database rule config", err, nil)
	}
	if err := s.store.SetResource(ctx, resourceID, v); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) DeleteCollectionRules(ctx context.Context, project, dbAlias, col string, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to delete collection/table rules as provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseRule, dbAlias, col, "rule")
	_, ok := projectConfig.DatabaseRules[resourceID]
	if !ok {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to delete collection rules as provided table or collection (%s) does not exists", col), nil, nil)
	}
	delete(projectConfig.DatabaseRules, resourceID)
	if err := s.modules.SetDatabaseRulesConfig(ctx, project, projectConfig.DatabaseRules); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set database rules config", err, nil)
	}
	if err := s.store.DeleteResource(ctx, resourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}