mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Began work on user favorites backend support & storage
This commit is contained in:
34
pkg/services/sqlstore/favorite.go
Normal file
34
pkg/services/sqlstore/favorite.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"github.com/go-xorm/xorm"
|
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", AddAsFavorite)
|
||||
bus.AddHandler("sql", RemoveAsFavorite)
|
||||
}
|
||||
|
||||
func AddAsFavorite(cmd *m.AddAsFavoriteCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
|
||||
entity := m.Favorite{
|
||||
UserId: cmd.UserId,
|
||||
DashboardId: cmd.DashboardId,
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func RemoveAsFavorite(cmd *m.RemoveAsFavoriteCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "DELETE FROM favorite WHERE user_id=? and dashboard_id=?"
|
||||
_, err := sess.Exec(rawSql, cmd.UserId, cmd.DashboardId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -6,11 +6,11 @@ import . "github.com/torkelo/grafana-pro/pkg/services/sqlstore/migrator"
|
||||
// 1. Never change a migration that is committed and pushed to master
|
||||
// 2. Always add new migrations (to change or undo previous migrations)
|
||||
// 3. Some migraitons are not yet written (rename column, table, drop table, index etc)
|
||||
// 4
|
||||
|
||||
func addMigrations(mg *Migrator) {
|
||||
addMigrationLogMigrations(mg)
|
||||
addUserMigrations(mg)
|
||||
addFavoritesMigrations(mg)
|
||||
addAccountMigrations(mg)
|
||||
addDashboardMigration(mg)
|
||||
addDataSourceMigration(mg)
|
||||
@@ -55,6 +55,18 @@ func addUserMigrations(mg *Migrator) {
|
||||
Table("user").Column(&Column{Name: "rands", Type: DB_NVarchar, Length: 255, Nullable: true}))
|
||||
}
|
||||
|
||||
func addFavoritesMigrations(mg *Migrator) {
|
||||
mg.AddMigration("create favorite table", new(AddTableMigration).
|
||||
Name("favorite").WithColumns(
|
||||
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
&Column{Name: "user_id", Type: DB_BigInt, Nullable: false},
|
||||
&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
|
||||
))
|
||||
|
||||
mg.AddMigration("add unique index favorite.user_id_dashboard_id", new(AddIndexMigration).
|
||||
Table("favorite").Columns("user_id", "dashboard_id").Unique())
|
||||
}
|
||||
|
||||
func addAccountMigrations(mg *Migrator) {
|
||||
mg.AddMigration("create account table", new(AddTableMigration).
|
||||
Name("account").WithColumns(
|
||||
|
||||
Reference in New Issue
Block a user