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
Conversation
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.
Good start but missing tests. We need:
- parse tests
- engine tests (probably best to stick this kind of thing either in script_queries.go or else as their own standalone methods in enginetests.go)
sql/catalog.go
Outdated
@@ -81,6 +81,13 @@ func (c *Catalog) AddDatabase(db Database) { | |||
c.mu.Unlock() | |||
} | |||
|
|||
// DropDatabase removes a database to the catalog. |
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.
from the catalog
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.
Call this Remove instead of Drop. Drop would be for integrators to implement (which isn't supported yet)
sql/catalog.go
Outdated
@@ -120,6 +127,10 @@ func (d Databases) Database(name string) (Database, error) { | |||
return nil, ErrDatabaseNotFound.New(name) | |||
} | |||
|
|||
if len(name) == 0 { | |||
return nil, ErrNoDatabaseSelected.New() |
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.
This isn't the right error here, should just be ErrDatabaseNotFound as above
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.
And in fact you don't need this check specifically, it's already handled by the logic below
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.
So the problem I was having is that mysql should return a database not set error when you drop the database you are currently using. This adds that functionality. Lmk if that seems like a hacky solution to you.
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.
I'll add a test to sql-server in dolt to show this.
sql/catalog.go
Outdated
|
||
if idx != -1 { | ||
dbs := *d | ||
dbs[idx] = dbs[len(dbs)-1] |
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.
Check out this guide for a shorter way to remove an element from a slice
sql/errors.go
Outdated
@@ -126,4 +126,10 @@ var ( | |||
|
|||
// ErrTruncateReferencedFromForeignKey is returned when a table is referenced in a foreign key and TRUNCATE is called on it. | |||
ErrTruncateReferencedFromForeignKey = errors.NewKind("cannot truncate table %s as it is referenced in foreign key %s on table %s") | |||
|
|||
// ErrDatabaseExists is returned when a CREATE DATABASE is called on a table that already exists. | |||
ErrDatabaseExists = errors.NewKind("can't create database %s; database exists") |
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.
ErrCannotCreateDatabaseExists
sql/errors.go
Outdated
ErrDatabaseExists = errors.NewKind("can't create database %s; database exists") | ||
|
||
// ErrDatabaseDoesntExists is returned when a DROP DATABASE is callend when a table is dropped that doesn't exist. | ||
ErrDatabaseDoesntExists = errors.NewKind("can't drop database %s; database doesn't exist") |
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.
ErrCannotDropDatabaseDoesntExist
sql/plan/dbddl.go
Outdated
return NillaryWithChildren(c, children...) | ||
} | ||
|
||
func NewCreateDatabase(dbName string, ifNotExists bool, collate string, charset string) *CreateDB { |
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.
collation (same with field names
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.
On second thought, don't even take collation and charset since we don't and can't do anything with them.
} | ||
} | ||
|
||
type DropDB struct { |
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.
Also needs a comment about what this does
sql/plan/dbddl.go
Outdated
} | ||
|
||
func (d DropDB) Schema() sql.Schema { | ||
return nil |
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.
Both of these should return sql.OkResultSchema and report one row modified from RowIter()
sql/plan/dbddl.go
Outdated
|
||
func (d DropDB) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) { | ||
exists := d.Catalog.HasDB(d.dbName) | ||
if d.IfExists && !exists { |
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.
Same comment about structuring this logic
sql/plan/dbddl.go
Outdated
return NillaryWithChildren(d, children...) | ||
} | ||
|
||
func NewDropDatabase(dbName string, ifExists bool, collate string, charset string) *DropDB { |
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.
Same comment about collation and charset
require.Error(t, err) | ||
|
||
// TODO: Deal with handling this error. | ||
//AssertErr(t, e, harness, "SHOW TABLES", sql.ErrNoDatabaseSelected) |
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
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.
Looks great, just a few comments.
require.Error(t, err) | ||
|
||
// TODO: Deal with handling this error. | ||
//AssertErr(t, e, harness, "SHOW TABLES", sql.ErrNoDatabaseSelected) |
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
enginetest/enginetests.go
Outdated
//AssertErr(t, e, harness, "SHOW TABLES", sql.ErrNoDatabaseSelected) | ||
}) | ||
|
||
t.Run("DROP DATABASE works on newly created tables.", func(t *testing.T) { |
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.
newly created databases?
enginetest/enginetests.go
Outdated
AssertErr(t, e, harness, "USE testdb", sql.ErrDatabaseNotFound) | ||
}) | ||
|
||
t.Run("DROP SCHEMA works on newly created tables.", func(t *testing.T) { |
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.
created database
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 comment
The 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
enginetest/enginetests.go
Outdated
}) | ||
|
||
t.Run("DROP DATABASE IF EXISTS correctly works.", func(t *testing.T) { | ||
AssertWarning(t, e, harness, "DROP DATABASE IF EXISTS mydb", 1007) |
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.
Use consts for expected err codes
"github.com/dolthub/vitess/go/vt/sqlparser" | ||
) | ||
|
||
type CreateDB struct { |
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.
integrators are people who provide database implementation, e.g. dolt
sql/plan/dbddl.go
Outdated
if d.IfExists { | ||
ctx.Session.Warn(&sql.Warning{ | ||
Level: "Note", | ||
Code: 1007, |
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.
Use the vitess const for this
Add functionality for
CREATE DATABASE