From f18092a336e811bbd0f5c88826211cc84c22c6d8 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 13 Mar 2025 18:19:34 -0700 Subject: [PATCH 1/2] Handle parsing a character set encoding for database creation --- server/ast/create_database.go | 11 +++-- testing/go/create_database_test.go | 65 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100755 testing/go/create_database_test.go diff --git a/server/ast/create_database.go b/server/ast/create_database.go index bd1695fdc6..df3bdc79f1 100644 --- a/server/ast/create_database.go +++ b/server/ast/create_database.go @@ -24,9 +24,8 @@ import ( // nodeCreateDatabase handles *tree.CreateDatabase nodes. func nodeCreateDatabase(ctx *Context, node *tree.CreateDatabase) (*vitess.DBDDL, error) { - if len(node.Owner) > 0 { - return nil, errors.Errorf("OWNER clause is not yet supported") - } + var charsets []*vitess.CharsetAndCollate + if len(node.Template) > 0 { // TODO: special casing "template0", as some tests make use of it and we need them to pass for now if node.Template != "template0" { @@ -34,7 +33,10 @@ func nodeCreateDatabase(ctx *Context, node *tree.CreateDatabase) (*vitess.DBDDL, } } if len(node.Encoding) > 0 { - return nil, errors.Errorf("ENCODING clause is not yet supported") + charsets = append(charsets, &vitess.CharsetAndCollate{ + Type: "CHARACTER SET", + Value: node.Encoding, + }) } if len(node.Strategy) > 0 { return nil, errors.Errorf("STRATEGY clause is not yet supported") @@ -85,5 +87,6 @@ func nodeCreateDatabase(ctx *Context, node *tree.CreateDatabase) (*vitess.DBDDL, SchemaOrDatabase: "database", DBName: node.Name.String(), IfNotExists: node.IfNotExists, + CharsetCollate: charsets, }, nil } diff --git a/testing/go/create_database_test.go b/testing/go/create_database_test.go new file mode 100755 index 0000000000..9f7eda902e --- /dev/null +++ b/testing/go/create_database_test.go @@ -0,0 +1,65 @@ +// Copyright 2025 Dolthub, Inc. +// +// 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 _go + +import "testing" + +func TestCreateDatabase(t *testing.T) { + RunScripts(t, []ScriptTest{ + { + Name: "simple create database", + Assertions: []ScriptTestAssertion{ + { + Query: "CREATE DATABASE testdb", + }, + { + Query: "USE testdb", + }, + }, + }, + { + Name: "encoding", + Assertions: []ScriptTestAssertion{ + { + Query: "CREATE DATABASE testdb encoding=utf8", + }, + { + Query: "USE testdb", + }, + { + Query: "CREATE DATABASE testdb2 encoding=latin1", + }, + { + Query: "USE testdb2", + }, + { + Query: "CREATE DATABASE testdb encoding=notexist", + ExpectedErr: "Unknown character set: notexist", + }, + }, + }, + { + Name: "multiple options", + Assertions: []ScriptTestAssertion{ + { + Query: "CREATE DATABASE testdb OWNER=foo ENCODING=utf8", + }, + { + Query: "USE testdb", + }, + }, + }, + }) +} \ No newline at end of file From aefd6ee6ed92c3db1964a6873658d2a384097825 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 13 Mar 2025 18:20:45 -0700 Subject: [PATCH 2/2] Formatting --- server/ast/create_database.go | 6 +++--- testing/go/create_database_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/ast/create_database.go b/server/ast/create_database.go index df3bdc79f1..9992ac0d3d 100644 --- a/server/ast/create_database.go +++ b/server/ast/create_database.go @@ -25,7 +25,7 @@ import ( // nodeCreateDatabase handles *tree.CreateDatabase nodes. func nodeCreateDatabase(ctx *Context, node *tree.CreateDatabase) (*vitess.DBDDL, error) { var charsets []*vitess.CharsetAndCollate - + if len(node.Template) > 0 { // TODO: special casing "template0", as some tests make use of it and we need them to pass for now if node.Template != "template0" { @@ -34,8 +34,8 @@ func nodeCreateDatabase(ctx *Context, node *tree.CreateDatabase) (*vitess.DBDDL, } if len(node.Encoding) > 0 { charsets = append(charsets, &vitess.CharsetAndCollate{ - Type: "CHARACTER SET", - Value: node.Encoding, + Type: "CHARACTER SET", + Value: node.Encoding, }) } if len(node.Strategy) > 0 { diff --git a/testing/go/create_database_test.go b/testing/go/create_database_test.go index 9f7eda902e..0041a213fc 100755 --- a/testing/go/create_database_test.go +++ b/testing/go/create_database_test.go @@ -45,7 +45,7 @@ func TestCreateDatabase(t *testing.T) { Query: "USE testdb2", }, { - Query: "CREATE DATABASE testdb encoding=notexist", + Query: "CREATE DATABASE testdb encoding=notexist", ExpectedErr: "Unknown character set: notexist", }, }, @@ -62,4 +62,4 @@ func TestCreateDatabase(t *testing.T) { }, }, }) -} \ No newline at end of file +}