-
-
Notifications
You must be signed in to change notification settings - Fork 190
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
Vinai/create database #298
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3d2bdde
basic parsing
VinaiRachakonda 3424aee
Dolt can now see what's going on
VinaiRachakonda 844ec4b
Add Catalog
VinaiRachakonda eb6c89f
Basic usage of drop database
VinaiRachakonda d6d1914
Add if exists features
VinaiRachakonda d27c033
Final changes
VinaiRachakonda 6562d0a
Cleanup
VinaiRachakonda d7c178c
Fixes
VinaiRachakonda f7447c6
Pr review
VinaiRachakonda e6aa2b7
fix problem with gomod
VinaiRachakonda 7dffe2b
Final changes
VinaiRachakonda 76e1155
fix tests
VinaiRachakonda 3cf6f81
Fix
VinaiRachakonda 4889b83
Table fix
VinaiRachakonda 08739be
nits
VinaiRachakonda c2f8170
Nits
VinaiRachakonda f88b961
Merge master, bump vitess
VinaiRachakonda 9a29d25
Additional fixes
VinaiRachakonda 6846595
fmt
VinaiRachakonda File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import ( | |
"github.com/dolthub/go-mysql-server/sql/parse" | ||
"github.com/dolthub/go-mysql-server/sql/plan" | ||
"github.com/dolthub/go-mysql-server/test" | ||
"github.com/dolthub/vitess/go/mysql" | ||
) | ||
|
||
// Tests a variety of queries against databases and tables provided by the given harness. | ||
|
@@ -1736,6 +1737,198 @@ func TestDropColumn(t *testing.T, harness Harness) { | |
require.True(sql.ErrTableColumnNotFound.Is(err)) | ||
} | ||
|
||
func TestCreateDatabase(t *testing.T, harness Harness) { | ||
e := NewEngine(t, harness) | ||
ctx := NewContext(harness) | ||
|
||
t.Run("CREATE DATABASE and create table", func(t *testing.T) { | ||
TestQuery(t, harness, e, | ||
"CREATE DATABASE testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
db, err := e.Catalog.Database("testdb") | ||
require.NoError(t, err) | ||
|
||
TestQuery(t, harness, e, | ||
"USE testdb", | ||
[]sql.Row(nil), | ||
nil, | ||
) | ||
|
||
require.Equal(t, ctx.GetCurrentDatabase(), "testdb") | ||
|
||
ctx = NewContext(harness) | ||
TestQuery(t, harness, e, | ||
"CREATE TABLE test (pk int primary key)", | ||
[]sql.Row(nil), | ||
nil, | ||
) | ||
|
||
db, err = e.Catalog.Database("testdb") | ||
require.NoError(t, err) | ||
|
||
_, ok, err := db.GetTableInsensitive(ctx, "test") | ||
|
||
require.NoError(t, err) | ||
require.True(t, ok) | ||
}) | ||
|
||
t.Run("CREATE DATABASE IF NOT EXISTS", func(t *testing.T) { | ||
TestQuery(t, harness, e, | ||
"CREATE DATABASE IF NOT EXISTS testdb2", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
db, err := e.Catalog.Database("testdb2") | ||
require.NoError(t, err) | ||
|
||
TestQuery(t, harness, e, | ||
"USE testdb2", | ||
[]sql.Row(nil), | ||
nil, | ||
) | ||
|
||
require.Equal(t, ctx.GetCurrentDatabase(), "testdb2") | ||
|
||
ctx = NewContext(harness) | ||
TestQuery(t, harness, e, | ||
"CREATE TABLE test (pk int primary key)", | ||
[]sql.Row(nil), | ||
nil, | ||
) | ||
|
||
db, err = e.Catalog.Database("testdb2") | ||
require.NoError(t, err) | ||
|
||
_, ok, err := db.GetTableInsensitive(ctx, "test") | ||
|
||
require.NoError(t, err) | ||
require.True(t, ok) | ||
}) | ||
|
||
t.Run("CREATE SCHEMA", func(t *testing.T) { | ||
TestQuery(t, harness, e, | ||
"CREATE SCHEMA testdb3", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
db, err := e.Catalog.Database("testdb3") | ||
require.NoError(t, err) | ||
|
||
TestQuery(t, harness, e, | ||
"USE testdb3", | ||
[]sql.Row(nil), | ||
nil, | ||
) | ||
|
||
require.Equal(t, ctx.GetCurrentDatabase(), "testdb3") | ||
|
||
ctx = NewContext(harness) | ||
TestQuery(t, harness, e, | ||
"CREATE TABLE test (pk int primary key)", | ||
[]sql.Row(nil), | ||
nil, | ||
) | ||
|
||
db, err = e.Catalog.Database("testdb3") | ||
require.NoError(t, err) | ||
|
||
_, ok, err := db.GetTableInsensitive(ctx, "test") | ||
|
||
require.NoError(t, err) | ||
require.True(t, ok) | ||
}) | ||
|
||
t.Run("CREATE DATABASE error handling", func(t *testing.T) { | ||
AssertErr(t, e, harness, "CREATE DATABASE mydb", sql.ErrCannotCreateDatabaseExists) | ||
|
||
AssertWarning(t, e, harness, "CREATE DATABASE IF NOT EXISTS mydb", mysql.ERDbCreateExists) | ||
}) | ||
} | ||
|
||
func TestDropDatabase(t *testing.T, harness Harness) { | ||
e := NewEngine(t, harness) | ||
|
||
t.Run("DROP DATABASE correctly works", func(t *testing.T) { | ||
TestQuery(t, harness, e, | ||
"DROP DATABASE mydb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
_, err := e.Catalog.Database("mydb") | ||
require.Error(t, err) | ||
|
||
// TODO: Deal with handling this error. | ||
//AssertErr(t, e, harness, "SHOW TABLES", sql.ErrNoDatabaseSelected) | ||
}) | ||
|
||
t.Run("DROP DATABASE works on newly created databases.", func(t *testing.T) { | ||
TestQuery(t, harness, e, | ||
"CREATE DATABASE testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
_, err := e.Catalog.Database("testdb") | ||
require.NoError(t, err) | ||
|
||
TestQuery(t, harness, e, | ||
"DROP DATABASE testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
AssertErr(t, e, harness, "USE testdb", sql.ErrDatabaseNotFound) | ||
}) | ||
|
||
t.Run("DROP SCHEMA works on newly created databases.", func(t *testing.T) { | ||
TestQuery(t, harness, e, | ||
"CREATE SCHEMA testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
_, err := e.Catalog.Database("testdb") | ||
require.NoError(t, err) | ||
|
||
TestQuery(t, harness, e, | ||
"DROP SCHEMA testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
AssertErr(t, e, harness, "USE testdb", sql.ErrDatabaseNotFound) | ||
}) | ||
|
||
t.Run("DROP DATABASE IF EXISTS correctly works.", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to also do this on a database that doesn't exist and assert no err |
||
AssertWarning(t, e, harness, "DROP DATABASE IF EXISTS mydb", mysql.ERDbDropExists) | ||
|
||
TestQuery(t, harness, e, | ||
"CREATE DATABASE testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
_, err := e.Catalog.Database("testdb") | ||
require.NoError(t, err) | ||
|
||
TestQuery(t, harness, e, | ||
"DROP DATABASE IF EXISTS testdb", | ||
[]sql.Row{{sql.OkResult{RowsAffected: 1}}}, | ||
nil, | ||
) | ||
|
||
AssertErr(t, e, harness, "USE testdb", sql.ErrDatabaseNotFound) | ||
|
||
AssertWarning(t, e, harness, "DROP DATABASE IF EXISTS testdb", mysql.ERDbDropExists) | ||
}) | ||
} | ||
|
||
func TestCreateForeignKeys(t *testing.T, harness Harness) { | ||
require := require.New(t) | ||
|
||
|
@@ -2391,6 +2584,27 @@ func AssertErr(t *testing.T, e *sqle.Engine, harness Harness, query string, expe | |
} | ||
} | ||
|
||
func AssertWarning(t *testing.T, e *sqle.Engine, harness Harness, query string, expectedCode int) { | ||
_, iter, err := e.Query(NewContext(harness), query) | ||
require.NoError(t, err) | ||
|
||
_, err = sql.RowIterToRows(iter) | ||
require.NoError(t, err) | ||
|
||
ctx := NewContext(harness) | ||
require.True(t, len(ctx.Warnings()) > 0) | ||
|
||
condition := false | ||
for _, warning := range ctx.Warnings() { | ||
if warning.Code == expectedCode { | ||
condition = true | ||
break | ||
} | ||
} | ||
|
||
require.True(t, condition) | ||
} | ||
|
||
type customFunc struct { | ||
expression.UnaryExpression | ||
} | ||
|
@@ -3049,9 +3263,14 @@ func TestColumnDefaults(t *testing.T, harness Harness) { | |
var pid uint64 | ||
|
||
func NewContext(harness Harness) *sql.Context { | ||
ctx := harness.NewContext().WithCurrentDB("mydb") | ||
ctx := harness.NewContext() | ||
currentDB := ctx.GetCurrentDatabase() | ||
if currentDB == "" { | ||
currentDB = "mydb" | ||
ctx.WithCurrentDB(currentDB) | ||
} | ||
|
||
_ = ctx.ViewRegistry.Register("mydb", | ||
_ = ctx.ViewRegistry.Register(currentDB, | ||
plan.NewSubqueryAlias( | ||
"myview", | ||
"SELECT * FROM mytable", | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zachmu Are you fine with me leaving this as an issue an moving on? Getting this render correctly seems to be a pain to get right and is not an important feature imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that's fine for now