-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
base: main
Are you sure you want to change the base?
Conversation
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=? |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
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=?" |
There was a problem hiding this comment.
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?
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 |
There was a problem hiding this comment.
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.
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