2019-05-16 19:14:32 +02:00
|
|
|
package upgrades
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-25 16:25:19 +02:00
|
|
|
upgrades[1] = upgrade{"Add ON DELETE CASCADE to message table", func(tx *sql.Tx, ctx context) error {
|
|
|
|
if ctx.dialect == SQLite {
|
2019-05-16 19:14:32 +02:00
|
|
|
// SQLite doesn't support constraint updates, but it isn't that careful with constraints anyway.
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-02 20:53:45 +02:00
|
|
|
res, _ := ctx.db.Query(`SELECT EXISTS(SELECT constraint_name FROM information_schema.table_constraints
|
|
|
|
WHERE table_name='message' AND constraint_name='message_chat_jid_fkey')`)
|
2020-04-02 20:37:51 +02:00
|
|
|
var exists bool
|
|
|
|
_ = res.Scan(&exists)
|
|
|
|
if exists {
|
|
|
|
_, err := tx.Exec("ALTER TABLE message DROP CONSTRAINT IF EXISTS message_chat_jid_fkey")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = tx.Exec(`ALTER TABLE message ADD CONSTRAINT message_chat_jid_fkey
|
2019-05-16 19:14:32 +02:00
|
|
|
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
|
|
|
|
ON DELETE CASCADE`)
|
2020-04-02 20:37:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-16 19:14:32 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
}
|