Migrate all orphaned resources to DefaultOrg

When users upgrade, all of their bolt resources will not belong to any
organization. This PR introduces a migration path where any orphaned,
resources without an organization, will become owned by the default
organization.
pull/10616/head
Michael Desa 2017-11-07 17:25:44 -05:00
parent 865715516b
commit 3af66d98ab
5 changed files with 111 additions and 3 deletions

View File

@ -83,10 +83,19 @@ func (c *Client) Open(ctx context.Context) error {
}
// Runtime migrations
if err := c.DashboardsStore.Migrate(ctx); err != nil {
if err := c.OrganizationsStore.Migrate(ctx); err != nil {
return err
}
if err := c.OrganizationsStore.Migrate(ctx); err != nil {
if err := c.SourcesStore.Migrate(ctx); err != nil {
return err
}
if err := c.ServersStore.Migrate(ctx); err != nil {
return err
}
if err := c.LayoutsStore.Migrate(ctx); err != nil {
return err
}
if err := c.DashboardsStore.Migrate(ctx); err != nil {
return err
}

View File

@ -2,6 +2,7 @@ package bolt
import (
"context"
"fmt"
"strconv"
"github.com/boltdb/bolt"
@ -54,7 +55,27 @@ func (d *DashboardsStore) Migrate(ctx context.Context) error {
if err != nil {
return err
}
return d.AddIDs(ctx, boards)
if err := d.AddIDs(ctx, boards); err != nil {
return nil
}
defaultOrg, err := d.client.OrganizationsStore.DefaultOrganization(ctx)
if err != nil {
return err
}
defaultOrgID := fmt.Sprintf("%d", defaultOrg.ID)
for _, board := range boards {
if board.Organization == "" {
board.Organization = defaultOrgID
if err := d.Update(ctx, board); err != nil {
return nil
}
}
}
return nil
}
// All returns all known dashboards

View File

@ -2,6 +2,7 @@ package bolt
import (
"context"
"fmt"
"github.com/boltdb/bolt"
"github.com/influxdata/chronograf"
@ -20,6 +21,31 @@ type LayoutsStore struct {
IDs chronograf.ID
}
func (s *LayoutsStore) Migrate(ctx context.Context) error {
layouts, err := s.All(ctx)
if err != nil {
return err
}
defaultOrg, err := s.client.OrganizationsStore.DefaultOrganization(ctx)
if err != nil {
return err
}
defaultOrgID := fmt.Sprintf("%d", defaultOrg.ID)
for _, layout := range layouts {
if layout.Organization == "" {
layout.Organization = defaultOrgID
if err := s.Update(ctx, layout); err != nil {
return nil
}
}
}
return nil
}
// All returns all known layouts
func (s *LayoutsStore) All(ctx context.Context) ([]chronograf.Layout, error) {
var srcs []chronograf.Layout

View File

@ -2,6 +2,7 @@ package bolt
import (
"context"
"fmt"
"github.com/boltdb/bolt"
"github.com/influxdata/chronograf"
@ -20,6 +21,31 @@ type ServersStore struct {
client *Client
}
func (s *ServersStore) Migrate(ctx context.Context) error {
servers, err := s.All(ctx)
if err != nil {
return err
}
defaultOrg, err := s.client.OrganizationsStore.DefaultOrganization(ctx)
if err != nil {
return err
}
defaultOrgID := fmt.Sprintf("%d", defaultOrg.ID)
for _, server := range servers {
if server.Organization == "" {
server.Organization = defaultOrgID
if err := s.Update(ctx, server); err != nil {
return nil
}
}
}
return nil
}
// All returns all known servers
func (s *ServersStore) All(ctx context.Context) ([]chronograf.Server, error) {
var srcs []chronograf.Server

View File

@ -2,6 +2,7 @@ package bolt
import (
"context"
"fmt"
"github.com/boltdb/bolt"
"github.com/influxdata/chronograf"
@ -19,6 +20,31 @@ type SourcesStore struct {
client *Client
}
func (s *SourcesStore) Migrate(ctx context.Context) error {
sources, err := s.All(ctx)
if err != nil {
return err
}
defaultOrg, err := s.client.OrganizationsStore.DefaultOrganization(ctx)
if err != nil {
return err
}
defaultOrgID := fmt.Sprintf("%d", defaultOrg.ID)
for _, source := range sources {
if source.Organization == "" {
source.Organization = defaultOrgID
if err := s.Update(ctx, source); err != nil {
return nil
}
}
}
return nil
}
// All returns all known sources
func (s *SourcesStore) All(ctx context.Context) ([]chronograf.Source, error) {
var srcs []chronograf.Source