Get rid of misleading SCHEMA_INFO error 44/200244/9
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 20 Feb 2019 11:40:21 +0000 (12:40 +0100)
committerTomasz Swierczek <t.swierczek@samsung.com>
Fri, 17 May 2019 08:48:15 +0000 (10:48 +0200)
During startup the key-manager attempts to read a table SCHEMA_INFO to get the
information about the database version. In older versions of the database that
table is missing. Key-manager properly handles that case but produces 3 lines of
error log which may suggest that something went wrong.

This commit checks the existence of the table before attempting to use it. Whole
operation is enclosed in a transaction.

Change-Id: Ie7f1fbe1182c2add5965f8e5ddada262ffcb42fe

src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index 2bc2a92..d7acb0f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -229,25 +229,28 @@ void Crypto::createView(const char *create_cmd)
 
 bool Crypto::getDBVersion(int &schemaVersion)
 {
-       SchemaInfo SchemaInfo(this);
-
-       if (SchemaInfo.getVersionInfo(schemaVersion)) {
-               LogDebug("Current DB version: " << schemaVersion);
-               return true;
-       } else {
-               LogDebug("No DB version known or DB not present");
+       Transaction transaction(this);
 
-               if (m_connection->CheckTableExist("CKM_TABLE")) {
-                       // special case: old CKM_TABLE exists
-                       schemaVersion = DB_VERSION_1;
-                       return true;
-               } else if (m_connection->CheckTableExist("NAME_TABLE")) {
-                       // special case: new scheme exists, but no SCHEMA_INFO table present
-                       schemaVersion = DB_VERSION_2;
+       if (m_connection->CheckTableExist("SCHEMA_INFO")) {
+               SchemaInfo SchemaInfo(m_connection);
+               if (SchemaInfo.getVersionInfo(schemaVersion)) {
+                       LogDebug("Current DB version: " << schemaVersion);
                        return true;
                }
        }
 
+       LogDebug("No DB version known or DB not present");
+
+       if (m_connection->CheckTableExist("CKM_TABLE")) {
+               // special case: old CKM_TABLE exists
+               schemaVersion = DB_VERSION_1;
+               return true;
+       } else if (m_connection->CheckTableExist("NAME_TABLE")) {
+               // special case: new scheme exists, but no SCHEMA_INFO table present
+               schemaVersion = DB_VERSION_2;
+               return true;
+       }
+
        // not recognized - proceed with an empty DBs
        return false;
 }
@@ -282,7 +285,7 @@ void Crypto::initDatabase()
                }
 
                // update DB version info
-               SchemaInfo SchemaInfo(this);
+               SchemaInfo SchemaInfo(m_connection);
                SchemaInfo.setVersionInfo();
                transaction.commit();
        }
@@ -320,7 +323,7 @@ void Crypto::createDBSchema()
                                 "Can not create the database schema: no initialization script");
 
        m_connection->ExecCommand((*script).c_str());
-       SchemaInfo SchemaInfo(this);
+       SchemaInfo SchemaInfo(m_connection);
        SchemaInfo.setVersionInfo();
        transaction.commit();
 }
@@ -712,34 +715,23 @@ void Crypto::setPermission(
 void Crypto::SchemaInfo::setVersionInfo()
 {
        SqlConnection::DataCommandUniquePtr insertContextCommand =
-               m_db->m_connection->PrepareDataCommand(DB_CMD_SCHEMA_SET);
+               m_connection->PrepareDataCommand(DB_CMD_SCHEMA_SET);
        insertContextCommand->BindString(101, DB_SCHEMA_VERSION_FIELD);
        insertContextCommand->BindString(103,
                                                                         std::to_string(DB_VERSION_CURRENT).c_str());
        insertContextCommand->Step();
 }
 
-bool Crypto::SchemaInfo::getVersionInfo(int &version) const
+bool Crypto::SchemaInfo::getVersionInfo(int &version)
 {
-       // Try..Catch mandatory here - we don't need to escalate the error
-       // if it happens - we just won't return the version, allowing CKM to work
-       try {
-               SqlConnection::DataCommandUniquePtr selectCommand =
-                       m_db->m_connection->PrepareDataCommand(DB_CMD_SCHEMA_GET);
-               selectCommand->BindString(101, DB_SCHEMA_VERSION_FIELD);
+       SqlConnection::DataCommandUniquePtr selectCommand =
+               m_connection->PrepareDataCommand(DB_CMD_SCHEMA_GET);
+       selectCommand->BindString(101, DB_SCHEMA_VERSION_FIELD);
 
-               if (selectCommand->Step()) {
-                       version = static_cast<int>(atoi(selectCommand->GetColumnString(1).c_str()));
-                       return true;
-               }
-       } catch (const SqlConnection::Exception::InvalidColumn &) {
-               LogError("Select statement invalid column error");
-       } catch (const SqlConnection::Exception::SyntaxError &) {
-               LogError("Couldn't prepare select statement");
-       } catch (const SqlConnection::Exception::InternalError &) {
-               LogError("Couldn't execute select statement");
+       if (selectCommand->Step()) {
+               version = static_cast<int>(atoi(selectCommand->GetColumnString(1).c_str()));
+               return true;
        }
-
        return false;
 }
 
index 65ef3a3..ad0ef12 100644 (file)
@@ -231,13 +231,13 @@ private:
 
        class SchemaInfo {
        public:
-               explicit SchemaInfo(const Crypto *db) : m_db(db) {}
+               explicit SchemaInfo(SqlConnection *connection) : m_connection(connection) {}
 
                void setVersionInfo();
-               bool getVersionInfo(int &version) const;
+               bool getVersionInfo(int &version);
 
        private:
-               const Crypto *m_db;
+               SqlConnection *m_connection;
        };
 
 public: