Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2397 from ianco/postgres_updates
Browse files Browse the repository at this point in the history
Update postgres check for existing database/schema
  • Loading branch information
WadeBarnes authored Jun 7, 2021
2 parents 794ad8a + b67d103 commit 9fbbb79
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions experimental/plugins/postgres_storage/src/postgres_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand Down

0 comments on commit 9fbbb79

Please sign in to comment.