Db create by FE

This commit is contained in:
Hanh 2022-12-20 21:34:47 +08:00
parent 9ff8363ef3
commit b0f3cd6a9d
3 changed files with 14 additions and 23 deletions

View File

@ -47,8 +47,6 @@ void deallocate_str(char *s);
struct CResult_u8 init_wallet(uint8_t coin, char *db_path); struct CResult_u8 init_wallet(uint8_t coin, char *db_path);
struct CResult_u8 create_db(char *db_path);
struct CResult_u8 migrate_db(uint8_t coin, char *db_path); struct CResult_u8 migrate_db(uint8_t coin, char *db_path);
struct CResult_u8 migrate_data_db(uint8_t coin); struct CResult_u8 migrate_data_db(uint8_t coin);

View File

@ -98,13 +98,6 @@ pub unsafe extern "C" fn init_wallet(coin: u8, db_path: *mut c_char) -> CResult<
to_cresult(init_coin(coin, &db_path).and_then(|()| Ok(0u8))) to_cresult(init_coin(coin, &db_path).and_then(|()| Ok(0u8)))
} }
#[no_mangle]
pub unsafe extern "C" fn create_db(db_path: *mut c_char) -> CResult<u8> {
try_init_logger();
from_c_str!(db_path);
to_cresult(crate::db::DbAdapter::create_db(&db_path).and_then(|()| Ok(0u8)))
}
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn migrate_db(coin: u8, db_path: *mut c_char) -> CResult<u8> { pub unsafe extern "C" fn migrate_db(coin: u8, db_path: *mut c_char) -> CResult<u8> {
try_init_logger(); try_init_logger();

View File

@ -11,12 +11,10 @@ use crate::unified::UnifiedAddressType;
use crate::{sync, BlockId, CompactTxStreamerClient, Hash}; use crate::{sync, BlockId, CompactTxStreamerClient, Hash};
use orchard::keys::FullViewingKey; use orchard::keys::FullViewingKey;
use rusqlite::Error::QueryReturnedNoRows; use rusqlite::Error::QueryReturnedNoRows;
use rusqlite::{params, Connection, OpenFlags, OptionalExtension, Transaction}; use rusqlite::{params, Connection, OptionalExtension, Transaction};
use serde::Serialize; use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto; use std::convert::TryInto;
use std::path::Path;
use anyhow::anyhow;
use tonic::transport::Channel; use tonic::transport::Channel;
use tonic::Request; use tonic::Request;
use zcash_client_backend::encoding::decode_extended_full_viewing_key; use zcash_client_backend::encoding::decode_extended_full_viewing_key;
@ -104,18 +102,9 @@ impl DbAdapter {
}) })
} }
pub fn create_db(db_path: &str) -> anyhow::Result<()> {
let path = Path::new(db_path);
let directory = path.parent().ok_or(anyhow!("Invalid path"))?;
std::fs::create_dir_all(directory)?;
let connection = Connection::open_with_flags(db_path, OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_CREATE)?;
connection.query_row("PRAGMA journal_mode = WAL", [], |_| Ok(()))?;
connection.execute("PRAGMA synchronous = NORMAL", [])?;
Ok(())
}
pub fn migrate_db(network: &Network, db_path: &str, has_ua: bool) -> anyhow::Result<()> { pub fn migrate_db(network: &Network, db_path: &str, has_ua: bool) -> anyhow::Result<()> {
let connection = Connection::open(db_path)?; let connection = Connection::open(db_path)?;
Self::set_wal(&connection, true)?;
migration::init_db(&connection, network, has_ua)?; migration::init_db(&connection, network, has_ua)?;
Ok(()) Ok(())
} }
@ -153,9 +142,20 @@ impl DbAdapter {
Ok(()) Ok(())
} }
pub fn set_wal(connection: &Connection, v: bool) -> anyhow::Result<()> {
if v {
connection.query_row("PRAGMA journal_mode = WAL", [], |_| Ok(()))?;
connection.execute("PRAGMA synchronous = NORMAL", [])?;
}
else {
connection.query_row("PRAGMA journal_mode = OFF", [], |_| Ok(()))?;
}
Ok(())
}
pub fn disable_wal(db_path: &str) -> anyhow::Result<()> { pub fn disable_wal(db_path: &str) -> anyhow::Result<()> {
let connection = Connection::open(db_path)?; let connection = Connection::open(db_path)?;
connection.query_row("PRAGMA journal_mode = OFF", [], |_| Ok(()))?; Self::set_wal(&connection, false)?;
Ok(()) Ok(())
} }