use separate schema version for tag changes

Will prevent issues if user upgrades after an unclean shutdown
This commit is contained in:
Damien Elmes 2020-04-04 10:14:51 +10:00
parent 2cff078b50
commit 45ea0ddefd
8 changed files with 49 additions and 36 deletions

View File

@ -5,5 +5,6 @@ mod card;
mod deckconf;
mod sqlite;
mod tag;
mod upgrades;
pub(crate) use sqlite::SqliteStorage;

View File

@ -23,7 +23,7 @@ use unicase::UniCase;
const SCHEMA_MIN_VERSION: u8 = 11;
const SCHEMA_STARTING_VERSION: u8 = 11;
const SCHEMA_MAX_VERSION: u8 = 12;
const SCHEMA_MAX_VERSION: u8 = 13;
fn unicase_compare(s1: &str, s2: &str) -> Ordering {
UniCase::new(s1).cmp(&UniCase::new(s2))
@ -204,35 +204,6 @@ impl SqliteStorage {
Ok(storage)
}
fn upgrade_to_latest_schema(&self, ver: u8) -> Result<()> {
if ver < 12 {
self.upgrade_to_schema_12()?;
}
Ok(())
}
pub(crate) fn downgrade_to_schema_11(self) -> Result<()> {
self.begin_trx()?;
self.downgrade_from_schema_12()?;
self.commit_trx()
}
fn upgrade_to_schema_12(&self) -> Result<()> {
self.db
.execute_batch(include_str!("schema12_upgrade.sql"))?;
self.upgrade_deck_conf_to_schema12()?;
self.upgrade_tags_to_schema12()
}
fn downgrade_from_schema_12(&self) -> Result<()> {
self.downgrade_tags_from_schema12()?;
self.downgrade_deck_conf_from_schema12()?;
self.db
.execute_batch(include_str!("schema12_downgrade.sql"))?;
Ok(())
}
// Standard transaction start/stop
//////////////////////////////////////

View File

@ -74,7 +74,7 @@ impl SqliteStorage {
Ok(())
}
pub(super) fn downgrade_tags_from_schema12(&self) -> Result<()> {
pub(super) fn downgrade_tags_from_schema13(&self) -> Result<()> {
let alltags = self.all_tags()?;
let tagsmap: HashMap<String, Usn> = alltags.into_iter().collect();
self.db.execute(

View File

@ -0,0 +1,35 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::SqliteStorage;
use crate::err::Result;
impl SqliteStorage {
pub(super) fn upgrade_to_latest_schema(&self, ver: u8) -> Result<()> {
if ver < 12 {
self.db
.execute_batch(include_str!("schema12_upgrade.sql"))?;
self.upgrade_deck_conf_to_schema12()?;
}
if ver < 13 {
self.db
.execute_batch(include_str!("schema13_upgrade.sql"))?;
self.upgrade_tags_to_schema12()?;
}
Ok(())
}
pub(crate) fn downgrade_to_schema_11(self) -> Result<()> {
self.begin_trx()?;
self.downgrade_tags_from_schema13()?;
self.db
.execute_batch(include_str!("schema13_downgrade.sql"))?;
self.downgrade_deck_conf_from_schema12()?;
self.db
.execute_batch(include_str!("schema12_downgrade.sql"))?;
self.commit_trx()
}
}

View File

@ -1,5 +1,4 @@
drop table deck_config;
drop table tags;
update col
set
ver = 11;

View File

@ -5,10 +5,6 @@ create table deck_config (
usn integer not null,
config text not null
);
create table tags (
tag text not null primary key collate unicase,
usn integer not null
) without rowid;
update col
set
ver = 12;

View File

@ -0,0 +1,4 @@
drop table tags;
update col
set
ver = 12;

View File

@ -0,0 +1,7 @@
create table tags (
tag text not null primary key collate unicase,
usn integer not null
) without rowid;
update col
set
ver = 13;