diff --git a/experimental/plugins/postgres_storage/src/postgres_storage.rs b/experimental/plugins/postgres_storage/src/postgres_storage.rs index c7e5001e10..90ef81c26b 100644 --- a/experimental/plugins/postgres_storage/src/postgres_storage.rs +++ b/experimental/plugins/postgres_storage/src/postgres_storage.rs @@ -668,12 +668,9 @@ impl WalletStrategy for DatabasePerWalletStrategy { 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) - } - }; + // 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"); @@ -684,6 +681,25 @@ impl WalletStrategy for DatabasePerWalletStrategy { } }; + // select metadata for this wallet to ensure it DOESN'T exist + let mut schema_result = { + let rows = conn.query( + "SELECT value FROM metadata", + &[]); + match rows { + 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(()) + } + }; + debug!("setting up multi schema"); for sql in &_CREATE_SCHEMA { match schema_result {