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

Fix creating index in mysql. #31

Merged
merged 1 commit into from
May 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ func (b *Backend) initSchema() error {
_, err = b.db.Exec(`
CREATE INDEX IF NOT EXISTS extKeys_uid_id
ON extKeys(uid, id)`)
// MySQL does not support "IF NOT EXISTS", but MariaDB does.
if err != nil && b.db.driver == "mysql" {
_, err = b.db.Exec(`
CREATE INDEX extKeys_uid_id
ON extKeys(uid, id)`)
if err != nil && strings.HasPrefix(err.Error(), "Error 1061: Duplicate key name") {
err = nil
}
}
if err != nil {
return wrapErr(err, "create index extKeys_uid_id")
}
Expand Down Expand Up @@ -167,6 +176,15 @@ func (b *Backend) initSchema() error {
_, err = b.db.Exec(`
CREATE INDEX IF NOT EXISTS seen_msgs
ON msgs(mboxId, seen)`)
// MySQL does not support "IF NOT EXISTS", but MariaDB does.
if err != nil && b.db.driver == "mysql" {
_, err = b.db.Exec(`
CREATE INDEX seen_msgs
ON msgs(mboxId, seen)`)
if err != nil && strings.HasPrefix(err.Error(), "Error 1061: Duplicate key name") {
err = nil
}
}
if err != nil {
return wrapErr(err, "create index seen_msgs")
}
Expand Down Expand Up @@ -648,7 +666,7 @@ func (b *Backend) prepareStmts() error {
b.decreaseRefForMarked, err = b.db.Prepare(`
UPDATE extKeys
SET refs = refs - 1
WHERE uid = ?
WHERE uid = ?
AND id IN (
SELECT extBodyKey
FROM msgs
Expand All @@ -660,7 +678,7 @@ func (b *Backend) prepareStmts() error {
b.decreaseRefForDeleted, err = b.db.Prepare(`
UPDATE extKeys
SET refs = refs - 1
WHERE uid = ?
WHERE uid = ?
AND id IN (
SELECT extBodyKey
FROM msgs
Expand Down Expand Up @@ -751,7 +769,7 @@ func (b *Backend) prepareStmts() error {
}

b.specialUseMbox, err = b.db.Prepare(`
SELECT name, id
SELECT name, id
FROM mboxes
WHERE uid = ?
AND specialuse = ?
Expand Down