From 82713a4ecd63bc8a61e3d91055f0e6b071d51c51 Mon Sep 17 00:00:00 2001 From: Luca Saiu Date: Fri, 27 Aug 2021 11:34:50 +0200 Subject: [PATCH] fix ENGINE-570: new database user version 19, the only difference being indices Change SQL definitions. Add code to migrate cleanly from version 18. --- src/engine_sql.c | 27 ++++++++++++++++++++++++++- src/engine_sql.h | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/engine_sql.c b/src/engine_sql.c index a983c5ea..5bee4f75 100644 --- a/src/engine_sql.c +++ b/src/engine_sql.c @@ -713,7 +713,7 @@ static PEP_STATUS _create_core_tables(PEP_SESSION session) { " timestamp integer default (datetime('now')),\n" " primary key (address, user_id)\n" ");\n" - "create index if not exists identity_userid_addr on identity(address, user_id);\n" + "create index if not exists identity_userid on identity (user_id);\n" "create table if not exists trust (\n" " user_id text not null\n" " references person (id)\n" @@ -1548,6 +1548,27 @@ static PEP_STATUS _upgrade_DB_to_ver_18(PEP_SESSION session) { return _force_upgrade_own_latest_message_version(session); } +static PEP_STATUS _upgrade_DB_to_ver_19(PEP_SESSION session) { + int int_result = sqlite3_exec( + session->db, + /* This index was useless: it was an index on the (multi-column) + primary key, always implemented using an index which gets also + used in queries. */ + "drop index if exists identity_userid_addr;\n" + "\n" + "create index if not exists identity_userid on identity (user_id);\n" + NULL, + NULL, + NULL + ); + assert(int_result == SQLITE_OK); + + if (int_result != SQLITE_OK) + return PEP_UNKNOWN_DB_ERROR; + + return PEP_STATUS_OK; +} + // Honestly, the upgrades should be redone in a transaction IMHO. static PEP_STATUS _check_and_execute_upgrades(PEP_SESSION session, int version) { PEP_STATUS status = PEP_STATUS_OK; @@ -1620,6 +1641,10 @@ static PEP_STATUS _check_and_execute_upgrades(PEP_SESSION session, int version) if (status != PEP_STATUS_OK) return status; case 18: + status = _upgrade_DB_to_ver_19(session); + if (status != PEP_STATUS_OK) + return status; + case 19: break; default: return PEP_ILLEGAL_VALUE; diff --git a/src/engine_sql.h b/src/engine_sql.h index 0bcd6415..25d98ea4 100644 --- a/src/engine_sql.h +++ b/src/engine_sql.h @@ -3,7 +3,7 @@ #include "pEp_internal.h" // increment this when patching DDL -#define _DDL_USER_VERSION "18" +#define _DDL_USER_VERSION "19" PEP_STATUS init_databases(PEP_SESSION session); PEP_STATUS pEp_sql_init(PEP_SESSION session);