Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

restore: insert IF NOT EXISTS to create table SQL#85

Closed
5kbpers wants to merge 2 commits intopingcap:masterfrom
5kbpers:skip-create-table
Closed

restore: insert IF NOT EXISTS to create table SQL#85
5kbpers wants to merge 2 commits intopingcap:masterfrom
5kbpers:skip-create-table

Conversation

@5kbpers
Copy link
Contributor

@5kbpers 5kbpers commented Dec 6, 2019

Signed-off-by: 5kbpers tangminghua@pingcap.com

When we cannot rebuild the CREATE TABLE SQL correctly, users should be able to create the table manually.
This PR inserts IF NOT EXISTS to skip creating the existed table.

Maybe a better way is to check if the table existed before creating it, but that will introduce extra cost.

5kbpers added 2 commits December 6, 2019 14:53
Signed-off-by: 5kbpers <tangminghua@pingcap.com>
Signed-off-by: 5kbpers <tangminghua@pingcap.com>
@5kbpers 5kbpers changed the title restore: insert IF NOT EXISTs to create table SQL restore: insert IF NOT EXISTS to create table SQL Dec 6, 2019
@codecov
Copy link

codecov bot commented Dec 6, 2019

Codecov Report

Merging #85 into master will decrease coverage by 0.12%.
The diff coverage is 42.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #85      +/-   ##
==========================================
- Coverage    66.4%   66.27%   -0.13%     
==========================================
  Files          22       22              
  Lines        2542     2556      +14     
==========================================
+ Hits         1688     1694       +6     
- Misses        625      632       +7     
- Partials      229      230       +1
Impacted Files Coverage Δ
pkg/restore/db.go 45.94% <42.85%> (-0.73%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6b99182...47036ca. Read the comment docs.

Copy link
Member

@overvenus overvenus left a comment

Choose a reason for hiding this comment

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

LGTM

}
createSQL := buf.String()
// Insert `IF NOT EXISTS` statement to skip the created tables
words := strings.Split(createSQL, " ")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use SplitN and limits n to 3.

resWords := append([]string{}, words[:2]...)
resWords = append(resWords, "IF NOT EXISTS")
resWords = append(resWords, words[2:]...)
createSQL = strings.Join(resWords, " ")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Then just write "create table if not exists " + words[2] here.

zap.Stringer("table", schema.Name),
zap.String("SQL", createSQL),
zap.Error(err))
return errors.New("build create table SQL failed")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need to immediately bail out if the SQL does not start with CREATE TABLE.

@overvenus
Copy link
Member

/run-integration-tests

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants