diff options
Diffstat (limited to 'store/sqlstore/license_store.go')
-rw-r--r-- | store/sqlstore/license_store.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/store/sqlstore/license_store.go b/store/sqlstore/license_store.go new file mode 100644 index 000000000..5f3e91e88 --- /dev/null +++ b/store/sqlstore/license_store.go @@ -0,0 +1,83 @@ +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package sqlstore + +import ( + "net/http" + + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/store" +) + +type SqlLicenseStore struct { + SqlStore +} + +func NewSqlLicenseStore(sqlStore SqlStore) store.LicenseStore { + ls := &SqlLicenseStore{sqlStore} + + for _, db := range sqlStore.GetAllConns() { + table := db.AddTableWithName(model.LicenseRecord{}, "Licenses").SetKeys(false, "Id") + table.ColMap("Id").SetMaxSize(26) + table.ColMap("Bytes").SetMaxSize(10000) + } + + return ls +} + +func (ls SqlLicenseStore) CreateIndexesIfNotExists() { +} + +func (ls SqlLicenseStore) Save(license *model.LicenseRecord) store.StoreChannel { + + storeChannel := make(store.StoreChannel, 1) + + go func() { + result := store.StoreResult{} + + license.PreSave() + if result.Err = license.IsValid(); result.Err != nil { + storeChannel <- result + close(storeChannel) + return + } + + // Only insert if not exists + if err := ls.GetReplica().SelectOne(&model.LicenseRecord{}, "SELECT * FROM Licenses WHERE Id = :Id", map[string]interface{}{"Id": license.Id}); err != nil { + if err := ls.GetMaster().Insert(license); err != nil { + result.Err = model.NewAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error(), http.StatusInternalServerError) + } else { + result.Data = license + } + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (ls SqlLicenseStore) Get(id string) store.StoreChannel { + + storeChannel := make(store.StoreChannel, 1) + + go func() { + result := store.StoreResult{} + + if obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id); err != nil { + result.Err = model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError) + } else if obj == nil { + result.Err = model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id, http.StatusNotFound) + } else { + result.Data = obj.(*model.LicenseRecord) + } + + storeChannel <- result + close(storeChannel) + + }() + + return storeChannel +} |