Skip to content

Commit e24900c

Browse files
authored
Merge pull request #3217 from dolthub/elian/dbvr-tests
Add `CREATE DATABASE` fall back on `CREATE SCHEMA` to mirror MySQL
2 parents 725f43c + cbafdb0 commit e24900c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

enginetest/queries/script_queries.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13519,6 +13519,52 @@ var BrokenScriptTests = []ScriptTest{
1351913519
}
1352013520

1352113521
var CreateDatabaseScripts = []ScriptTest{
13522+
{
13523+
// https://github.com/dolthub/dolt/pull/9830
13524+
Name: "CREATE SCHEMA without database selection falls back to CREATE DATABASE",
13525+
SetUpScript: []string{
13526+
"CREATE DATABASE tmp",
13527+
"USE tmp",
13528+
},
13529+
Dialect: "mysql",
13530+
Assertions: []ScriptTestAssertion{
13531+
{
13532+
Query: "DROP DATABASE tmp",
13533+
},
13534+
{
13535+
Query: "CREATE SCHEMA NewDatabase",
13536+
Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}},
13537+
},
13538+
{
13539+
Query: "SHOW DATABASES",
13540+
Expected: []sql.Row{{"NewDatabase"}, {"information_schema"}, {"mydb"}, {"mysql"}},
13541+
},
13542+
{
13543+
Query: "USE NewDatabase",
13544+
Expected: []sql.Row{},
13545+
},
13546+
{
13547+
Query: "SELECT DATABASE()",
13548+
Expected: []sql.Row{{"NewDatabase"}},
13549+
},
13550+
{
13551+
Query: "CREATE TABLE test_table (id INT PRIMARY KEY)",
13552+
Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}},
13553+
},
13554+
{
13555+
Query: "SHOW TABLES",
13556+
Expected: []sql.Row{{"test_table"}},
13557+
},
13558+
{
13559+
Query: "USE mydb",
13560+
Expected: []sql.Row{},
13561+
},
13562+
{
13563+
Query: "DROP DATABASE NewDatabase",
13564+
Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}},
13565+
},
13566+
},
13567+
},
1352213568
{
1352313569
Name: "CREATE DATABASE and create table",
1352413570
Assertions: []ScriptTestAssertion{

sql/rowexec/ddl.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,18 @@ func (b *BaseBuilder) buildCreateDB(ctx *sql.Context, n *plan.CreateDB, row sql.
405405

406406
func (b *BaseBuilder) buildCreateSchema(ctx *sql.Context, n *plan.CreateSchema, row sql.Row) (sql.RowIter, error) {
407407
database := ctx.GetCurrentDatabase()
408+
409+
// If no database is selected, first try to fall back to CREATE DATABASE
410+
// since CREATE SCHEMA is a synonym for CREATE DATABASE in MySQL
411+
// https://dev.mysql.com/doc/refman/8.4/en/create-database.html
412+
// TODO: For PostgreSQL, return an error if no database is selected.
408413
if database == "" {
409-
return nil, sql.ErrNoDatabaseSelected.New()
414+
return b.buildCreateDB(ctx, &plan.CreateDB{
415+
Catalog: n.Catalog,
416+
DbName: n.DbName,
417+
IfNotExists: n.IfNotExists,
418+
Collation: n.Collation,
419+
}, row)
410420
}
411421

412422
db, err := n.Catalog.Database(ctx, database)

0 commit comments

Comments
 (0)