mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 12:23:10 +08:00
fix: prevent datasource json data stored as nil (#15508)
prevent datasource json data stored as nil closes #14239
This commit is contained in:

committed by
Carl Bergquist

parent
06de01dc23
commit
d49f0bedd3
@ -3,6 +3,8 @@ package sqlstore
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
|
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
@ -95,6 +97,10 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
|
|||||||
return m.ErrDataSourceNameExists
|
return m.ErrDataSourceNameExists
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cmd.JsonData == nil {
|
||||||
|
cmd.JsonData = simplejson.New()
|
||||||
|
}
|
||||||
|
|
||||||
ds := &m.DataSource{
|
ds := &m.DataSource{
|
||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
Name: cmd.Name,
|
Name: cmd.Name,
|
||||||
@ -142,6 +148,10 @@ func updateIsDefaultFlag(ds *m.DataSource, sess *DBSession) error {
|
|||||||
|
|
||||||
func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
|
func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
|
||||||
return inTransaction(func(sess *DBSession) error {
|
return inTransaction(func(sess *DBSession) error {
|
||||||
|
if cmd.JsonData == nil {
|
||||||
|
cmd.JsonData = simplejson.New()
|
||||||
|
}
|
||||||
|
|
||||||
ds := &m.DataSource{
|
ds := &m.DataSource{
|
||||||
Id: cmd.Id,
|
Id: cmd.Id,
|
||||||
OrgId: cmd.OrgId,
|
OrgId: cmd.OrgId,
|
||||||
|
@ -130,4 +130,7 @@ func addDataSourceMigration(mg *Migrator) {
|
|||||||
|
|
||||||
const migrateLoggingToLoki = `UPDATE data_source SET type = 'loki' WHERE type = 'logging'`
|
const migrateLoggingToLoki = `UPDATE data_source SET type = 'loki' WHERE type = 'logging'`
|
||||||
mg.AddMigration("Migrate logging ds to loki ds", NewRawSqlMigration(migrateLoggingToLoki))
|
mg.AddMigration("Migrate logging ds to loki ds", NewRawSqlMigration(migrateLoggingToLoki))
|
||||||
|
|
||||||
|
const setEmptyJSONWhereNullJSON = `UPDATE data_source SET json_data = '{}' WHERE json_data is null`
|
||||||
|
mg.AddMigration("Update json_data with nulls", NewRawSqlMigration(setEmptyJSONWhereNullJSON))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user