summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
Diffstat (limited to 'store')
-rw-r--r--store/sql_license_store.go83
-rw-r--r--store/sql_license_store_test.go43
-rw-r--r--store/sql_store.go8
-rw-r--r--store/sql_system_store.go26
-rw-r--r--store/sql_system_store_test.go16
-rw-r--r--store/store.go9
6 files changed, 183 insertions, 2 deletions
diff --git a/store/sql_license_store.go b/store/sql_license_store.go
new file mode 100644
index 000000000..f5d67bc5d
--- /dev/null
+++ b/store/sql_license_store.go
@@ -0,0 +1,83 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package store
+
+import (
+ "github.com/mattermost/platform/model"
+)
+
+type SqlLicenseStore struct {
+ *SqlStore
+}
+
+func NewSqlLicenseStore(sqlStore *SqlStore) 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) UpgradeSchemaIfNeeded() {
+}
+
+func (ls SqlLicenseStore) CreateIndexesIfNotExists() {
+}
+
+func (ls SqlLicenseStore) Save(license *model.LicenseRecord) StoreChannel {
+
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := 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.NewLocAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error())
+ } else {
+ result.Data = license
+ }
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (ls SqlLicenseStore) Get(id string) StoreChannel {
+
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ if obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id); err != nil {
+ result.Err = model.NewLocAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error())
+ } else if obj == nil {
+ result.Err = model.NewLocAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id)
+ } else {
+ result.Data = obj.(*model.LicenseRecord)
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+
+ }()
+
+ return storeChannel
+}
diff --git a/store/sql_license_store_test.go b/store/sql_license_store_test.go
new file mode 100644
index 000000000..ad24a6af7
--- /dev/null
+++ b/store/sql_license_store_test.go
@@ -0,0 +1,43 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package store
+
+import (
+ "github.com/mattermost/platform/model"
+ "testing"
+)
+
+func TestLicenseStoreSave(t *testing.T) {
+ Setup()
+
+ l1 := model.LicenseRecord{}
+ l1.Id = model.NewId()
+ l1.Bytes = "junk"
+
+ if err := (<-store.License().Save(&l1)).Err; err != nil {
+ t.Fatal("couldn't save license record", err)
+ }
+
+ if err := (<-store.License().Save(&l1)).Err; err != nil {
+ t.Fatal("shouldn't fail on trying to save existing license record", err)
+ }
+}
+
+func TestLicenseStoreGet(t *testing.T) {
+ Setup()
+
+ l1 := model.LicenseRecord{}
+ l1.Id = model.NewId()
+ l1.Bytes = "junk"
+
+ Must(store.License().Save(&l1))
+
+ if r := <-store.License().Get(l1.Id); r.Err != nil {
+ t.Fatal("couldn't get license", r.Err)
+ } else {
+ if r.Data.(*model.LicenseRecord).Bytes != l1.Bytes {
+ t.Fatal("license bytes didn't match")
+ }
+ }
+}
diff --git a/store/sql_store.go b/store/sql_store.go
index 8517eb1a2..a994ec57e 100644
--- a/store/sql_store.go
+++ b/store/sql_store.go
@@ -49,6 +49,7 @@ type SqlStore struct {
webhook WebhookStore
command CommandStore
preference PreferenceStore
+ license LicenseStore
}
func NewSqlStore() Store {
@@ -103,6 +104,7 @@ func NewSqlStore() Store {
sqlStore.webhook = NewSqlWebhookStore(sqlStore)
sqlStore.command = NewSqlCommandStore(sqlStore)
sqlStore.preference = NewSqlPreferenceStore(sqlStore)
+ sqlStore.license = NewSqlLicenseStore(sqlStore)
err := sqlStore.master.CreateTablesIfNotExists()
if err != nil {
@@ -120,6 +122,7 @@ func NewSqlStore() Store {
sqlStore.webhook.(*SqlWebhookStore).UpgradeSchemaIfNeeded()
sqlStore.command.(*SqlCommandStore).UpgradeSchemaIfNeeded()
sqlStore.preference.(*SqlPreferenceStore).UpgradeSchemaIfNeeded()
+ sqlStore.license.(*SqlLicenseStore).UpgradeSchemaIfNeeded()
sqlStore.team.(*SqlTeamStore).CreateIndexesIfNotExists()
sqlStore.channel.(*SqlChannelStore).CreateIndexesIfNotExists()
@@ -132,6 +135,7 @@ func NewSqlStore() Store {
sqlStore.webhook.(*SqlWebhookStore).CreateIndexesIfNotExists()
sqlStore.command.(*SqlCommandStore).CreateIndexesIfNotExists()
sqlStore.preference.(*SqlPreferenceStore).CreateIndexesIfNotExists()
+ sqlStore.license.(*SqlLicenseStore).CreateIndexesIfNotExists()
sqlStore.preference.(*SqlPreferenceStore).DeleteUnusedFeatures()
@@ -523,6 +527,10 @@ func (ss SqlStore) Preference() PreferenceStore {
return ss.preference
}
+func (ss SqlStore) License() LicenseStore {
+ return ss.license
+}
+
type mattermConverter struct{}
func (me mattermConverter) ToDb(val interface{}) (interface{}, error) {
diff --git a/store/sql_system_store.go b/store/sql_system_store.go
index cfd4a670f..f8da06cec 100644
--- a/store/sql_system_store.go
+++ b/store/sql_system_store.go
@@ -1,4 +1,4 @@
-// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package store
@@ -47,6 +47,30 @@ func (s SqlSystemStore) Save(system *model.System) StoreChannel {
return storeChannel
}
+func (s SqlSystemStore) SaveOrUpdate(system *model.System) StoreChannel {
+
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ if err := s.GetReplica().SelectOne(&model.System{}, "SELECT * FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": system.Name}); err == nil {
+ if _, err := s.GetMaster().Update(system); err != nil {
+ result.Err = model.NewLocAppError("SqlSystemStore.SaveOrUpdate", "store.sql_system.update.app_error", nil, "")
+ }
+ } else {
+ if err := s.GetMaster().Insert(system); err != nil {
+ result.Err = model.NewLocAppError("SqlSystemStore.SaveOrUpdate", "store.sql_system.save.app_error", nil, "")
+ }
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (s SqlSystemStore) Update(system *model.System) StoreChannel {
storeChannel := make(StoreChannel)
diff --git a/store/sql_system_store_test.go b/store/sql_system_store_test.go
index 8ff5445cc..ce149e97a 100644
--- a/store/sql_system_store_test.go
+++ b/store/sql_system_store_test.go
@@ -31,3 +31,19 @@ func TestSqlSystemStore(t *testing.T) {
t.Fatal()
}
}
+
+func TestSqlSystemStoreSaveOrUpdate(t *testing.T) {
+ Setup()
+
+ system := &model.System{Name: model.NewId(), Value: "value"}
+
+ if err := (<-store.System().SaveOrUpdate(system)).Err; err != nil {
+ t.Fatal(err)
+ }
+
+ system.Value = "value2"
+
+ if r := <-store.System().SaveOrUpdate(system); r.Err != nil {
+ t.Fatal(r.Err)
+ }
+}
diff --git a/store/store.go b/store/store.go
index b6b86e0d9..952b96e87 100644
--- a/store/store.go
+++ b/store/store.go
@@ -1,4 +1,4 @@
-// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package store
@@ -39,6 +39,7 @@ type Store interface {
Webhook() WebhookStore
Command() CommandStore
Preference() PreferenceStore
+ License() LicenseStore
MarkSystemRanUnitTests()
Close()
}
@@ -164,6 +165,7 @@ type OAuthStore interface {
type SystemStore interface {
Save(system *model.System) StoreChannel
+ SaveOrUpdate(system *model.System) StoreChannel
Update(system *model.System) StoreChannel
Get() StoreChannel
}
@@ -203,3 +205,8 @@ type PreferenceStore interface {
PermanentDeleteByUser(userId string) StoreChannel
IsFeatureEnabled(feature, userId string) StoreChannel
}
+
+type LicenseStore interface {
+ Save(license *model.LicenseRecord) StoreChannel
+ Get(id string) StoreChannel
+}