<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[278053] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/278053">278053</a></dd>
<dt>Author</dt> <dd>sihui_liu@apple.com</dd>
<dt>Date</dt> <dd>2021-05-25 15:34:54 -0700 (Tue, 25 May 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make error returned by SQLiteIDBBackingStore::getOrEstablishDatabaseInfo more detailed
https://bugs.webkit.org/show_bug.cgi?id=226124

Reviewed by Chris Dumez.

Convert some error loggings to error info to help debug rdar://77532291.

* Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
(WebCore::IDBServer::createOrMigrateRecordsTableIfNecessary):
(WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidBlobTables):
(WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidRecordsTable):
(WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidIndexRecordsTable):
(WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidIndexRecordsIndex):
(WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidIndexRecordsRecordIndex):
(WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidObjectStoreInfoTable):
(WebCore::IDBServer::SQLiteIDBBackingStore::getOrEstablishDatabaseInfo):
* Modules/indexeddb/server/SQLiteIDBBackingStore.h:
* platform/sql/SQLiteDatabase.cpp: add two helper funtions that are used in many places.
(WebCore::SQLiteDatabase::tableExists):
(WebCore::SQLiteDatabase::tableSQL):
(WebCore::SQLiteDatabase::indexSQL):
* platform/sql/SQLiteDatabase.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesindexeddbserverSQLiteIDBBackingStorecpp">trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesindexeddbserverSQLiteIDBBackingStoreh">trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h</a></li>
<li><a href="#trunkSourceWebCoreModulesindexeddbserverUniqueIDBDatabasecpp">trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformsqlSQLiteDatabasecpp">trunk/Source/WebCore/platform/sql/SQLiteDatabase.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformsqlSQLiteDatabaseh">trunk/Source/WebCore/platform/sql/SQLiteDatabase.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (278052 => 278053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-05-25 21:34:39 UTC (rev 278052)
+++ trunk/Source/WebCore/ChangeLog      2021-05-25 22:34:54 UTC (rev 278053)
</span><span class="lines">@@ -1,3 +1,28 @@
</span><ins>+2021-05-25  Sihui Liu  <sihui_liu@apple.com>
+
+        Make error returned by SQLiteIDBBackingStore::getOrEstablishDatabaseInfo more detailed
+        https://bugs.webkit.org/show_bug.cgi?id=226124
+
+        Reviewed by Chris Dumez.
+
+        Convert some error loggings to error info to help debug rdar://77532291.
+
+        * Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
+        (WebCore::IDBServer::createOrMigrateRecordsTableIfNecessary):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidBlobTables):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidRecordsTable):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidIndexRecordsTable):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidIndexRecordsIndex):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidIndexRecordsRecordIndex):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::ensureValidObjectStoreInfoTable):
+        (WebCore::IDBServer::SQLiteIDBBackingStore::getOrEstablishDatabaseInfo):
+        * Modules/indexeddb/server/SQLiteIDBBackingStore.h:
+        * platform/sql/SQLiteDatabase.cpp: add two helper funtions that are used in many places.
+        (WebCore::SQLiteDatabase::tableExists):
+        (WebCore::SQLiteDatabase::tableSQL):
+        (WebCore::SQLiteDatabase::indexSQL):
+        * platform/sql/SQLiteDatabase.h:
+
</ins><span class="cx"> 2021-05-25  Don Olmstead  <don.olmstead@sony.com>
</span><span class="cx"> 
</span><span class="cx">         [PlayStation] Build failure in UserAgentPlayStation
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesindexeddbserverSQLiteIDBBackingStorecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp (278052 => 278053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp  2021-05-25 21:34:39 UTC (rev 278052)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp     2021-05-25 22:34:54 UTC (rev 278053)
</span><span class="lines">@@ -252,47 +252,24 @@
</span><span class="cx">         closeSQLiteDB();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static bool createOrMigrateRecordsTableIfNecessary(SQLiteDatabase& database)
</del><ins>+static IDBError createOrMigrateRecordsTableIfNecessary(SQLiteDatabase& database)
</ins><span class="cx"> {
</span><del>-    String currentSchema;
-    {
-        // Fetch the schema for an existing records table.
-        auto statement = database.prepareStatement("SELECT type, sql FROM sqlite_master WHERE tbl_name='Records'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the Records table.");
-            return false;
-        }
</del><ins>+    String tableStatement = database.tableSQL("Records"_s);
+    if (tableStatement.isEmpty()) {
+        if (!database.executeCommand(v3RecordsTableSchema()))
+            return IDBError { UnknownError, makeString("Error creating new Records table (%i) - %s", database.lastError(), database.lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        int sqliteResult = statement->step();
-
-        // If there is no Records table at all, create it and then bail.
-        if (sqliteResult == SQLITE_DONE) {
-            if (!database.executeCommand(v3RecordsTableSchema())) {
-                LOG_ERROR("Could not create Records table in database (%i) - %s", database.lastError(), database.lastErrorMsg());
-                return false;
-            }
-
-            return true;
-        }
-
-        if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the Records table.");
-            return false;
-        }
-
-        currentSchema = statement->columnText(1);
</del><ins>+        return IDBError { };
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    ASSERT(!currentSchema.isEmpty());
-
</del><span class="cx">     // If the schema in the backing store is the current schema, we're done.
</span><del>-    if (currentSchema == v3RecordsTableSchema() || currentSchema == v3RecordsTableSchemaAlternate())
-        return true;
</del><ins>+    if (tableStatement == v3RecordsTableSchema() || tableStatement == v3RecordsTableSchemaAlternate())
+        return IDBError { };
</ins><span class="cx"> 
</span><span class="cx">     // If the record table is not the current schema then it must be one of the previous schemas.
</span><span class="cx">     // If it is not then the database is in an unrecoverable state and this should be considered a fatal error.
</span><del>-    if (currentSchema != v1RecordsTableSchema() && currentSchema != v1RecordsTableSchemaAlternate()
-        && currentSchema != v2RecordsTableSchema() && currentSchema != v2RecordsTableSchemaAlternate())
</del><ins>+    if (tableStatement != v1RecordsTableSchema() && tableStatement != v1RecordsTableSchemaAlternate()
+        && tableStatement != v2RecordsTableSchema() && tableStatement != v2RecordsTableSchemaAlternate())
</ins><span class="cx">         RELEASE_ASSERT_NOT_REACHED();
</span><span class="cx"> 
</span><span class="cx">     SQLiteTransaction transaction(database);
</span><span class="lines">@@ -299,293 +276,141 @@
</span><span class="cx">     transaction.begin();
</span><span class="cx"> 
</span><span class="cx">     // Create a temporary table with the correct schema and migrate all existing content over.
</span><del>-    if (!database.executeCommand(v3RecordsTableSchemaTemp())) {
-        LOG_ERROR("Could not create temporary records table in database (%i) - %s", database.lastError(), database.lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!database.executeCommand(v3RecordsTableSchemaTemp()))
+        return IDBError { UnknownError, makeString("Error creating temporary Records table (%i) - %s", database.lastError(), database.lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    if (!database.executeCommand("INSERT INTO _Temp_Records (objectStoreID, key, value) SELECT objectStoreID, CAST(key AS TEXT), value FROM Records"_s)) {
-        LOG_ERROR("Could not migrate existing Records content (%i) - %s", database.lastError(), database.lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!database.executeCommand("INSERT INTO _Temp_Records (objectStoreID, key, value) SELECT objectStoreID, CAST(key AS TEXT), value FROM Records"_s))
+        return IDBError { UnknownError, makeString("Error migrating Records table (%i) - %s", database.lastError(), database.lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    if (!database.executeCommand("DROP TABLE Records"_s)) {
-        LOG_ERROR("Could not drop existing Records table (%i) - %s", database.lastError(), database.lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!database.executeCommand("DROP TABLE Records"_s))
+        return IDBError { UnknownError, makeString("Error dropping Records table (%i) - %s", database.lastError(), database.lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    if (!database.executeCommand("ALTER TABLE _Temp_Records RENAME TO Records"_s)) {
-        LOG_ERROR("Could not rename temporary Records table (%i) - %s", database.lastError(), database.lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!database.executeCommand("ALTER TABLE _Temp_Records RENAME TO Records"_s))
+        return IDBError { UnknownError, makeString("Error renaming temporary Records table (%i) - %s", database.lastError(), database.lastErrorMsg()) };
</ins><span class="cx"> 
</span><span class="cx">     transaction.commit();
</span><span class="cx"> 
</span><del>-    return true;
</del><ins>+    return IDBError { };
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool SQLiteIDBBackingStore::ensureValidBlobTables()
</del><ins>+IDBError SQLiteIDBBackingStore::ensureValidBlobTables()
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_sqliteDB);
</span><span class="cx">     ASSERT(m_sqliteDB->isOpen());
</span><span class="cx"> 
</span><del>-    String currentSchema;
-    {
-        // Fetch the schema for an existing blob record table.
-        auto statement = m_sqliteDB->prepareStatement("SELECT type, sql FROM sqlite_master WHERE tbl_name='BlobRecords'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the BlobRecords table.");
-            return false;
-        }
</del><ins>+    String recordsTableStatement = m_sqliteDB->tableSQL("BlobRecords"_s);
+    if (recordsTableStatement.isEmpty()) {
+        if (!m_sqliteDB->executeCommand(blobRecordsTableSchema()))
+            return IDBError { UnknownError, makeString("Error creating BlobRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        int sqliteResult = statement->step();
-
-        // If there is no BlobRecords table at all, create it..
-        if (sqliteResult == SQLITE_DONE) {
-            if (!m_sqliteDB->executeCommand(blobRecordsTableSchema())) {
-                LOG_ERROR("Could not create BlobRecords table in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-                return false;
-            }
-
-            currentSchema = blobRecordsTableSchema();
-        } else if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the BlobRecords table.");
-            return false;
-        } else
-            currentSchema = statement->columnText(1);
</del><ins>+        recordsTableStatement = blobRecordsTableSchema();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (currentSchema != blobRecordsTableSchema() && currentSchema != blobRecordsTableSchemaAlternate()) {
-        LOG_ERROR("Invalid BlobRecords table schema found");
-        return false;
-    }
</del><ins>+    RELEASE_ASSERT(recordsTableStatement == blobRecordsTableSchema() || recordsTableStatement == blobRecordsTableSchemaAlternate());
</ins><span class="cx"> 
</span><del>-    {
-        // Fetch the schema for an existing blob file table.
-        auto statement = m_sqliteDB->prepareStatement("SELECT type, sql FROM sqlite_master WHERE tbl_name='BlobFiles'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the BlobFiles table.");
-            return false;
-        }
</del><ins>+    String filesTableStatement = m_sqliteDB->tableSQL("BlobFiles"_s);
+    if (filesTableStatement.isEmpty()) {
+        if (!m_sqliteDB->executeCommand(blobFilesTableSchema()))
+            return IDBError { UnknownError, makeString("Error creating BlobFiles table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        int sqliteResult = statement->step();
-
-        // If there is no BlobFiles table at all, create it and then bail.
-        if (sqliteResult == SQLITE_DONE) {
-            if (!m_sqliteDB->executeCommand(blobFilesTableSchema())) {
-                LOG_ERROR("Could not create BlobFiles table in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-                return false;
-            }
-
-            return true;
-        }
-
-        if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the BlobFiles table.");
-            return false;
-        }
-
-        currentSchema = statement->columnText(1);
</del><ins>+        filesTableStatement = blobFilesTableSchema();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (currentSchema != blobFilesTableSchema() && currentSchema != blobFilesTableSchemaAlternate()) {
-        LOG_ERROR("Invalid BlobFiles table schema found");
-        return false;
-    }
-
-    return true;
</del><ins>+    RELEASE_ASSERT(filesTableStatement == blobFilesTableSchema() || filesTableStatement == blobFilesTableSchemaAlternate());
+    return IDBError { };
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool SQLiteIDBBackingStore::ensureValidRecordsTable()
</del><ins>+IDBError SQLiteIDBBackingStore::ensureValidRecordsTable()
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_sqliteDB);
</span><span class="cx">     ASSERT(m_sqliteDB->isOpen());
</span><span class="cx"> 
</span><del>-    if (!createOrMigrateRecordsTableIfNecessary(*m_sqliteDB))
-        return false;
</del><ins>+    IDBError error = createOrMigrateRecordsTableIfNecessary(*m_sqliteDB);
+    if (!error.isNull())
+        return error;
</ins><span class="cx"> 
</span><span class="cx">     // Whether the updated records table already existed or if it was just created and the data migrated over,
</span><span class="cx">     // make sure the uniqueness index exists.
</span><del>-    if (!m_sqliteDB->executeCommand("CREATE UNIQUE INDEX IF NOT EXISTS RecordsIndex ON Records (objectStoreID, key);"_s)) {
-        LOG_ERROR("Could not create RecordsIndex on Records table in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!m_sqliteDB->executeCommand("CREATE UNIQUE INDEX IF NOT EXISTS RecordsIndex ON Records (objectStoreID, key);"_s))
+        error = IDBError { UnknownError, makeString("Error creating RecordsIndex on Records table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    return true;
</del><ins>+    return error;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool SQLiteIDBBackingStore::ensureValidIndexRecordsTable()
</del><ins>+IDBError SQLiteIDBBackingStore::ensureValidIndexRecordsTable()
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_sqliteDB);
</span><span class="cx">     ASSERT(m_sqliteDB->isOpen());
</span><span class="cx"> 
</span><del>-    String currentSchema;
-    {
-        // Fetch the schema for an existing index record table.
-        auto statement = m_sqliteDB->prepareStatement("SELECT type, sql FROM sqlite_master WHERE tbl_name='IndexRecords'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the IndexRecords table.");
-            return false;
-        }
</del><ins>+    String tableStatement = m_sqliteDB->tableSQL("IndexRecords"_s);
+    if (tableStatement.isEmpty()) {
+        if (!m_sqliteDB->executeCommand(v3IndexRecordsTableSchema()))
+            return IDBError { UnknownError, makeString("Error creating IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        int sqliteResult = statement->step();
-
-        // If there is no IndexRecords table at all, create it and then bail.
-        if (sqliteResult == SQLITE_DONE) {
-            if (!m_sqliteDB->executeCommand(v3IndexRecordsTableSchema())) {
-                LOG_ERROR("Could not create IndexRecords table in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-                return false;
-            }
-
-            return true;
-        }
-
-        if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the IndexRecords table.");
-            return false;
-        }
-
-        currentSchema = statement->columnText(1);
</del><ins>+        return IDBError { };
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    ASSERT(!currentSchema.isEmpty());
-
</del><span class="cx">     // If the schema in the backing store is the current schema, we're done.
</span><del>-    if (currentSchema == v3IndexRecordsTableSchema() || currentSchema == v3IndexRecordsTableSchemaAlternate())
-        return true;
</del><ins>+    if (tableStatement == v3IndexRecordsTableSchema() || tableStatement == v3IndexRecordsTableSchemaAlternate())
+        return IDBError { };
</ins><span class="cx"> 
</span><del>-    // If the record table is not the current schema then it must be one of the previous schemas.
-    // If it is not then the database is in an unrecoverable state and this should be considered a fatal error.
-    if (currentSchema != v1IndexRecordsTableSchema() && currentSchema != v1IndexRecordsTableSchemaAlternate()
-        && currentSchema != v2IndexRecordsTableSchema() && currentSchema != v2IndexRecordsTableSchemaAlternate())
-        RELEASE_ASSERT_NOT_REACHED();
</del><ins>+    RELEASE_ASSERT(tableStatement == v1IndexRecordsTableSchema() || tableStatement == v1IndexRecordsTableSchemaAlternate() || tableStatement == v2IndexRecordsTableSchema() || tableStatement == v2IndexRecordsTableSchemaAlternate());
</ins><span class="cx"> 
</span><span class="cx">     SQLiteTransaction transaction(*m_sqliteDB);
</span><span class="cx">     transaction.begin();
</span><span class="cx"> 
</span><span class="cx">     // Create a temporary table with the correct schema and migrate all existing content over.
</span><del>-    if (!m_sqliteDB->executeCommand(v3IndexRecordsTableSchemaTemp())) {
-        LOG_ERROR("Could not create temporary index records table in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!m_sqliteDB->executeCommand(v3IndexRecordsTableSchemaTemp()))
+        return IDBError { UnknownError, makeString("Error creating temporary IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    if (!m_sqliteDB->executeCommand("INSERT INTO _Temp_IndexRecords SELECT IndexRecords.indexID, IndexRecords.objectStoreID, IndexRecords.key, IndexRecords.value, Records.rowid FROM IndexRecords INNER JOIN Records ON Records.key = IndexRecords.value AND Records.objectStoreID = IndexRecords.objectStoreID"_s)) {
-        LOG_ERROR("Could not migrate existing IndexRecords content (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!m_sqliteDB->executeCommand("INSERT INTO _Temp_IndexRecords SELECT IndexRecords.indexID, IndexRecords.objectStoreID, IndexRecords.key, IndexRecords.value, Records.rowid FROM IndexRecords INNER JOIN Records ON Records.key = IndexRecords.value AND Records.objectStoreID = IndexRecords.objectStoreID"_s))
+        return IDBError { UnknownError, makeString("Error migrating IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    if (!m_sqliteDB->executeCommand("DROP TABLE IndexRecords"_s)) {
-        LOG_ERROR("Could not drop existing IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!m_sqliteDB->executeCommand("DROP TABLE IndexRecords"_s))
+        return IDBError { UnknownError, makeString("Error dropping IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-    if (!m_sqliteDB->executeCommand("ALTER TABLE _Temp_IndexRecords RENAME TO IndexRecords"_s)) {
-        LOG_ERROR("Could not rename temporary IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
</del><ins>+    if (!m_sqliteDB->executeCommand("ALTER TABLE _Temp_IndexRecords RENAME TO IndexRecords"_s))
+        return IDBError { UnknownError, makeString("Error renaming temporary IndexRecords table (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><span class="cx">     transaction.commit();
</span><span class="cx"> 
</span><del>-    return true;
</del><ins>+    return IDBError { };
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool SQLiteIDBBackingStore::ensureValidIndexRecordsIndex()
</del><ins>+IDBError SQLiteIDBBackingStore::ensureValidIndexRecordsIndex()
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_sqliteDB);
</span><span class="cx">     ASSERT(m_sqliteDB->isOpen());
</span><span class="cx"> 
</span><del>-    String currentSchema;
-    {
-        // Fetch the schema for an existing index record index.
-        auto statement = m_sqliteDB->prepareStatement("SELECT sql FROM sqlite_master WHERE name='IndexRecordsIndex'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the IndexRecordsIndex index.");
-            return false;
-        }
</del><ins>+    String indexStatement = m_sqliteDB->indexSQL("IndexRecordsIndex"_s);
+    if (indexStatement == IndexRecordsIndexSchema)
+        return IDBError { };
+    
+    if (!m_sqliteDB->executeCommand("DROP INDEX IF EXISTS IndexRecordsIndex"_s))
+        return IDBError { UnknownError, makeString("Error dropping IndexRecordsIndex index (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        int sqliteResult = statement->step();
</del><ins>+    if (!m_sqliteDB->executeCommand(IndexRecordsIndexSchema))
+        return IDBError { UnknownError, makeString("Error creating IndexRecordsIndex index (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        // If there is no IndexRecordsIndex index at all, create it and then bail.
-        if (sqliteResult == SQLITE_DONE) {
-            if (!m_sqliteDB->executeCommand(IndexRecordsIndexSchema)) {
-                LOG_ERROR("Could not create IndexRecordsIndex index in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-                return false;
-            }
-
-            return true;
-        }
-
-        if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the IndexRecordsIndex index.");
-            return false;
-        }
-
-        currentSchema = statement->columnText(0);
-    }
-
-    ASSERT(!currentSchema.isEmpty());
-
-    // If the schema in the backing store is the current schema, we're done.
-    if (currentSchema == IndexRecordsIndexSchema)
-        return true;
-
-    // Otherwise, update the schema.
-    SQLiteTransaction transaction(*m_sqliteDB);
-    transaction.begin();
-
-    if (!m_sqliteDB->executeCommand("DROP INDEX IndexRecordsIndex"_s)) {
-        LOG_ERROR("Could not drop index IndexRecordsIndex in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
-
-    if (!m_sqliteDB->executeCommand(IndexRecordsIndexSchema)) {
-        LOG_ERROR("Could not create IndexRecordsIndex index in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-        return false;
-    }
-
-    transaction.commit();
-
-    return true;
</del><ins>+    return IDBError { };
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool SQLiteIDBBackingStore::ensureValidIndexRecordsRecordIndex()
</del><ins>+IDBError SQLiteIDBBackingStore::ensureValidIndexRecordsRecordIndex()
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_sqliteDB);
</span><span class="cx">     ASSERT(m_sqliteDB->isOpen());
</span><span class="cx"> 
</span><del>-    String currentSchema;
-    {
-        auto statement = m_sqliteDB->prepareStatement("SELECT sql FROM sqlite_master WHERE name='IndexRecordsRecordIndex'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the IndexRecordsRecordIndex index.");
-            return false;
-        }
</del><ins>+    String indexStatement = m_sqliteDB->indexSQL("IndexRecordsRecordIndex"_s);
+    if (indexStatement == v1IndexRecordsRecordIndexSchema)
+        return IDBError { };
+    
+    if (!m_sqliteDB->executeCommand("DROP INDEX IF EXISTS IndexRecordsRecordIndex"_s))
+        return IDBError { UnknownError, makeString("Error dropping IndexRecordsRecordIndex index (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        int sqliteResult = statement->step();
</del><ins>+    if (!m_sqliteDB->executeCommand(v1IndexRecordsRecordIndexSchema))
+        return IDBError { UnknownError, makeString("Error creating IndexRecordsRecordIndex index (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg()) };
</ins><span class="cx"> 
</span><del>-        if (sqliteResult == SQLITE_DONE) {
-            if (!m_sqliteDB->executeCommand(v1IndexRecordsRecordIndexSchema)) {
-                LOG_ERROR("Could not create IndexRecordsRecordIndex index in database (%i) - %s", m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
-                return false;
-            }
-
-            return true;
-        }
-
-        if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the IndexRecordsRecordIndex index.");
-            return false;
-        }
-
-        currentSchema = statement->columnText(0);
-    }
-
-    ASSERT(!currentSchema.isEmpty());
-
-    if (currentSchema == v1IndexRecordsRecordIndexSchema)
-        return true;
-
-    return false;
</del><ins>+    return IDBError { };
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> std::unique_ptr<IDBDatabaseInfo> SQLiteIDBBackingStore::createAndPopulateInitialDatabaseInfo()
</span><span class="lines">@@ -665,33 +490,15 @@
</span><span class="cx">     ASSERT(m_sqliteDB);
</span><span class="cx">     ASSERT(m_sqliteDB->isOpen());
</span><span class="cx"> 
</span><del>-    String currentSchema;
-    {
-        // Fetch the schema for ObjectStoreInfo table.
-        auto statement = m_sqliteDB->prepareStatement("SELECT sql FROM sqlite_master WHERE tbl_name='ObjectStoreInfo'"_s);
-        if (!statement) {
-            LOG_ERROR("Unable to prepare statement to fetch schema for the ObjectStoreInfo table.");
-            return WTF::nullopt;
-        }
-
-        int sqliteResult = statement->step();
-        if (sqliteResult != SQLITE_ROW) {
-            LOG_ERROR("Error executing statement to fetch schema for the ObjectStoreInfo table.");
-            return WTF::nullopt;
-        }
-
-        currentSchema = statement->columnText(0);
-    }
-
-    ASSERT(!currentSchema.isEmpty());
-    if (currentSchema == v2ObjectStoreInfoSchema || currentSchema == createV2ObjectStoreInfoSchema(objectStoreInfoTableNameAlternate))
</del><ins>+    String tableStatement = m_sqliteDB->tableSQL("ObjectStoreInfo"_s);
+    if (tableStatement.isEmpty())
+        return WTF::nullopt;
+    
+    if (tableStatement == v2ObjectStoreInfoSchema || tableStatement == createV2ObjectStoreInfoSchema(objectStoreInfoTableNameAlternate))
</ins><span class="cx">         return { IsSchemaUpgraded::No };
</span><ins>+    
+    RELEASE_ASSERT(tableStatement == createV1ObjectStoreInfoSchema(objectStoreInfoTableName) || tableStatement == createV1ObjectStoreInfoSchema(objectStoreInfoTableNameAlternate));
</ins><span class="cx"> 
</span><del>-    if (currentSchema != createV1ObjectStoreInfoSchema(objectStoreInfoTableName) && currentSchema != createV1ObjectStoreInfoSchema(objectStoreInfoTableNameAlternate)) {
-        RELEASE_LOG_ERROR(IndexedDB, "%p - SQLiteIDBBackingStore::ensureValidObjectStoreInfoTable: schema is invalid - %s", this, currentSchema.utf8().data());
-        return WTF::nullopt;
-    }
-
</del><span class="cx">     // Drop column maxIndexID from table.
</span><span class="cx">     SQLiteTransaction transaction(*m_sqliteDB);
</span><span class="cx">     transaction.begin();
</span><span class="lines">@@ -1203,34 +1010,34 @@
</span><span class="cx">         return idbKeyCollate(aLength, a, bLength, b);
</span><span class="cx">     });
</span><span class="cx"> 
</span><del>-    if (!ensureValidRecordsTable()) {
-        LOG_ERROR("Error creating or migrating Records table in database");
</del><ins>+    IDBError error = ensureValidRecordsTable();
+    if (!error.isNull()) {
</ins><span class="cx">         closeSQLiteDB();
</span><del>-        return IDBError { UnknownError, "Error creating or migrating Records table in database"_s };
</del><ins>+        return error;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (!ensureValidIndexRecordsTable()) {
-        LOG_ERROR("Error creating or migrating Index Records table in database");
</del><ins>+    error = ensureValidIndexRecordsTable();
+    if (!error.isNull()) {
</ins><span class="cx">         closeSQLiteDB();
</span><del>-        return IDBError { UnknownError, "Error creating or migrating Index Records table in database"_s };
</del><ins>+        return error;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (!ensureValidIndexRecordsIndex()) {
-        LOG_ERROR("Error creating or migrating Index Records index in database");
</del><ins>+    error = ensureValidIndexRecordsIndex();
+    if (!error.isNull()) {
</ins><span class="cx">         closeSQLiteDB();
</span><del>-        return IDBError { UnknownError, "Error creating or migrating Index Records index in database"_s };
</del><ins>+        return error;
</ins><span class="cx">     }
</span><del>-
-    if (!ensureValidIndexRecordsRecordIndex()) {
-        LOG_ERROR("Error creating or migrating Index Records second index for in database");
</del><ins>+    
+    error = ensureValidIndexRecordsRecordIndex();
+    if (!error.isNull()) {
</ins><span class="cx">         closeSQLiteDB();
</span><del>-        return IDBError { UnknownError, "Error creating or migrating Index Records second index in database"_s };
</del><ins>+        return error;
</ins><span class="cx">     }
</span><del>-
-    if (!ensureValidBlobTables()) {
-        LOG_ERROR("Error creating or confirming Blob Records tables in database");
</del><ins>+    
+    error = ensureValidBlobTables();
+    if (!error.isNull()) {
</ins><span class="cx">         closeSQLiteDB();
</span><del>-        return IDBError { UnknownError, "Error creating or confirming Blob Records tables in database"_s };
</del><ins>+        return error;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     auto databaseInfo = extractExistingDatabaseInfo();
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesindexeddbserverSQLiteIDBBackingStoreh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h (278052 => 278053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h    2021-05-25 21:34:39 UTC (rev 278052)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h       2021-05-25 22:34:54 UTC (rev 278053)
</span><span class="lines">@@ -109,11 +109,11 @@
</span><span class="cx">     String fullDatabasePath() const;
</span><span class="cx">     String fullDatabaseDirectoryWithUpgrade();
</span><span class="cx"> 
</span><del>-    bool ensureValidRecordsTable();
-    bool ensureValidIndexRecordsTable();
-    bool ensureValidIndexRecordsIndex();
-    bool ensureValidIndexRecordsRecordIndex();
-    bool ensureValidBlobTables();
</del><ins>+    IDBError ensureValidRecordsTable();
+    IDBError ensureValidIndexRecordsTable();
+    IDBError ensureValidIndexRecordsIndex();
+    IDBError ensureValidIndexRecordsRecordIndex();
+    IDBError ensureValidBlobTables();
</ins><span class="cx">     Optional<IsSchemaUpgraded> ensureValidObjectStoreInfoTable();
</span><span class="cx">     std::unique_ptr<IDBDatabaseInfo> createAndPopulateInitialDatabaseInfo();
</span><span class="cx">     std::unique_ptr<IDBDatabaseInfo> extractExistingDatabaseInfo();
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesindexeddbserverUniqueIDBDatabasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp (278052 => 278053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp      2021-05-25 21:34:39 UTC (rev 278052)
+++ trunk/Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp 2021-05-25 22:34:54 UTC (rev 278053)
</span><span class="lines">@@ -195,9 +195,10 @@
</span><span class="cx">             m_backingStore = m_server.createBackingStore(m_identifier);
</span><span class="cx">             IDBDatabaseInfo databaseInfo;
</span><span class="cx">             backingStoreOpenError = m_backingStore->getOrEstablishDatabaseInfo(databaseInfo);
</span><del>-            if (backingStoreOpenError.isNull())
</del><ins>+            if (backingStoreOpenError.isNull()) {
+                LOG_ERROR("Failed to get database info '%s'", backingStoreOpenError.message().utf8().data());
</ins><span class="cx">                 m_databaseInfo = makeUnique<IDBDatabaseInfo>(databaseInfo);
</span><del>-            else
</del><ins>+            } else
</ins><span class="cx">                 m_backingStore = nullptr;
</span><span class="cx">         }
</span><span class="cx">     }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformsqlSQLiteDatabasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/sql/SQLiteDatabase.cpp (278052 => 278053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/sql/SQLiteDatabase.cpp     2021-05-25 21:34:39 UTC (rev 278052)
+++ trunk/Source/WebCore/platform/sql/SQLiteDatabase.cpp        2021-05-25 22:34:54 UTC (rev 278053)
</span><span class="lines">@@ -387,17 +387,33 @@
</span><span class="cx"> 
</span><span class="cx"> bool SQLiteDatabase::tableExists(const String& tableName)
</span><span class="cx"> {
</span><ins>+    return !tableSQL(tableName).isEmpty();
+}
+
+String SQLiteDatabase::tableSQL(const String& tableName)
+{
</ins><span class="cx">     if (!isOpen())
</span><del>-        return false;
</del><ins>+        return { };
</ins><span class="cx"> 
</span><del>-    auto statement = prepareStatement("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?;"_s);
-    if (!statement)
-        return false;
-    if (statement->bindText(1, tableName) != SQLITE_OK)
-        return false;
-    return statement->step() == SQLITE_ROW;
</del><ins>+    auto statement = prepareStatement("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?;"_s);
+    if (!statement || statement->bindText(1, tableName) != SQLITE_OK || statement->step() != SQLITE_ROW)
+        return { };
+
+    return statement->columnText(0);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+String SQLiteDatabase::indexSQL(const String& indexName)
+{
+    if (!isOpen())
+        return { };
+
+    auto statement = prepareStatement("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?;"_s);
+    if (!statement || statement->bindText(1, indexName) != SQLITE_OK || statement->step() != SQLITE_ROW)
+        return { };
+
+    return statement->columnText(0);
+}
+
</ins><span class="cx"> void SQLiteDatabase::clearAllTables()
</span><span class="cx"> {
</span><span class="cx">     auto statement = prepareStatement("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"_s);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformsqlSQLiteDatabaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/sql/SQLiteDatabase.h (278052 => 278053)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/sql/SQLiteDatabase.h       2021-05-25 21:34:39 UTC (rev 278052)
+++ trunk/Source/WebCore/platform/sql/SQLiteDatabase.h  2021-05-25 22:34:54 UTC (rev 278053)
</span><span class="lines">@@ -65,6 +65,8 @@
</span><span class="cx">     WEBCORE_EXPORT bool executeCommand(ASCIILiteral);
</span><span class="cx">     
</span><span class="cx">     WEBCORE_EXPORT bool tableExists(const String&);
</span><ins>+    WEBCORE_EXPORT String tableSQL(const String&);
+    WEBCORE_EXPORT String indexSQL(const String&);
</ins><span class="cx">     WEBCORE_EXPORT void clearAllTables();
</span><span class="cx">     WEBCORE_EXPORT int runVacuumCommand();
</span><span class="cx">     WEBCORE_EXPORT int runIncrementalVacuumCommand();
</span></span></pre>
</div>
</div>

</body>
</html>