Skip to content

Commit 6a27de3

Browse files
committed
refactoring(sql): recreate config table with UNIQUE constraint
Migration 71 relying on set_config() was added in early 2021. It is simply removed, worst case that happens if you upgrade a pre-71 version to the current one is that you will not have a configured_provider and will not use provider database settings. This is already what happens if your provider was added after configuring.
1 parent 1c9662a commit 6a27de3

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/sql.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -572,22 +572,15 @@ impl Sql {
572572
pub async fn set_raw_config(&self, key: &str, value: Option<&str>) -> Result<()> {
573573
let mut lock = self.config_cache.write().await;
574574
if let Some(value) = value {
575-
let exists = self
576-
.exists("SELECT COUNT(*) FROM config WHERE keyname=?;", (key,))
577-
.await?;
578-
579-
if exists {
580-
self.execute("UPDATE config SET value=? WHERE keyname=?;", (value, key))
581-
.await?;
582-
} else {
583-
self.execute(
584-
"INSERT INTO config (keyname, value) VALUES (?, ?);",
585-
(key, value),
586-
)
587-
.await?;
588-
}
575+
self.execute(
576+
"INSERT INTO config (keyname, value) VALUES (?, ?)
577+
ON CONFLICT (keyname)
578+
DO UPDATE SET value=excluded.value",
579+
(key, value),
580+
)
581+
.await?;
589582
} else {
590-
self.execute("DELETE FROM config WHERE keyname=?;", (key,))
583+
self.execute("DELETE FROM config WHERE keyname=?", (key,))
591584
.await?;
592585
}
593586
lock.insert(key.to_string(), value.map(|s| s.to_string()));
@@ -608,7 +601,7 @@ impl Sql {
608601

609602
let mut lock = self.config_cache.write().await;
610603
let value = self
611-
.query_get_value("SELECT value FROM config WHERE keyname=?;", (key,))
604+
.query_get_value("SELECT value FROM config WHERE keyname=?", (key,))
612605
.await
613606
.context(format!("failed to fetch raw config: {key}"))?;
614607
lock.insert(key.to_string(), value.clone());

src/sql/migrations.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -362,22 +362,6 @@ UPDATE chats SET protected=1, type=120 WHERE type=130;"#,
362362
.await?;
363363
}
364364

365-
if dbversion < 71 {
366-
if let Ok(addr) = context.get_primary_self_addr().await {
367-
if let Ok(domain) = EmailAddress::new(&addr).map(|email| email.domain) {
368-
context
369-
.set_config(
370-
Config::ConfiguredProvider,
371-
get_provider_by_domain(&domain).map(|provider| provider.id),
372-
)
373-
.await?;
374-
} else {
375-
warn!(context, "Can't parse configured address: {:?}", addr);
376-
}
377-
}
378-
379-
sql.set_db_version(71).await?;
380-
}
381365
if dbversion < 72 && !sql.col_exists("msgs", "mime_modified").await? {
382366
sql.execute_migration(
383367
r#"
@@ -785,6 +769,27 @@ CREATE INDEX msgs_status_updates_index2 ON msgs_status_updates (uid);
785769
.await?;
786770
}
787771

772+
if dbversion < 106 {
773+
// Recreate `config` table with UNIQUE constraint on `keyname`.
774+
sql.execute_migration(
775+
r#"
776+
CREATE TABLE new_config (
777+
id INTEGER PRIMARY KEY,
778+
keyname TEXT UNIQUE,
779+
value TEXT
780+
);
781+
INSERT OR IGNORE INTO new_config SELECT
782+
id, keyname, value
783+
FROM config;
784+
DROP TABLE config;
785+
ALTER TABLE new_config RENAME TO config;
786+
CREATE INDEX config_index1 ON config (keyname);
787+
"#,
788+
106,
789+
)
790+
.await?;
791+
}
792+
788793
let new_version = sql
789794
.get_raw_config_int(VERSION_CFG)
790795
.await?

0 commit comments

Comments
 (0)