Skip to content

Commit 7d62724

Browse files
committed
Introduce suggestion icon struct
1 parent a5b133a commit 7d62724

File tree

9 files changed

+367
-329
lines changed

9 files changed

+367
-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: 43 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,24 @@ CREATE TABLE IF NOT EXISTS dismissed_suggestions (
195195
)?;
196196
Ok(())
197197
}
198+
19 => {
199+
tx.execute_batch(
200+
"
201+
ALTER TABLE yelp_custom_details RENAME TO old_yelp_custom_details;
202+
CREATE TABLE yelp_custom_details(
203+
icon_light_theme_id TEXT NOT NULL,
204+
icon_dark_theme_id TEXT NOT NULL,
205+
score REAL NOT NULL,
206+
record_id TEXT NOT NULL,
207+
PRIMARY KEY (icon_light_theme_id, icon_dark_theme_id)
208+
) WITHOUT ROWID;
209+
INSERT INTO yelp_custom_details (icon_light_theme_id, icon_dark_theme_id, score, record_id)
210+
SELECT icon_id, icon_id, score, record_id FROM old_yelp_custom_details;
211+
DROP TABLE old_yelp_custom_details;
212+
",
213+
)?;
214+
Ok(())
215+
}
198216
_ => Err(open_database::Error::IncompatibleVersion(version)),
199217
}
200218
}
@@ -322,6 +340,8 @@ CREATE TABLE yelp_custom_details(
322340
record_id TEXT NOT NULL
323341
) WITHOUT ROWID;
324342
343+
INSERT INTO yelp_custom_details(icon_id, score, record_id) VALUES ('yelp-icon', 0.5, 'yelp-record');
344+
325345
CREATE TABLE mdn_custom_details(
326346
suggestion_id INTEGER PRIMARY KEY,
327347
description TEXT NOT NULL,
@@ -340,5 +360,27 @@ PRAGMA user_version=16;
340360
let db_file = MigratedDatabaseFile::new(SuggestConnectionInitializer, V16_SCHEMA);
341361
db_file.run_all_upgrades();
342362
db_file.assert_schema_matches_new_database();
363+
364+
// Test wether yelp_custom_details data inherits to new table.
365+
let connection = db_file.open();
366+
let result: Result<(String, String, f64, String), crate::error::Error> = connection.query_row_and_then(
367+
"SELECT icon_light_theme_id, icon_dark_theme_id, score, record_id FROM yelp_custom_details LIMIT 1",
368+
(),
369+
|row| Ok((
370+
row.get::<_, String>(0)?,
371+
row.get::<_, String>(1)?,
372+
row.get::<_, f64>(2)?,
373+
row.get::<_, String>(3)?
374+
))
375+
);
376+
377+
if let Ok((icon_light_theme_id, icon_dark_theme_id, score, record_id)) = result {
378+
assert_eq!(icon_light_theme_id, "yelp-icon");
379+
assert_eq!(icon_dark_theme_id, "yelp-icon");
380+
assert_eq!(score, 0.5);
381+
assert_eq!(record_id, "yelp-record");
382+
} else {
383+
assert!(false);
384+
}
343385
}
344386
}

0 commit comments

Comments
 (0)