Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vttest create db #7989

Merged
merged 7 commits into from
Apr 29, 2021
29 changes: 28 additions & 1 deletion go/cmd/vtcombo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"strings"
"time"

"vitess.io/vitess/go/vt/wrangler"

"context"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -158,7 +160,8 @@ func main() {

// tablets configuration and init.
// Send mycnf as nil because vtcombo won't do backups and restores.
if err := vtcombo.InitTabletMap(ts, tpb, mysqld, &dbconfigs.GlobalDBConfigs, *schemaDir, nil, *startMysql); err != nil {
uid, err := vtcombo.InitTabletMap(ts, tpb, mysqld, &dbconfigs.GlobalDBConfigs, *schemaDir, *startMysql)
if err != nil {
log.Errorf("initTabletMapProto failed: %v", err)
// ensure we start mysql in the event we fail here
if *startMysql {
Expand All @@ -167,6 +170,30 @@ func main() {
exit.Return(1)
}

globalCreateDb = func(ctx context.Context, ks *vttestpb.Keyspace) error {
wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil)
newUID, err := vtcombo.CreateKs(ctx, ts, tpb, mysqld, &dbconfigs.GlobalDBConfigs, *schemaDir, ks, true, uid, wr)
if err != nil {
return err
}
uid = newUID
tpb.Keyspaces = append(tpb.Keyspaces, ks)
return nil
}

globalDropDb = func(ctx context.Context, ksName string) error {
if err := vtcombo.DeleteKs(ctx, ts, ksName, mysqld, tpb); err != nil {
return err
}

// Rebuild the SrvVSchema object
if err := ts.RebuildSrvVSchema(ctx, tpb.Cells); err != nil {
return err
}

return nil
}

// Now that we have fully initialized the tablets, rebuild the keyspace graph.
for _, ks := range tpb.Keyspaces {
err := topotools.RebuildKeyspace(context.Background(), logutil.NewConsoleLogger(), ts, ks.GetName(), tpb.Cells, false)
Expand Down
54 changes: 54 additions & 0 deletions go/cmd/vtcombo/plugin_dbddl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2021 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"

"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtgate/engine"

vttestpb "vitess.io/vitess/go/vt/proto/vttest"
)

var globalCreateDb func(ctx context.Context, ks *vttestpb.Keyspace) error
var globalDropDb func(ctx context.Context, ksName string) error

// DBDDL doesn't need to store any state - we use the global variables above instead
type DBDDL struct{}

// CreateDatabase implements the engine.DBDDLPlugin interface
func (plugin *DBDDL) CreateDatabase(ctx context.Context, name string) error {
ks := &vttestpb.Keyspace{
Name: name,
Shards: []*vttestpb.Shard{{
Name: "0",
}},
}
return globalCreateDb(ctx, ks)
}

// DropDatabase implements the engine.DBDDLPlugin interface
func (plugin *DBDDL) DropDatabase(ctx context.Context, name string) error {
return globalDropDb(ctx, name)
}

func init() {
servenv.OnRun(func() {
engine.DBDDLRegister("vttest", &DBDDL{})
})
}
Loading