mirror of
https://github.com/grafana/grafana.git
synced 2025-08-06 20:20:23 +08:00
Cloudmigration: create migration (#85386)
* Cloudmigration: create migration * fix drone build * lint fix * fix unit test --------- Co-authored-by: Michael Mandrus <michael.mandrus@grafana.com>
This commit is contained in:
@ -236,7 +236,8 @@ func (s *Service) CreateMigration(ctx context.Context, cmd cloudmigration.CloudM
|
||||
return nil, fmt.Errorf("invalid token") // don't want to leak info here
|
||||
}
|
||||
|
||||
if err := s.store.CreateMigration(ctx, token); err != nil {
|
||||
migration := token.ToMigration()
|
||||
if err := s.store.CreateMigration(ctx, migration); err != nil {
|
||||
return nil, fmt.Errorf("error creating migration: %w", err)
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,6 @@ import (
|
||||
|
||||
type store interface {
|
||||
MigrateDatasources(context.Context, *cloudmigration.MigrateDatasourcesRequest) (*cloudmigration.MigrateDatasourcesResponse, error)
|
||||
CreateMigration(ctx context.Context, token cloudmigration.Base64EncodedTokenPayload) error
|
||||
CreateMigration(ctx context.Context, token cloudmigration.CloudMigration) error
|
||||
GetAllCloudMigrations(ctx context.Context) ([]*cloudmigration.CloudMigration, error)
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ package cloudmigrationimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration"
|
||||
@ -15,7 +18,19 @@ func (ss *sqlStore) MigrateDatasources(ctx context.Context, request *cloudmigrat
|
||||
return nil, cloudmigration.ErrInternalNotImplementedError
|
||||
}
|
||||
|
||||
func (ss *sqlStore) CreateMigration(ctx context.Context, token cloudmigration.Base64EncodedTokenPayload) error {
|
||||
func (ss *sqlStore) CreateMigration(ctx context.Context, migration cloudmigration.CloudMigration) error {
|
||||
err := ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
migration.Created = time.Now()
|
||||
migration.Updated = time.Now()
|
||||
_, err := sess.Insert(migration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/bmizerany/assert"
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/tests/testsuite"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -26,10 +27,10 @@ func TestGetAllCloudMigrations(t *testing.T) {
|
||||
t.Run("get all cloud_migrations", func(t *testing.T) {
|
||||
// replace this with proper method when created
|
||||
_, err := testDB.GetSqlxSession().Exec(ctx, `
|
||||
INSERT INTO cloud_migration (id, auth_token, stack, created, updated)
|
||||
VALUES (1, '12345', 'stack1', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000'),
|
||||
(2, '6789', 'stack2', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000'),
|
||||
(3, '777', 'stack3', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000');
|
||||
INSERT INTO cloud_migration (id, auth_token, stack, stack_id, region_slug, cluster_slug, created, updated)
|
||||
VALUES (1, '12345', '11111', 11111, 'test', 'test', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000'),
|
||||
(2, '6789', '22222', 22222, 'test', 'test', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000'),
|
||||
(3, '777', '33333', 33333, 'test', 'test', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000');
|
||||
`)
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -39,14 +40,14 @@ func TestGetAllCloudMigrations(t *testing.T) {
|
||||
for _, m := range value {
|
||||
switch m.ID {
|
||||
case 1:
|
||||
require.Equal(t, "stack1", m.Stack)
|
||||
require.Equal(t, "12345", m.AuthToken)
|
||||
assert.Equal(t, "11111", m.Stack)
|
||||
assert.Equal(t, "12345", m.AuthToken)
|
||||
case 2:
|
||||
require.Equal(t, "stack2", m.Stack)
|
||||
require.Equal(t, "6789", m.AuthToken)
|
||||
assert.Equal(t, "22222", m.Stack)
|
||||
assert.Equal(t, "6789", m.AuthToken)
|
||||
case 3:
|
||||
require.Equal(t, "stack3", m.Stack)
|
||||
require.Equal(t, "777", m.AuthToken)
|
||||
assert.Equal(t, "33333", m.Stack)
|
||||
assert.Equal(t, "777", m.AuthToken)
|
||||
default:
|
||||
require.Fail(t, "ID value not expected: "+strconv.FormatInt(m.ID, 10))
|
||||
}
|
||||
|
@ -12,11 +12,14 @@ var (
|
||||
)
|
||||
|
||||
type CloudMigration struct {
|
||||
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
||||
AuthToken string `json:"authToken"`
|
||||
Stack string `json:"stack"`
|
||||
Created time.Time `json:"created"`
|
||||
Updated time.Time `json:"updated"`
|
||||
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
||||
AuthToken string `json:"authToken"`
|
||||
Stack string `json:"stack"`
|
||||
StackID int `json:"stackID" xorm:"stack_id"`
|
||||
RegionSlug string `json:"regionSlug"`
|
||||
ClusterSlug string `json:"clusterSlug"`
|
||||
Created time.Time `json:"created"`
|
||||
Updated time.Time `json:"updated"`
|
||||
}
|
||||
|
||||
type MigratedResourceResult struct {
|
||||
@ -93,6 +96,16 @@ type Base64EncodedTokenPayload struct {
|
||||
Instance Base64HGInstance
|
||||
}
|
||||
|
||||
func (p Base64EncodedTokenPayload) ToMigration() CloudMigration {
|
||||
return CloudMigration{
|
||||
AuthToken: p.Token,
|
||||
Stack: p.Instance.Slug,
|
||||
StackID: p.Instance.StackID,
|
||||
RegionSlug: p.Instance.RegionSlug,
|
||||
ClusterSlug: p.Instance.ClusterSlug,
|
||||
}
|
||||
}
|
||||
|
||||
type Base64HGInstance struct {
|
||||
StackID int
|
||||
Slug string
|
||||
|
@ -29,4 +29,12 @@ func addCloudMigrationsMigrations(mg *Migrator) {
|
||||
|
||||
mg.AddMigration("create cloud_migration table v1", NewAddTableMigration(migrationTable))
|
||||
mg.AddMigration("create cloud_migration_run table v1", NewAddTableMigration(migrationRunTable))
|
||||
|
||||
stackIDColumn := Column{Name: "stack_id", Type: DB_BigInt, Nullable: false}
|
||||
regionSlugColumn := Column{Name: "region_slug", Type: DB_Text, Nullable: false}
|
||||
clusterSlugColumn := Column{Name: "cluster_slug", Type: DB_Text, Nullable: false}
|
||||
|
||||
mg.AddMigration("add stack_id column", NewAddColumnMigration(migrationTable, &stackIDColumn))
|
||||
mg.AddMigration("add region_slug column", NewAddColumnMigration(migrationTable, ®ionSlugColumn))
|
||||
mg.AddMigration("add cluster_slug column", NewAddColumnMigration(migrationTable, &clusterSlugColumn))
|
||||
}
|
||||
|
Reference in New Issue
Block a user