Reverting to single step persist

This commit is contained in:
relikd
2020-08-01 09:42:57 +02:00
parent a3973c7e9a
commit 4af56b0cb1

View File

@@ -22,23 +22,9 @@ extension SQLiteDatabase {
} }
if version != 1 { if version != 1 {
// version 0 -> 1: req(domain) -> heap(fqdn, domain) // version 0 -> 1: req(domain) -> heap(fqdn, domain)
if version == 0 {
try tempMigrate()
}
try run(sql: "PRAGMA user_version = 1;") try run(sql: "PRAGMA user_version = 1;")
} }
} }
private func tempMigrate() throws { // TODO: remove with next internal release
do {
try run(sql: "SELECT 1 FROM req LIMIT 1;") // fails if req doesnt exist
createFunction("domainof") { ($0.first as! String).extractDomain() }
transaction("""
INSERT INTO heap(ts,fqdn,domain,opt) SELECT ts,domain,domainof(domain),nullif(logOpt,0) FROM req;
DROP TABLE req;
""")
} catch { /* no need to migrate */ }
}
} }
private enum TableName: String { private enum TableName: String {
@@ -117,12 +103,11 @@ extension SQLiteDatabase {
/// - Returns: `nil` in case no entries were transmitted. /// - Returns: `nil` in case no entries were transmitted.
@discardableResult func dnsLogsPersist() -> SQLiteRowRange? { @discardableResult func dnsLogsPersist() -> SQLiteRowRange? {
guard lastRowId(.cache) > 0 else { return nil } guard lastRowId(.cache) > 0 else { return nil }
transaction("ALTER TABLE cache RENAME TO tmp_cache; \(CreateTable.cache)")
let before = lastRowId(.heap) + 1 let before = lastRowId(.heap) + 1
createFunction("domainof") { ($0.first as! String).extractDomain() } createFunction("domainof") { ($0.first as! String).extractDomain() }
transaction(""" transaction("""
INSERT INTO heap(ts,fqdn,domain,opt) SELECT ts,dns,domainof(dns),nullif(opt&1,0) FROM tmp_cache; INSERT INTO heap(ts,fqdn,domain,opt) SELECT ts,dns,domainof(dns),nullif(opt&1,0) FROM cache;
DROP TABLE tmp_cache; DELETE FROM cache;
""") """)
let after = lastRowId(.heap) let after = lastRowId(.heap)
return (before > after) ? nil : (before, after) return (before > after) ? nil : (before, after)