Skip to content

Commit

Permalink
Update postgres check for existing database/schema
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 794ad8a commit d8f13ba
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions experimental/plugins/postgres_storage/src/postgres_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit d8f13ba

Please sign in to comment.