From 562a1c36b15695d55517edef73dd20dcb0d75234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Thu, 3 Dec 2020 15:18:10 +0100 Subject: [PATCH] PanelLibrary: Adds library_panel table (#29565) * PanelLibrary: Adds panellib table * Refactor: removes drop table migration * Refactor: fixes spelling mistake * Refactor: changes after PR comments * Refactor: some more renames --- pkg/server/server.go | 1 + pkg/services/librarypanels/librarypanels.go | 59 +++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 pkg/services/librarypanels/librarypanels.go diff --git a/pkg/server/server.go b/pkg/server/server.go index 7372a329a7b..6fd28aa63e9 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -35,6 +35,7 @@ import ( _ "github.com/grafana/grafana/pkg/services/alerting" _ "github.com/grafana/grafana/pkg/services/auth" _ "github.com/grafana/grafana/pkg/services/cleanup" + _ "github.com/grafana/grafana/pkg/services/librarypanels" _ "github.com/grafana/grafana/pkg/services/ngalert" _ "github.com/grafana/grafana/pkg/services/notifications" _ "github.com/grafana/grafana/pkg/services/provisioning" diff --git a/pkg/services/librarypanels/librarypanels.go b/pkg/services/librarypanels/librarypanels.go new file mode 100644 index 00000000000..d48132444e1 --- /dev/null +++ b/pkg/services/librarypanels/librarypanels.go @@ -0,0 +1,59 @@ +package librarypanels + +import ( + "github.com/grafana/grafana/pkg/infra/log" + "github.com/grafana/grafana/pkg/registry" + "github.com/grafana/grafana/pkg/services/sqlstore/migrator" + "github.com/grafana/grafana/pkg/setting" +) + +// LibraryPanelService is the service for the Panel Library feature. +type LibraryPanelService struct { + Cfg *setting.Cfg `inject:""` + log log.Logger +} + +func init() { + registry.RegisterService(&LibraryPanelService{}) +} + +// Init initializes the LibraryPanel service +func (pl *LibraryPanelService) Init() error { + pl.log = log.New("library_panel") + + return nil +} + +// IsEnabled returns true if the Panel Library feature is enabled for this instance. +func (pl *LibraryPanelService) IsEnabled() bool { + if pl.Cfg == nil { + return false + } + + return pl.Cfg.IsPanelLibraryEnabled() +} + +// AddMigration defines database migrations. +// If Panel Library is not enabled does nothing. +func (pl *LibraryPanelService) AddMigration(mg *migrator.Migrator) { + if !pl.IsEnabled() { + return + } + + libraryPanelV1 := migrator.Table{ + Name: "library_panel", + Columns: []*migrator.Column{ + {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, + {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false}, + {Name: "folder_id", Type: migrator.DB_BigInt, Nullable: false}, + {Name: "title", Type: migrator.DB_NVarchar, Length: 255, Nullable: false}, + {Name: "data", Type: migrator.DB_Text, Nullable: false}, + {Name: "created", Type: migrator.DB_DateTime, Nullable: false}, + {Name: "created_by", Type: migrator.DB_BigInt, Nullable: false}, + {Name: "updated", Type: migrator.DB_DateTime, Nullable: false}, + {Name: "updated_by", Type: migrator.DB_BigInt, Nullable: false}, + }, + } + + mg.AddMigration("create library_panel table v1", migrator.NewAddTableMigration(libraryPanelV1)) +}