Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

2.9 KB
Raw Blame
package mariadb

import (
	"context"
	"io"
	"math"
	"os"
	"testing"

	"github.com/stretchr/testify/require"
	"gopkg.in/yaml.v3"

	"github.com/bytebase/bytebase/backend/common/yamltest"
	"github.com/bytebase/bytebase/backend/generated-go/store"
	"github.com/bytebase/bytebase/backend/plugin/parser/base"
)

type restoreCase struct {
	Input            string
	BackupDatabase   string
	BackupTable      string
	OriginalDatabase string
	OriginalTable    string
	Result           string
}

func TestRestore(t *testing.T) {
	tests := []restoreCase{}

	const (
		record = false
	)
	var (
		filepath = "test-data/test_restore.yaml"
	)

	a := require.New(t)
	yamlFile, err := os.Open(filepath)
	a.NoError(err)

	byteValue, err := io.ReadAll(yamlFile)
	a.NoError(yamlFile.Close())
	a.NoError(err)
	a.NoError(yaml.Unmarshal(byteValue, &tests))

	for _, databasePrefix := range []string{"instances/i1/databases/", "projects/project-a/instances/i1/databases/"} {
		for i, t := range tests {
			getter, lister := buildFixedMockDatabaseMetadataGetterAndLister()
			result, err := GenerateRestoreSQL(context.Background(), base.RestoreContext{
				GetDatabaseMetadataFunc: getter,
				ListDatabaseNamesFunc:   lister,
				IsCaseSensitive:         false,
			}, t.Input, &store.PriorBackupDetail_Item{
				SourceTable: &store.PriorBackupDetail_Item_Table{
					Database: databasePrefix + t.OriginalDatabase,
					Table:    t.OriginalTable,
				},
				TargetTable: &store.PriorBackupDetail_Item_Table{
					Database: databasePrefix + t.BackupDatabase,
					Table:    t.BackupTable,
				},
				StartPosition: &store.Position{
					Line:   0,
					Column: 0,
				},
				EndPosition: &store.Position{
					Line:   math.MaxInt32,
					Column: 0,
				},
			})
			a.NoError(err)

			if record {
				tests[i].Result = result
			} else {
				a.Equal(t.Result, result, t.Input)
			}
		}
	}
	if record {
		yamltest.Record(t, filepath, tests)
	}
}

func TestMariaDBGenerateRestoreSQLRegistration(t *testing.T) {
	getter, lister := buildFixedMockDatabaseMetadataGetterAndLister()

	result, err := base.GenerateRestoreSQL(context.Background(), store.Engine_MARIADB, base.RestoreContext{
		GetDatabaseMetadataFunc: getter,
		ListDatabaseNamesFunc:   lister,
		IsCaseSensitive:         false,
	}, "DELETE FROM test WHERE b1 = 1;", &store.PriorBackupDetail_Item{
		SourceTable: &store.PriorBackupDetail_Item_Table{
			Database: "instances/i1/databases/db",
			Table:    "test",
		},
		TargetTable: &store.PriorBackupDetail_Item_Table{
			Database: "instances/i1/databases/bbarchive",
			Table:    "prefix_test",
		},
		StartPosition: &store.Position{
			Line:   0,
			Column: 0,
		},
		EndPosition: &store.Position{
			Line:   math.MaxInt32,
			Column: 0,
		},
	})

	require.NoError(t, err)
	require.Equal(t, "/*\nOriginal SQL:\nDELETE FROM test WHERE b1 = 1;\n*/\nINSERT INTO `db`.`test` (`a`, `b`, `c`) SELECT `a`, `b`, `c` FROM `bbarchive`.`prefix_test`;", result)
}