1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::api::places_api::PlacesApi;
use crate::bookmark_sync::store::BookmarksStore;
use crate::db::db::PlacesDb;
use crate::error::*;
use crate::import::common::attached_database;
use rusqlite::Connection;
use serde_derive::*;
use sql_support::ConnExt;
use std::time::Instant;
use url::Url;
const FENNEC_DB_VERSION: i64 = 34;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Default)]
pub struct HistoryMigrationResult {
pub num_total: u32,
pub num_succeeded: u32,
pub num_failed: u32,
pub total_duration: u128,
}
pub fn import(
places_api: &PlacesApi,
path: impl AsRef<std::path::Path>,
) -> Result<HistoryMigrationResult> {
let url = crate::util::ensure_url_path(path)?;
do_import(places_api, url)
}
pub fn select_count(conn: &PlacesDb, stmt: &str) -> u32 {
let count: Result<Option<u32>> =
conn.try_query_row(stmt, &[], |row| Ok(row.get::<_, u32>(0)?), false);
count.unwrap().unwrap()
}
fn do_import(places_api: &PlacesApi, android_db_file_url: Url) -> Result<HistoryMigrationResult> {
let conn = places_api.open_sync_connection()?;
let scope = conn.begin_interrupt_scope();
define_sql_functions(&conn)?;
let import_start = Instant::now();
log::trace!("Attaching database {}", android_db_file_url);
let auto_detach = attached_database(&conn, &android_db_file_url, "fennec")?;
let db_version = conn.db.query_one::<i64>("PRAGMA fennec.user_version")?;
if db_version < FENNEC_DB_VERSION {
return Err(ErrorKind::UnsupportedDatabaseVersion(db_version).into());
}
let tx = conn.begin_transaction()?;
log::debug!("Counting Fennec history visits");
let num_total = select_count(&conn, &COUNT_FENNEC_HISTORY_VISITS);
log::debug!("Creating and populating staging table");
conn.execute_batch(&CREATE_STAGING_TABLE)?;
conn.execute_batch(&FILL_STAGING)?;
log::debug!("Populating missing entries in moz_places");
conn.execute_batch(&FILL_MOZ_PLACES)?;
scope.err_if_interrupted()?;
log::debug!("Inserting the history visits");
conn.execute_batch(&INSERT_HISTORY_VISITS)?;
scope.err_if_interrupted()?;
log::debug!("Committing...");
tx.commit()?;
log::debug!("Updating frecencies");
let store = BookmarksStore::new(&conn, &scope);
store.update_frecencies()?;
log::info!("Successfully imported history visits!");
log::debug!("Counting Fenix history visits");
let num_succeeded = select_count(&conn, &COUNT_FENIX_HISTORY_VISITS);
let num_failed = num_total - num_succeeded;
auto_detach.execute_now()?;
let metrics = HistoryMigrationResult {
num_total,
num_succeeded,
num_failed,
total_duration: import_start.elapsed().as_millis(),
};
Ok(metrics)
}
lazy_static::lazy_static! {
static ref CREATE_STAGING_TABLE: &'static str = "
CREATE TEMP TABLE temp.fennecHistoryStaging(
guid TEXT PRIMARY KEY,
url TEXT,
url_hash INTEGER NOT NULL,
title TEXT
) WITHOUT ROWID;"
;
static ref FILL_STAGING: &'static str = "
INSERT OR IGNORE INTO temp.fennecHistoryStaging(guid, url, url_hash, title)
SELECT
sanitize_utf8(guid), -- The places record in our DB may be different, but we
-- need this to join to Fennec's visits table.
validate_url(h.url),
hash(validate_url(h.url)),
sanitize_utf8(h.title)
FROM fennec.history h
WHERE url IS NOT NULL"
;
static ref FILL_MOZ_PLACES: &'static str =
"INSERT OR IGNORE INTO main.moz_places(guid, url, url_hash, title, frecency, sync_change_counter)
SELECT
IFNULL(
(SELECT p.guid FROM main.moz_places p WHERE p.url_hash = t.url_hash AND p.url = t.url),
generate_guid()
),
t.url,
t.url_hash,
t.title,
-1,
1
FROM temp.fennecHistoryStaging t"
;
static ref INSERT_HISTORY_VISITS: &'static str =
"INSERT OR IGNORE INTO main.moz_historyvisits(from_visit, place_id, visit_date, visit_type, is_local)
SELECT
NULL, -- Fenec does not store enough information to rebuild redirect chains.
(SELECT p.id FROM main.moz_places p WHERE p.url_hash = t.url_hash AND p.url = t.url),
sanitize_timestamp(v.date),
v.visit_type, -- Fennec stores visit types maps 1:1 to ours.
v.is_local
FROM fennec.visits v
-- Note that we *do not* `sanitize_utf8(v.history_guid)` here due to
-- perf concerns. It just means if there happens to be non-utf8
-- guids in both tables we will not migrate their visits - which
-- seems fine as it should impact ~ 0 users.
LEFT JOIN temp.fennecHistoryStaging t on v.history_guid = t.guid"
;
static ref COUNT_FENNEC_HISTORY_VISITS: &'static str =
"SELECT COUNT(*) FROM fennec.visits"
;
static ref COUNT_FENIX_HISTORY_VISITS: &'static str =
"SELECT COUNT(*) FROM main.moz_historyvisits"
;
}
pub(super) fn define_sql_functions(c: &Connection) -> Result<()> {
use rusqlite::functions::FunctionFlags;
c.create_scalar_function(
"validate_url",
1,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
crate::import::common::sql_fns::validate_url,
)?;
c.create_scalar_function(
"sanitize_timestamp",
1,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
crate::import::common::sql_fns::sanitize_timestamp,
)?;
c.create_scalar_function(
"hash",
-1,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
crate::db::db::sql_fns::hash,
)?;
c.create_scalar_function(
"generate_guid",
0,
FunctionFlags::SQLITE_UTF8,
crate::db::db::sql_fns::generate_guid,
)?;
c.create_scalar_function(
"sanitize_utf8",
1,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
crate::import::common::sql_fns::sanitize_utf8,
)?;
Ok(())
}