From ed978bcb9c353a7e73a6c1e6ae04b6facc8b9b54 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 21 May 2020 20:56:41 +0300 Subject: [PATCH] Don't use different types for SQLite in DB schema --- database/cryptostore.go | 2 +- database/migrate.go | 4 +++ .../upgrades/2018-09-01-initial-schema.go | 25 +++++++------------ database/upgrades/2020-05-09-crypto-store.go | 1 - ...2020-05-12-outbound-group-session-store.go | 1 - 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/database/cryptostore.go b/database/cryptostore.go index 59cecd3..7e04bc3 100644 --- a/database/cryptostore.go +++ b/database/cryptostore.go @@ -258,7 +258,7 @@ func (store *SQLCryptoStore) AddOutboundGroupSession(session *crypto.OutboundGro ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (room_id) DO UPDATE SET session_id=$2, session=$3, shared=$4, max_messages=$5, message_count=$6, max_age=$7, created_at=$8, last_used=$9`, session.RoomID, session.ID(), sessionBytes, session.Shared, session.MaxMessages, session.MessageCount, session.MaxAge, session.CreationTime, session.UseTime) - } else if store.db.dialect == "sqlite" { + } else if store.db.dialect == "sqlite3" { _, err = store.db.Exec(` INSERT OR REPLACE INTO crypto_megolm_outbound_session ( room_id, session_id, session, shared, max_messages, message_count, max_age, created_at, last_used diff --git a/database/migrate.go b/database/migrate.go index 9d30871..a13c6ca 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -145,4 +145,8 @@ func Migrate(old *Database, new *Database) { if err != nil { panic(err) } + err = migrateTable(old, new, "crypto_megolm_outbound_session", "room_id", "session_id", "session", "shared", "max_messages", "message_count", "max_age", "created_at", "last_used") + if err != nil { + panic(err) + } } diff --git a/database/upgrades/2018-09-01-initial-schema.go b/database/upgrades/2018-09-01-initial-schema.go index b1b8709..f142726 100644 --- a/database/upgrades/2018-09-01-initial-schema.go +++ b/database/upgrades/2018-09-01-initial-schema.go @@ -2,26 +2,19 @@ package upgrades import ( "database/sql" - "fmt" ) func init() { upgrades[0] = upgrade{"Initial schema", func(tx *sql.Tx, ctx context) error { - var byteType string - if ctx.dialect == SQLite { - byteType = "BLOB" - } else { - byteType = "bytea" - } _, err := tx.Exec(`CREATE TABLE IF NOT EXISTS portal ( jid VARCHAR(255), receiver VARCHAR(255), mxid VARCHAR(255) UNIQUE, - + name VARCHAR(255) NOT NULL, topic VARCHAR(255) NOT NULL, avatar VARCHAR(255) NOT NULL, - + PRIMARY KEY (jid, receiver) )`) if err != nil { @@ -38,7 +31,7 @@ func init() { return err } - _, err = tx.Exec(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS "user" ( + _, err = tx.Exec(`CREATE TABLE IF NOT EXISTS "user" ( mxid VARCHAR(255) PRIMARY KEY, jid VARCHAR(255) UNIQUE, @@ -47,24 +40,24 @@ func init() { client_id VARCHAR(255), client_token VARCHAR(255), server_token VARCHAR(255), - enc_key %[1]s, - mac_key %[1]s - )`, byteType)) + enc_key bytea, + mac_key bytea + )`) if err != nil { return err } - _, err = tx.Exec(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS message ( + _, err = tx.Exec(`CREATE TABLE IF NOT EXISTS message ( chat_jid VARCHAR(255), chat_receiver VARCHAR(255), jid VARCHAR(255), mxid VARCHAR(255) NOT NULL UNIQUE, sender VARCHAR(255) NOT NULL, - content %[1]s NOT NULL, + content bytea NOT NULL, PRIMARY KEY (chat_jid, chat_receiver, jid), FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE - )`, byteType)) + )`) if err != nil { return err } diff --git a/database/upgrades/2020-05-09-crypto-store.go b/database/upgrades/2020-05-09-crypto-store.go index fd479b0..8be6cd8 100644 --- a/database/upgrades/2020-05-09-crypto-store.go +++ b/database/upgrades/2020-05-09-crypto-store.go @@ -6,7 +6,6 @@ import ( func init() { upgrades[13] = upgrade{"Add crypto store to database", func(tx *sql.Tx, ctx context) error { - // TODO use DATETIME instead of timestamp and BLOB instead of bytea for sqlite _, err := tx.Exec(`CREATE TABLE crypto_account ( device_id VARCHAR(255) PRIMARY KEY, shared BOOLEAN NOT NULL, diff --git a/database/upgrades/2020-05-12-outbound-group-session-store.go b/database/upgrades/2020-05-12-outbound-group-session-store.go index 2d635ef..0f108a6 100644 --- a/database/upgrades/2020-05-12-outbound-group-session-store.go +++ b/database/upgrades/2020-05-12-outbound-group-session-store.go @@ -6,7 +6,6 @@ import ( func init() { upgrades[14] = upgrade{"Add outbound group sessions to database", func(tx *sql.Tx, ctx context) error { - // TODO use DATETIME instead of timestamp and BLOB instead of bytea for sqlite _, err := tx.Exec(`CREATE TABLE crypto_megolm_outbound_session ( room_id VARCHAR(255) PRIMARY KEY, session_id CHAR(43) NOT NULL UNIQUE,