Skip to content

Commit

Permalink
Start of making CREATE DATABASE pluggable
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Jan 26, 2021
1 parent 627f092 commit b7400d4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func buildDBDDLPlan(stmt sqlparser.Statement, vschema ContextVSchema) (engine.Pr
if !dbDDL.IfNotExists && ksExists {
return nil, vterrors.Errorf(vtrpcpb.Code_ALREADY_EXISTS, "cannot create database '%s'; database exists", ksName)
}
return nil, vterrors.New(vtrpcpb.Code_UNIMPLEMENTED, "create database not allowed")
return databaseCreator(dbDDL, vschema)
}
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] unreachable code path: %s", sqlparser.String(dbDDLstmt))
}
Expand Down
36 changes: 36 additions & 0 deletions go/vt/vtgate/planbuilder/create_database_pluggable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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 planbuilder

import (
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
)

// PlanFunc is the function signature that you need to implement to add a custom CREATE DATABASE handler
type PlanFunc = func(stmt sqlparser.Statement, vschema ContextVSchema) (engine.Primitive, error)

var databaseCreator = defaultCreateDatabase

//goland:noinspection GoVarAndConstTypeMayBeOmitted
var _ PlanFunc = databaseCreator

func defaultCreateDatabase(stmt sqlparser.Statement, vschema ContextVSchema) (engine.Primitive, error) {
return nil, vterrors.New(vtrpcpb.Code_UNIMPLEMENTED, "create database not allowed")
}
59 changes: 59 additions & 0 deletions go/vt/vtgate/planbuilder/create_database_pluggable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
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 planbuilder

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

"vitess.io/vitess/go/vt/vtgate/vindexes"
)

func TestCreateDB(t *testing.T) {
ks := &vindexes.Keyspace{Name: "main"}
vschema := &vschemaWrapper{
v: &vindexes.VSchema{
Keyspaces: map[string]*vindexes.KeyspaceSchema{"main": {Keyspace: ks}},
},
keyspace: ks,
}

// default behaviour
_, err := TestBuilder("create database test", vschema)
require.EqualError(t, err, "create database not allowed")

// we make sure to restore the state so we don't destabilize other tests
before := databaseCreator
defer func() {
databaseCreator = before
}()

// setting custom behaviour for CREATE DATABASE
s := &engine.SingleRow{}
databaseCreator = func(stmt sqlparser.Statement, vschema ContextVSchema) (engine.Primitive, error) {
return s, nil
}

output, err := TestBuilder("create database test", vschema)
require.NoError(t, err)
assert.Same(t, s, output.Instructions)
}

0 comments on commit b7400d4

Please sign in to comment.