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

Compatibility with MySQL (MariaDB) storage #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hoehermann
Copy link
Contributor

@hoehermann hoehermann commented Feb 4, 2022

Hi

My project purple-whatsmeow gained some traction based on your awesome work. I am getting requests to support MySQL for the storage. I looked into it. The necessary changes do not seem too drastic. However, all experienced and mentally sane people I talked to urged me to use a DBMS. "Adjustments with regexes would be insane", they said. But here we are anyway.

I tested my changes against SQLite and MySQL, but not with PostgreSQL. I would consider these changes experimental. Table creation and upgrades would have to be done manually, but for now "it is working for me"™.

What do you think?

Kind regards
Hermann

Comment on lines -180 to +184
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
ON CONFLICT (jid) DO UPDATE SET platform=$12, business_name=$13, push_name=$14
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (jid) DO UPDATE SET platform=?, business_name=?, push_name=?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apparently, MySQL (MariaDB) does not do positional placeholders. We must use some repetition. :(

@@ -243,3 +247,17 @@ func (c *Container) DeleteDevice(store *store.Device) error {
_, err := c.db.Exec(deleteDeviceQuery, store.ID.String())
return err
}

var (
mysqlConflictPattern = regexp.MustCompile(`ON CONFLICT \([^)]+\) DO UPDATE SET`) // cannot be const
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ON CONFLICT is the only statement which is not supported by MariaDB. Everything else is just fine.

@hoehermann hoehermann changed the title Compatibility with MySQL storage Compatibility with MySQL (MariaDB) storage Feb 4, 2022
Comment on lines -150 to +151
insertPreKeyQuery = `INSERT INTO whatsmeow_pre_keys (jid, key_id, key, uploaded) VALUES ($1, $2, $3, $4)`
getUnuploadedPreKeysQuery = `SELECT key_id, key FROM whatsmeow_pre_keys WHERE jid=$1 AND uploaded=false ORDER BY key_id LIMIT $2`
getPreKeyQuery = `SELECT key_id, key FROM whatsmeow_pre_keys WHERE jid=$1 AND key_id=$2`
deletePreKeyQuery = `DELETE FROM whatsmeow_pre_keys WHERE jid=$1 AND key_id=$2`
markPreKeysAsUploadedQuery = `UPDATE whatsmeow_pre_keys SET uploaded=true WHERE jid=$1 AND key_id<=$2`
getUploadedPreKeyCountQuery = `SELECT COUNT(*) FROM whatsmeow_pre_keys WHERE jid=$1 AND uploaded=true`
getLastPreKeyIDQuery = `SELECT MAX(key_id) FROM whatsmeow_pre_keys WHERE jid=?`
insertPreKeyQuery = "INSERT INTO whatsmeow_pre_keys (jid, key_id, `key`, uploaded) VALUES (?, ?, ?, ?)"
getUnuploadedPreKeysQuery = "SELECT key_id, `key` FROM whatsmeow_pre_keys WHERE jid=? AND uploaded=false ORDER BY key_id LIMIT ?"
getPreKeyQuery = "SELECT key_id, `key` FROM whatsmeow_pre_keys WHERE jid=? AND key_id=?"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In MySQL, key is a keyword. We must escape it with backticks. It seems that in go, you cannot have backticks in raw strings. Maybe we should use non-raw strings everywhere just for consistency?

Comment on lines +345 to +350
func (s *SQLStore) putAppStateMutationMACs(tx execable, name string, version uint64, mutations []store.AppStateMutationMAC) (err error) {
for _, mutation := range mutations {
query := putAppStateMutationMACsQuery + "(?, ?, ?, ?, ?)"
_, err = tx.Exec(query, s.JID, name, version, mutation.IndexMAC, mutation.ValueMAC)
if err != nil {
return
Copy link
Contributor Author

@hoehermann hoehermann Feb 4, 2022

Choose a reason for hiding this comment

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

This will impact performance, but it simplifies the query.

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