Skip to content

Commit 1907a14

Browse files
committed
Introduce suggestion icon struct
1 parent a5b133a commit 1907a14

File tree

9 files changed

+377
-329
lines changed

9 files changed

+377
-329
lines changed

components/suggest/src/db.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
DownloadedWikipediaSuggestion, Record, SuggestRecordId,
2626
},
2727
schema::{clear_database, SuggestConnectionInitializer},
28-
suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion},
28+
suggestion::{cook_raw_suggestion_url, AmpSuggestionType, Suggestion, SuggestionIcon},
2929
Result, SuggestionQuery,
3030
};
3131

@@ -299,7 +299,7 @@ impl<'a> SuggestDao<'a> {
299299
amp.iab_category,
300300
amp.impression_url,
301301
amp.click_url,
302-
i.data AS icon,
302+
i.data AS icon_data,
303303
i.mimetype AS icon_mimetype
304304
FROM
305305
amp_custom_details amp
@@ -315,6 +315,12 @@ impl<'a> SuggestDao<'a> {
315315
let cooked_url = cook_raw_suggestion_url(&raw_url);
316316
let raw_click_url = row.get::<_, String>("click_url")?;
317317
let cooked_click_url = cook_raw_suggestion_url(&raw_click_url);
318+
let icon_data = row.get::<_, Option<_>>("icon_data")?;
319+
let icon_mime_type = row.get::<_, Option<_>>("icon_mimetype")?;
320+
let icon = icon_data.map(|data| SuggestionIcon {
321+
data,
322+
mime_type: icon_mime_type.unwrap_or_default(),
323+
});
318324

319325
Ok(Suggestion::Amp {
320326
block_id: row.get("block_id")?,
@@ -325,8 +331,7 @@ impl<'a> SuggestDao<'a> {
325331
raw_url,
326332
full_keyword: full_keyword_from_db
327333
.unwrap_or_else(|| full_keyword(keyword_lowercased, &keywords)),
328-
icon: row.get("icon")?,
329-
icon_mimetype: row.get("icon_mimetype")?,
334+
icon,
330335
impression_url: row.get("impression_url")?,
331336
click_url: cooked_click_url,
332337
raw_click_url,
@@ -378,7 +383,7 @@ impl<'a> SuggestDao<'a> {
378383
},
379384
|row| row.get(0),
380385
)?;
381-
let (icon, icon_mimetype) = self
386+
let icon = self
382387
.conn
383388
.try_query_row(
384389
"SELECT i.data, i.mimetype
@@ -390,21 +395,20 @@ impl<'a> SuggestDao<'a> {
390395
":suggestion_id": suggestion_id
391396
},
392397
|row| -> Result<_> {
393-
Ok((
394-
row.get::<_, Option<Vec<u8>>>(0)?,
395-
row.get::<_, Option<String>>(1)?,
396-
))
398+
Ok(Some(SuggestionIcon {
399+
data: row.get::<_, Vec<u8>>(0)?,
400+
mime_type: row.get::<_, String>(1)?,
401+
}))
397402
},
398403
true,
399404
)?
400-
.unwrap_or((None, None));
405+
.unwrap_or(None);
401406

402407
Ok(Suggestion::Wikipedia {
403408
title,
404409
url: raw_url,
405410
full_keyword: full_keyword(keyword_lowercased, &keywords),
406411
icon,
407-
icon_mimetype,
408412
})
409413
},
410414
)?;
@@ -986,7 +990,7 @@ impl<'a> SuggestDao<'a> {
986990
}
987991

988992
/// Inserts or replaces an icon for a suggestion into the database.
989-
pub fn put_icon(&mut self, icon_id: &str, data: &[u8], mimetype: &str) -> Result<()> {
993+
pub fn put_icon(&mut self, icon_id: &str, data: &[u8], mime_type: &str) -> Result<()> {
990994
self.conn.execute(
991995
"INSERT OR REPLACE INTO icons(
992996
id,
@@ -1001,7 +1005,7 @@ impl<'a> SuggestDao<'a> {
10011005
named_params! {
10021006
":id": icon_id,
10031007
":data": data,
1004-
":mimetype": mimetype,
1008+
":mimetype": mime_type,
10051009
},
10061010
)?;
10071011
Ok(())

components/suggest/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use error::SuggestApiError;
2626
pub use provider::SuggestionProvider;
2727
pub use query::SuggestionQuery;
2828
pub use store::{InterruptKind, SuggestIngestionConstraints, SuggestStore, SuggestStoreBuilder};
29-
pub use suggestion::{raw_suggestion_url_matches, Suggestion};
29+
pub use suggestion::{raw_suggestion_url_matches, Suggestion, SuggestionIcon};
3030

3131
pub(crate) type Result<T> = std::result::Result<T, error::Error>;
3232
pub type SuggestApiResult<T> = std::result::Result<T, error::SuggestApiError>;

components/suggest/src/schema.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sql_support::open_database::{self, ConnectionInitializer};
1515
/// [`SuggestConnectionInitializer::upgrade_from`].
1616
/// a. If suggestions should be re-ingested after the migration, call `clear_database()` inside
1717
/// the migration.
18-
pub const VERSION: u32 = 19;
18+
pub const VERSION: u32 = 20;
1919

2020
/// The current Suggest database schema.
2121
pub const SQL: &str = "
@@ -195,6 +195,34 @@ CREATE TABLE IF NOT EXISTS dismissed_suggestions (
195195
)?;
196196
Ok(())
197197
}
198+
19 => {
199+
let result = tx.query_row_and_then(
200+
"SELECT icon_id FROM yelp_custom_details LIMIT 1",
201+
(),
202+
|row| row.get::<_, String>(0),
203+
);
204+
205+
if let Ok(_) = result {
206+
// As still old table, we chnage to new.
207+
tx.execute_batch(
208+
"
209+
ALTER TABLE yelp_custom_details RENAME TO old_yelp_custom_details;
210+
CREATE TABLE yelp_custom_details(
211+
icon_light_theme_id TEXT NOT NULL,
212+
icon_dark_theme_id TEXT NOT NULL,
213+
score REAL NOT NULL,
214+
record_id TEXT NOT NULL,
215+
PRIMARY KEY (icon_light_theme_id, icon_dark_theme_id)
216+
) WITHOUT ROWID;
217+
INSERT INTO yelp_custom_details (icon_light_theme_id, icon_dark_theme_id, score, record_id)
218+
SELECT icon_id, icon_id, score, record_id FROM old_yelp_custom_details;
219+
DROP TABLE old_yelp_custom_details;
220+
",
221+
)?;
222+
}
223+
224+
Ok(())
225+
}
198226
_ => Err(open_database::Error::IncompatibleVersion(version)),
199227
}
200228
}
@@ -322,6 +350,8 @@ CREATE TABLE yelp_custom_details(
322350
record_id TEXT NOT NULL
323351
) WITHOUT ROWID;
324352
353+
INSERT INTO yelp_custom_details(icon_id, score, record_id) VALUES ('yelp-icon', 0.5, 'yelp-record');
354+
325355
CREATE TABLE mdn_custom_details(
326356
suggestion_id INTEGER PRIMARY KEY,
327357
description TEXT NOT NULL,
@@ -340,5 +370,27 @@ PRAGMA user_version=16;
340370
let db_file = MigratedDatabaseFile::new(SuggestConnectionInitializer, V16_SCHEMA);
341371
db_file.run_all_upgrades();
342372
db_file.assert_schema_matches_new_database();
373+
374+
// Test wether yelp_custom_details data inherits to new table.
375+
let connection = db_file.open();
376+
let result: Result<(String, String, f64, String), crate::error::Error> = connection.query_row_and_then(
377+
"SELECT icon_light_theme_id, icon_dark_theme_id, score, record_id FROM yelp_custom_details LIMIT 1",
378+
(),
379+
|row| Ok((
380+
row.get::<_, String>(0)?,
381+
row.get::<_, String>(1)?,
382+
row.get::<_, f64>(2)?,
383+
row.get::<_, String>(3)?
384+
))
385+
);
386+
387+
if let Ok((icon_light_theme_id, icon_dark_theme_id, score, record_id)) = result {
388+
assert_eq!(icon_light_theme_id, "yelp-icon");
389+
assert_eq!(icon_dark_theme_id, "yelp-icon");
390+
assert_eq!(score, 0.5);
391+
assert_eq!(record_id, "yelp-record");
392+
} else {
393+
assert!(false);
394+
}
343395
}
344396
}

0 commit comments

Comments
 (0)