From d8f13ba7713420000a10c200f18b63b0012f7d03 Mon Sep 17 00:00:00 2001 From: Ian Costanzo Date: Tue, 1 Jun 2021 13:44:03 -0700 Subject: [PATCH 1/2] Update postgres check for existing database/schema Signed-off-by: Ian Costanzo --- .../postgres_storage/src/postgres_storage.rs | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/experimental/plugins/postgres_storage/src/postgres_storage.rs b/experimental/plugins/postgres_storage/src/postgres_storage.rs index c7e5001e10..14c69707ea 100644 --- a/experimental/plugins/postgres_storage/src/postgres_storage.rs +++ b/experimental/plugins/postgres_storage/src/postgres_storage.rs @@ -666,14 +666,38 @@ 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); - let mut schema_result = match conn.execute(&create_db_sql, &[]) { - Ok(_) => Ok(()), - Err(_error) => { - Err(WalletStorageError::AlreadyExists) + // 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() { + Ok(rows_data) => { + match rows_data.iter().next() { + Some(_) => { + error!("Metadata was found for wallet id '{}' which indicates this wallet already exists.", id); + Err(WalletStorageError::AlreadyExists) + }, + None => Ok(()) + } + }, + Err(_) => Ok(()) } }; + + 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"); From b67d103ee6b9f161d4fbc36aebcce297b3ae6424 Mon Sep 17 00:00:00 2001 From: Ian Costanzo Date: Tue, 1 Jun 2021 16:01:43 -0700 Subject: [PATCH 2/2] Fix logic testing for existing wallet metadata Signed-off-by: Ian Costanzo --- .../postgres_storage/src/postgres_storage.rs | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/experimental/plugins/postgres_storage/src/postgres_storage.rs b/experimental/plugins/postgres_storage/src/postgres_storage.rs index 14c69707ea..90ef81c26b 100644 --- a/experimental/plugins/postgres_storage/src/postgres_storage.rs +++ b/experimental/plugins/postgres_storage/src/postgres_storage.rs @@ -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(_) => { @@ -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 {