-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist remote tabs to a sql database.
- Loading branch information
Showing
10 changed files
with
366 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
#[macro_use] | ||
pub mod error; | ||
mod schema; | ||
mod storage; | ||
mod sync; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
// Tabs is a bit special - it's a trivial SQL schema and is only used as a persistent | ||
// cache, and the semantics of the "tabs" collection means there's no need for | ||
// syncChangeCounter/syncStatus nor a mirror etc. | ||
|
||
use rusqlite::{Connection, Transaction}; | ||
use sql_support::open_database::{ | ||
ConnectionInitializer as MigrationLogic, Error as MigrationError, Result as MigrationResult, | ||
}; | ||
|
||
// The payload is json and this module doesn't need to deserialize, so we just | ||
// store each "payload" as a row. | ||
// We delete the entire DB each sync and re-populate it with every record on | ||
// the server. When we read the DB, we also read every single record. | ||
// So we have no primary keys, no foreign keys, and really completely waste the | ||
// fact we are using sql. | ||
const CREATE_SCHEMA_SQL: &str = " | ||
CREATE TABLE IF NOT EXISTS tabs ( | ||
payload TEXT NOT NULL | ||
); | ||
"; | ||
|
||
pub struct TabsMigrationLogin; | ||
|
||
impl MigrationLogic for TabsMigrationLogin { | ||
const NAME: &'static str = "tabs storage db"; | ||
const END_VERSION: u32 = 1; | ||
|
||
fn prepare(&self, conn: &Connection) -> MigrationResult<()> { | ||
let initial_pragmas = " | ||
-- We don't care about temp tables being persisted to disk. | ||
PRAGMA temp_store = 2; | ||
-- we unconditionally want write-ahead-logging mode. | ||
PRAGMA journal_mode=WAL; | ||
-- foreign keys seem worth enforcing (and again, we don't care in practice) | ||
PRAGMA foreign_keys = ON; | ||
"; | ||
conn.execute_batch(initial_pragmas)?; | ||
// This is where we'd define our sql functions if we had any! | ||
conn.set_prepared_statement_cache_capacity(128); | ||
Ok(()) | ||
} | ||
|
||
fn init(&self, db: &Transaction<'_>) -> MigrationResult<()> { | ||
log::debug!("Creating schema"); | ||
db.execute_batch(CREATE_SCHEMA_SQL)?; | ||
Ok(()) | ||
} | ||
|
||
fn upgrade_from(&self, _db: &Transaction<'_>, version: u32) -> MigrationResult<()> { | ||
Err(MigrationError::IncompatibleVersion(version)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::storage::TabsStorage; | ||
|
||
#[test] | ||
fn test_create_schema_twice() { | ||
let mut db = TabsStorage::new_with_mem_path("test"); | ||
let conn = db.open_or_create().unwrap(); | ||
conn.execute_batch(CREATE_SCHEMA_SQL) | ||
.expect("should allow running twice"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.