mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 23:42:23 -04:00
use separate schema version for tag changes
Will prevent issues if user upgrades after an unclean shutdown
This commit is contained in:
parent
2cff078b50
commit
45ea0ddefd
8 changed files with 49 additions and 36 deletions
|
@ -5,5 +5,6 @@ mod card;
|
||||||
mod deckconf;
|
mod deckconf;
|
||||||
mod sqlite;
|
mod sqlite;
|
||||||
mod tag;
|
mod tag;
|
||||||
|
mod upgrades;
|
||||||
|
|
||||||
pub(crate) use sqlite::SqliteStorage;
|
pub(crate) use sqlite::SqliteStorage;
|
||||||
|
|
|
@ -23,7 +23,7 @@ use unicase::UniCase;
|
||||||
|
|
||||||
const SCHEMA_MIN_VERSION: u8 = 11;
|
const SCHEMA_MIN_VERSION: u8 = 11;
|
||||||
const SCHEMA_STARTING_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 {
|
fn unicase_compare(s1: &str, s2: &str) -> Ordering {
|
||||||
UniCase::new(s1).cmp(&UniCase::new(s2))
|
UniCase::new(s1).cmp(&UniCase::new(s2))
|
||||||
|
@ -204,35 +204,6 @@ impl SqliteStorage {
|
||||||
Ok(storage)
|
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
|
// Standard transaction start/stop
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ impl SqliteStorage {
|
||||||
Ok(())
|
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 alltags = self.all_tags()?;
|
||||||
let tagsmap: HashMap<String, Usn> = alltags.into_iter().collect();
|
let tagsmap: HashMap<String, Usn> = alltags.into_iter().collect();
|
||||||
self.db.execute(
|
self.db.execute(
|
||||||
|
|
35
rslib/src/storage/upgrades/mod.rs
Normal file
35
rslib/src/storage/upgrades/mod.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
drop table deck_config;
|
drop table deck_config;
|
||||||
drop table tags;
|
|
||||||
update col
|
update col
|
||||||
set
|
set
|
||||||
ver = 11;
|
ver = 11;
|
|
@ -5,10 +5,6 @@ create table deck_config (
|
||||||
usn integer not null,
|
usn integer not null,
|
||||||
config text 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
|
update col
|
||||||
set
|
set
|
||||||
ver = 12;
|
ver = 12;
|
4
rslib/src/storage/upgrades/schema13_downgrade.sql
Normal file
4
rslib/src/storage/upgrades/schema13_downgrade.sql
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
drop table tags;
|
||||||
|
update col
|
||||||
|
set
|
||||||
|
ver = 12;
|
7
rslib/src/storage/upgrades/schema13_upgrade.sql
Normal file
7
rslib/src/storage/upgrades/schema13_upgrade.sql
Normal 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;
|
Loading…
Reference in a new issue