From cf1be330447a5f20202cc70e32bcc52677056dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Oliveira?= Date: Wed, 10 Feb 2021 19:46:16 +0100 Subject: [PATCH] Add code to migrate database model Add code to migrate database model when `authors` column is not present in the index This recreates `search` table and resets the articles index Added missing or code in error --- .../Sources/ArticlesDatabase/ArticlesDatabase.swift | 5 +++++ .../Sources/ArticlesDatabase/ArticlesTable.swift | 4 ++-- ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index e2a62b8d0..800cb0f4c 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -63,6 +63,7 @@ public final class ArticlesDatabase { } private let articlesTable: ArticlesTable + private let searchTable: SearchTable private let queue: DatabaseQueue private let operationQueue = MainThreadOperationQueue() private let retentionStyle: RetentionStyle @@ -71,6 +72,7 @@ public final class ArticlesDatabase { let queue = DatabaseQueue(databasePath: databaseFilePath) self.queue = queue self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue, retentionStyle: retentionStyle) + self.searchTable = SearchTable(queue: queue, articlesTable: self.articlesTable) self.retentionStyle = retentionStyle try! queue.runCreateStatements(ArticlesDatabase.tableCreationStatements) @@ -81,6 +83,9 @@ public final class ArticlesDatabase { } database.executeStatements("CREATE INDEX if not EXISTS articles_searchRowID on articles(searchRowID);") database.executeStatements("DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;DROP INDEX if EXISTS articles_feedID_index;DROP INDEX if EXISTS statuses_read_index;DROP TABLE if EXISTS attachments;DROP TABLE if EXISTS attachmentsLookup;") + if !self.searchTable.containsColumn("authors", in: database) { + database.executeStatements("DROP TABLE if EXISTS search;CREATE VIRTUAL TABLE if not EXISTS search using fts4(title, body, authors);UPDATE articles SET searchRowID = null;") + } } DispatchQueue.main.async { diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index 0f9533c74..2a925612e 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -158,8 +158,8 @@ final class ArticlesTable: DatabaseTable { art.contentText, art.summary, art.searchRowID, - (SELECT GROUP_CONCAT(name SEPARATOR ' ') - FROM authorLookup as autL + (SELECT GROUP_CONCAT(name, ' ') + FROM authorsLookup as autL JOIN authors as aut ON autL.authorID = aut.authorID WHERE art.articleID = autL.articleID GROUP BY autl.articleID) as authors diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift index 788ac6418..acb6a2e64 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift @@ -61,7 +61,7 @@ final class ArticleSearchInfo: Hashable { // MARK: Equatable static func == (lhs: ArticleSearchInfo, rhs: ArticleSearchInfo) -> Bool { - return lhs.articleID == rhs.articleID && lhs.title == rhs.title && lhs.contentHTML == rhs.contentHTML && lhs.contentText == rhs.contentText && lhs.summary == rhs.summary && lhs.searchRowID == rhs.searchRowID + return lhs.articleID == rhs.articleID && lhs.title == rhs.title && lhs.authorsNames == rhs.authorsNames && lhs.contentHTML == rhs.contentHTML && lhs.contentText == rhs.contentText && lhs.summary == rhs.summary && lhs.searchRowID == rhs.searchRowID } } @@ -203,7 +203,7 @@ private extension SearchTable { return nil } let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(searchRowIDs.count))! - let sql = "select rowid, title, body from \(name) where rowid in \(placeholders);" + let sql = "select rowid, title, body, authors from \(name) where rowid in \(placeholders);" guard let resultSet = database.executeQuery(sql, withArgumentsIn: searchRowIDs) else { return nil }