From b4887b661c35ee5b167ca83ec77e08f77ff5c9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 1 Dec 2017 13:14:24 +0100 Subject: [PATCH] Tests for Database.dropFTS4SynchronizationTriggers & Database.dropFTS5SynchronizationTriggers --- Tests/GRDBTests/FTS4TableBuilderTests.swift | 59 +++++++++++++++++++++ Tests/GRDBTests/FTS5TableBuilderTests.swift | 59 +++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/Tests/GRDBTests/FTS4TableBuilderTests.swift b/Tests/GRDBTests/FTS4TableBuilderTests.swift index 4846070a6..c20ffe114 100644 --- a/Tests/GRDBTests/FTS4TableBuilderTests.swift +++ b/Tests/GRDBTests/FTS4TableBuilderTests.swift @@ -229,4 +229,63 @@ class FTS4TableBuilderTests: GRDBTestCase { XCTAssertEqual(try Int.fetchOne(db, "SELECT COUNT(*) FROM ft_documents WHERE ft_documents MATCH ?", arguments: ["bar"])!, 1) } } + + func testFTS4SynchronizationCleanup() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "documents") { t in + t.column("id", .integer).primaryKey() + t.column("content", .text) + } + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.create(virtualTable: "ft_documents", using: FTS4()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + + try db.drop(table: "ft_documents") + try db.dropFTS4SynchronizationTriggers(forTable: "ft_documents") + + // It is possible to modify the content table + try db.execute("UPDATE documents SET content = ?", arguments: ["bar"]) + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.execute("DELETE FROM documents WHERE content = ?", arguments: ["foo"]) + + // It is possible to recreate the FT table + try db.create(virtualTable: "ft_documents", using: FTS4()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + } + } + + func testFTS4SynchronizationCleanupWithLegacySupport() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "documents") { t in + t.column("id", .integer).primaryKey() + t.column("content", .text) + } + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.create(virtualTable: "ft_documents", using: FTS4()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + + try db.drop(table: "ft_documents") + try db.dropFTS4SynchronizationTriggers(forTable: "documents") // legacy GRDB <= 2.3.1 + try db.dropFTS4SynchronizationTriggers(forTable: "ft_documents") + + // It is possible to modify the content table + try db.execute("UPDATE documents SET content = ?", arguments: ["bar"]) + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.execute("DELETE FROM documents WHERE content = ?", arguments: ["foo"]) + + // It is possible to recreate the FT table + try db.create(virtualTable: "ft_documents", using: FTS4()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + } + } } diff --git a/Tests/GRDBTests/FTS5TableBuilderTests.swift b/Tests/GRDBTests/FTS5TableBuilderTests.swift index 8e0e086f0..c18eea07d 100644 --- a/Tests/GRDBTests/FTS5TableBuilderTests.swift +++ b/Tests/GRDBTests/FTS5TableBuilderTests.swift @@ -219,5 +219,64 @@ class FTS5TableBuilderTests: GRDBTestCase { XCTAssertEqual(try Int.fetchOne(db, "SELECT COUNT(*) FROM ft_documents WHERE ft_documents MATCH ?", arguments: ["bar"])!, 1) } } + + func testFTS5SynchronizationCleanup() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "documents") { t in + t.column("id", .integer).primaryKey() + t.column("content", .text) + } + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.create(virtualTable: "ft_documents", using: FTS5()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + + try db.drop(table: "ft_documents") + try db.dropFTS5SynchronizationTriggers(forTable: "ft_documents") + + // It is possible to modify the content table + try db.execute("UPDATE documents SET content = ?", arguments: ["bar"]) + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.execute("DELETE FROM documents WHERE content = ?", arguments: ["foo"]) + + // It is possible to recreate the FT table + try db.create(virtualTable: "ft_documents", using: FTS5()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + } + } + + func testFTS5SynchronizationCleanupWithLegacySupport() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "documents") { t in + t.column("id", .integer).primaryKey() + t.column("content", .text) + } + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.create(virtualTable: "ft_documents", using: FTS5()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + + try db.drop(table: "ft_documents") + try db.dropFTS5SynchronizationTriggers(forTable: "documents") // legacy GRDB <= 2.3.1 + try db.dropFTS5SynchronizationTriggers(forTable: "ft_documents") + + // It is possible to modify the content table + try db.execute("UPDATE documents SET content = ?", arguments: ["bar"]) + try db.execute("INSERT INTO documents (content) VALUES (?)", arguments: ["foo"]) + try db.execute("DELETE FROM documents WHERE content = ?", arguments: ["foo"]) + + // It is possible to recreate the FT table + try db.create(virtualTable: "ft_documents", using: FTS5()) { t in + t.synchronize(withTable: "documents") + t.column("content") + } + } + } } #endif