zcash_client_sqlite/chain/
init.rs

1//! Functions for initializing the various databases.
2use crate::BlockDb;
3
4#[cfg(feature = "unstable")]
5use {
6    super::migrations,
7    crate::FsBlockDb,
8    schemerz::{Migrator, MigratorError},
9    schemerz_rusqlite::RusqliteAdapter,
10};
11
12/// Sets up the internal structure of the cache database.
13///
14/// # Examples
15///
16/// ```
17/// use tempfile::NamedTempFile;
18/// use zcash_client_sqlite::{
19///     BlockDb,
20///     chain::init::init_cache_database,
21/// };
22///
23/// let cache_file = NamedTempFile::new().unwrap();
24/// let db = BlockDb::for_path(cache_file.path()).unwrap();
25/// init_cache_database(&db).unwrap();
26/// ```
27pub fn init_cache_database(db_cache: &BlockDb) -> Result<(), rusqlite::Error> {
28    db_cache.0.execute(
29        "CREATE TABLE IF NOT EXISTS compactblocks (
30            height INTEGER PRIMARY KEY,
31            data BLOB NOT NULL
32        )",
33        [],
34    )?;
35    Ok(())
36}
37
38/// Sets up the internal structure of the metadata cache database.
39///
40/// This will automatically apply any available migrations that have not yet been applied to the
41/// database as part of its operation.
42///
43/// # Examples
44///
45/// ```
46/// use tempfile::{tempdir, NamedTempFile};
47/// use zcash_client_sqlite::{
48///     FsBlockDb,
49///     chain::init::init_blockmeta_db,
50/// };
51///
52/// let cache_file = NamedTempFile::new().unwrap();
53/// let blocks_dir = tempdir().unwrap();
54/// let mut db = FsBlockDb::for_path(blocks_dir.path()).unwrap();
55/// init_blockmeta_db(&mut db).unwrap();
56/// ```
57#[cfg(feature = "unstable")]
58pub fn init_blockmeta_db(
59    db: &mut FsBlockDb,
60) -> Result<(), MigratorError<uuid::Uuid, rusqlite::Error>> {
61    let adapter = RusqliteAdapter::new(&mut db.conn, Some("schemer_migrations".to_string()));
62    adapter.init().expect("Migrations table setup succeeds.");
63
64    let mut migrator = Migrator::new(adapter);
65    migrator
66        .register_multiple(migrations::blockmeta::all_migrations().into_iter())
67        .expect("Migration registration should have been successful.");
68    migrator.up(None)?;
69    Ok(())
70}