Skip to content

Commit

Permalink
Fix logic testing for existing wallet metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Costanzo <[email protected]>
  • Loading branch information
ianco committed Jun 1, 2021
1 parent d8f13ba commit b67d103
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions experimental/plugins/postgres_storage/src/postgres_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,27 @@ impl WalletStrategy for DatabasePerWalletStrategy {
debug!("connecting to postgres, url_base: {:?}", url_base);
let conn = postgres::Connection::connect(&url_base[..], config.tls())?;

debug!("creating wallets DB");
let create_db_sql = str::replace(_CREATE_WALLET_DATABASE, "$1", id);
// ignore errors at this step, in case the database has been pre-created by the DBA
// if the create db fails, the user login/table creation will fail
let _err = conn.execute(&create_db_sql, &[]);
conn.finish()?;

debug!("connecting to wallet as user");
let conn = match postgres::Connection::connect(&url[..], config.tls()) {
Ok(conn) => conn,
Err(error) => {
return Err(WalletStorageError::IOError(format!("Error occurred while connecting to wallet schema: {}", error)));
}
};

// select metadata for this wallet to ensure it DOESN'T exist
let mut schema_result = {
let mut rows = conn.query(
"SELECT value FROM metadata WHERE wallet_id",
&[&id]);
match rows.as_mut() {
let rows = conn.query(
"SELECT value FROM metadata",
&[]);
match rows {
Ok(rows_data) => {
match rows_data.iter().next() {
Some(_) => {
Expand All @@ -685,29 +700,6 @@ impl WalletStrategy for DatabasePerWalletStrategy {
}
};

match schema_result {
Ok(_) => {
debug!("creating wallets DB");
let create_db_sql = str::replace(_CREATE_WALLET_DATABASE, "$1", id);
schema_result = match conn.execute(&create_db_sql, &[]) {
Ok(_) => Ok(()),
Err(_error) => {
Err(WalletStorageError::AlreadyExists)
}
};
},
Err(_) => ()
};
conn.finish()?;

debug!("connecting to wallet as user");
let conn = match postgres::Connection::connect(&url[..], config.tls()) {
Ok(conn) => conn,
Err(error) => {
return Err(WalletStorageError::IOError(format!("Error occurred while connecting to wallet schema: {}", error)));
}
};

debug!("setting up multi schema");
for sql in &_CREATE_SCHEMA {
match schema_result {
Expand Down

0 comments on commit b67d103

Please sign in to comment.