-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start of making CREATE DATABASE pluggable
Signed-off-by: Andres Taylor <[email protected]>
- Loading branch information
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
go/vt/vtgate/planbuilder/create_database_pluggable_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |