Skip to content
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 19 commits into from Feb 17, 2021
Merged

Vinai/create database #298

merged 19 commits into from Feb 17, 2021

Conversation

VinaiRachakonda
Copy link
Contributor

@VinaiRachakonda VinaiRachakonda commented Feb 12, 2021

Add functionality for CREATE DATABASE

Copy link
Member

@zachmu zachmu left a 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the catalog

Copy link
Member

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()
Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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]
Copy link
Member

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

https://github.com/golang/go/wiki/SliceTricks

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")
Copy link
Member

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrCannotDropDatabaseDoesntExist

return NillaryWithChildren(c, children...)
}

func NewCreateDatabase(dbName string, ifNotExists bool, collate string, charset string) *CreateDB {
Copy link
Member

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

Copy link
Member

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 {
Copy link
Member

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

}

func (d DropDB) Schema() sql.Schema {
return nil
Copy link
Member

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()


func (d DropDB) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
exists := d.Catalog.HasDB(d.dbName)
if d.IfExists && !exists {
Copy link
Member

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

return NillaryWithChildren(d, children...)
}

func NewDropDatabase(dbName string, ifExists bool, collate string, charset string) *DropDB {
Copy link
Member

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)
Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

@zachmu zachmu left a 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)
Copy link
Member

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

//AssertErr(t, e, harness, "SHOW TABLES", sql.ErrNoDatabaseSelected)
})

t.Run("DROP DATABASE works on newly created tables.", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newly created databases?

AssertErr(t, e, harness, "USE testdb", sql.ErrDatabaseNotFound)
})

t.Run("DROP SCHEMA works on newly created tables.", func(t *testing.T) {
Copy link
Member

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) {
Copy link
Member

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

})

t.Run("DROP DATABASE IF EXISTS correctly works.", func(t *testing.T) {
AssertWarning(t, e, harness, "DROP DATABASE IF EXISTS mydb", 1007)
Copy link
Member

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 {
Copy link
Member

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

if d.IfExists {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: 1007,
Copy link
Member

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

@VinaiRachakonda VinaiRachakonda merged commit 5724607 into master Feb 17, 2021
@VinaiRachakonda VinaiRachakonda deleted the vinai/create-database branch February 17, 2021 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants