Skip to content

exit early when IF NOT EXISTS and table exists#2387

Merged
jycor merged 4 commits intomainfrom
james/duplicate
Mar 12, 2024
Merged

exit early when IF NOT EXISTS and table exists#2387
jycor merged 4 commits intomainfrom
james/duplicate

Conversation

@jycor
Copy link
Contributor

@jycor jycor commented Mar 12, 2024

This PR addresses various issues related to CREATE TABLE IF NOT EXISTS ... queries.

Before, we simply ignored the table exists error, and continued creating indexes, foreign keys, and checks.
This led to errors when attempting to create indexes/foreign keys/checks that already exists.
Additionally, it would errorneously create indexes/foreng keys/checks that did exist.

The correct behavior is to do nothing if IF NOT EXISTS is specified and the table exists.

Also this contains some refactors and simplifications.

fixes dolthub/dolt#7602

Copy link
Contributor

@fulghum fulghum left a comment

Choose a reason for hiding this comment

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

Looks good! Just one question on the error message change.

if data.indexes[name] != nil {
// TODO: extract a standard error type for this
return nil, fmt.Errorf("Error: index already exists")
return nil, sql.ErrDuplicateKey.New(name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the error message changed; was "index already exists" not accurate or are we just trying to match MySQL's message closer? If the latter... the error message I got from MySQL when I tried to create a new index using an existing index name was:

`foo` already exists as an index for this table

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the output I got from mysql

mysql> create table t (i int, index (i));
Query OK, 0 rows affected (0.0254 sec)
mysql> create index i on t(i);
ERROR: 1061: Duplicate key name 'i'

@jycor jycor merged commit ffce238 into main Mar 12, 2024
@jycor jycor deleted the james/duplicate branch March 12, 2024 19:52
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.

Conditionally creating a table in a script produces index already exists error

2 participants