Skip to content

Commit 0af77af

Browse files
committed
PR review: rename connection extension method
* `fetch_one` to `fetch_first` * `fetch_and_collect` to `fetch_collect`
1 parent 761ab22 commit 0af77af

26 files changed

+77
-79
lines changed

internal/mithril-persistence/src/database/query/cardano_transaction/delete_cardano_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod tests {
5151

5252
fn insert_transactions(connection: &SqliteConnection, records: Vec<CardanoTransactionRecord>) {
5353
connection
54-
.fetch_one(InsertCardanoTransactionQuery::insert_many(records).unwrap())
54+
.fetch_first(InsertCardanoTransactionQuery::insert_many(records).unwrap())
5555
.unwrap();
5656
}
5757

internal/mithril-persistence/src/database/repository/cardano_transaction_repository.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl CardanoTransactionRepository {
3838
/// Return all the [CardanoTransactionRecord]s in the database.
3939
pub async fn get_all_transactions(&self) -> StdResult<Vec<CardanoTransactionRecord>> {
4040
self.connection
41-
.fetch_and_collect(GetCardanoTransactionQuery::all())
41+
.fetch_collect(GetCardanoTransactionQuery::all())
4242
}
4343

4444
/// Return all the [CardanoTransactionRecord]s in the database where block number is in the
@@ -48,7 +48,7 @@ impl CardanoTransactionRepository {
4848
range: Range<BlockNumber>,
4949
) -> StdResult<Vec<CardanoTransactionRecord>> {
5050
self.connection
51-
.fetch_and_collect(GetCardanoTransactionQuery::between_blocks(range))
51+
.fetch_collect(GetCardanoTransactionQuery::between_blocks(range))
5252
}
5353

5454
/// Return all the [CardanoTransactionRecord]s in the database up to the given beacon.
@@ -78,7 +78,7 @@ impl CardanoTransactionRepository {
7878
transaction_hash: T,
7979
) -> StdResult<Option<CardanoTransactionRecord>> {
8080
self.connection
81-
.fetch_one(GetCardanoTransactionQuery::by_transaction_hash(
81+
.fetch_first(GetCardanoTransactionQuery::by_transaction_hash(
8282
&transaction_hash.into(),
8383
))
8484
}
@@ -100,7 +100,7 @@ impl CardanoTransactionRepository {
100100
immutable_file_number,
101101
})?;
102102

103-
self.connection.fetch_one(query)
103+
self.connection.fetch_first(query)
104104
}
105105

106106
/// Create new [CardanoTransactionRecord]s in the database.
@@ -112,7 +112,7 @@ impl CardanoTransactionRepository {
112112
transactions.into_iter().map(|tx| tx.into()).collect();
113113

114114
self.connection
115-
.fetch_and_collect(InsertCardanoTransactionQuery::insert_many(records)?)
115+
.fetch_collect(InsertCardanoTransactionQuery::insert_many(records)?)
116116
}
117117

118118
/// Create new [BlockRangeRootRecord]s in the database.
@@ -124,7 +124,7 @@ impl CardanoTransactionRepository {
124124
block_ranges.into_iter().map(|tx| tx.into()).collect();
125125

126126
self.connection
127-
.fetch_and_collect(InsertBlockRangeRootQuery::insert_many(records)?)
127+
.fetch_collect(InsertBlockRangeRootQuery::insert_many(records)?)
128128
}
129129

130130
// TODO: remove this function when the Cardano transaction signature is based on block number instead of immutable number
@@ -182,8 +182,7 @@ impl CardanoTransactionRepository {
182182

183183
/// Retrieve all the [BlockRangeRootRecord] in database.
184184
pub fn get_all_block_range_root(&self) -> StdResult<Vec<BlockRangeRootRecord>> {
185-
self.connection
186-
.fetch_and_collect(GetBlockRangeRootQuery::all())
185+
self.connection.fetch_collect(GetBlockRangeRootQuery::all())
187186
}
188187

189188
/// Get the highest [ImmutableFileNumber] of the cardano transactions stored in the database.
@@ -231,7 +230,7 @@ impl CardanoTransactionRepository {
231230
) -> StdResult<Option<Range<BlockNumber>>> {
232231
let interval = self
233232
.connection
234-
.fetch_one(GetIntervalWithoutBlockRangeRootQuery::new())?
233+
.fetch_first(GetIntervalWithoutBlockRangeRootQuery::new())?
235234
// Should be impossible - the request as written in the query always returns a single row
236235
.unwrap_or_else(|| {
237236
panic!("GetIntervalWithoutBlockRangeRootQuery should always return a single row")
@@ -248,7 +247,7 @@ impl CardanoTransactionRepository {
248247
let query = GetCardanoTransactionQuery::by_transaction_hashes(
249248
hashes.into_iter().map(Into::into).collect(),
250249
);
251-
self.connection.fetch_and_collect(query)
250+
self.connection.fetch_collect(query)
252251
}
253252

254253
/// Get the [CardanoTransactionRecord] for the given block ranges.
@@ -257,7 +256,7 @@ impl CardanoTransactionRepository {
257256
block_ranges: Vec<BlockRange>,
258257
) -> StdResult<Vec<CardanoTransactionRecord>> {
259258
self.connection
260-
.fetch_and_collect(GetCardanoTransactionQuery::by_block_ranges(block_ranges))
259+
.fetch_collect(GetCardanoTransactionQuery::by_block_ranges(block_ranges))
261260
}
262261

263262
/// Prune the transactions older than the given number of blocks (based on the block range root
@@ -269,7 +268,7 @@ impl CardanoTransactionRepository {
269268
{
270269
let threshold = highest_block_range_start.saturating_sub(number_of_blocks_to_keep);
271270
let query = DeleteCardanoTransactionQuery::below_block_number_threshold(threshold)?;
272-
self.connection.fetch_one(query)?;
271+
self.connection.fetch_first(query)?;
273272
}
274273

275274
Ok(())
@@ -750,7 +749,7 @@ mod tests {
750749
.unwrap();
751750

752751
let records: Vec<BlockRangeRootRecord> = connection
753-
.fetch_and_collect(GetBlockRangeRootQuery::all())
752+
.fetch_collect(GetBlockRangeRootQuery::all())
754753
.unwrap();
755754
assert_eq!(
756755
vec![
@@ -783,7 +782,7 @@ mod tests {
783782
.unwrap();
784783

785784
let record: Vec<BlockRangeRootRecord> = connection
786-
.fetch_and_collect(GetBlockRangeRootQuery::all())
785+
.fetch_collect(GetBlockRangeRootQuery::all())
787786
.unwrap();
788787
assert_eq!(
789788
vec![BlockRangeRootRecord {

internal/mithril-persistence/src/database/version_checker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'conn> DatabaseVersionChecker<'conn> {
5757
.with_context(|| "Can not create table 'db_version' while applying migrations")?;
5858
let db_version = self
5959
.connection
60-
.fetch_one(GetDatabaseVersionQuery::get_application_version(
60+
.fetch_first(GetDatabaseVersionQuery::get_application_version(
6161
&self.application_type,
6262
))
6363
.with_context(|| "Can not get application version while applying migrations")?
@@ -117,7 +117,7 @@ impl<'conn> DatabaseVersionChecker<'conn> {
117117
updated_at: Utc::now(),
118118
};
119119
let _ = connection
120-
.fetch_one(UpdateDatabaseVersionQuery::one(db_version))
120+
.fetch_first(UpdateDatabaseVersionQuery::one(db_version))
121121
.with_context(|| {
122122
format!(
123123
"Can not save database version when applying migration: '{}'",
@@ -205,7 +205,7 @@ mod tests {
205205

206206
fn check_database_version(connection: &SqliteConnection, db_version: DbVersion) {
207207
let version = connection
208-
.fetch_one(GetDatabaseVersionQuery::get_application_version(
208+
.fetch_first(GetDatabaseVersionQuery::get_application_version(
209209
&ApplicationNodeType::Aggregator,
210210
))
211211
.unwrap()

internal/mithril-persistence/src/sqlite/connection_extensions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ pub trait ConnectionExtensions {
1818
fn fetch<Q: Query>(&self, query: Q) -> StdResult<EntityCursor<Q::Entity>>;
1919

2020
/// Fetch the first entity from the database returned using the given query.
21-
fn fetch_one<Q: Query>(&self, query: Q) -> StdResult<Option<Q::Entity>> {
21+
fn fetch_first<Q: Query>(&self, query: Q) -> StdResult<Option<Q::Entity>> {
2222
let mut cursor = self.fetch(query)?;
2323
Ok(cursor.next())
2424
}
2525

2626
/// Fetch entities from the database using the given query and collect the result in a collection.
27-
fn fetch_and_collect<Q: Query, B: FromIterator<Q::Entity>>(&self, query: Q) -> StdResult<B> {
27+
fn fetch_collect<Q: Query, B: FromIterator<Q::Entity>>(&self, query: Q) -> StdResult<B> {
2828
Ok(self.fetch(query)?.collect::<B>())
2929
}
3030
}

mithril-aggregator/src/database/query/certificate/get_certificate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ mod tests {
6868
insert_certificate_records(&connection, certificates.clone());
6969

7070
let certificate_records: Vec<CertificateRecord> = connection
71-
.fetch_and_collect(GetCertificateRecordQuery::by_epoch(Epoch(1)).unwrap())
71+
.fetch_collect(GetCertificateRecordQuery::by_epoch(Epoch(1)).unwrap())
7272
.unwrap();
7373
let expected_certificate_records: Vec<CertificateRecord> = certificates
7474
.iter()
@@ -78,7 +78,7 @@ mod tests {
7878
assert_eq!(expected_certificate_records, certificate_records);
7979

8080
let certificate_records: Vec<CertificateRecord> = connection
81-
.fetch_and_collect(GetCertificateRecordQuery::by_epoch(Epoch(3)).unwrap())
81+
.fetch_collect(GetCertificateRecordQuery::by_epoch(Epoch(3)).unwrap())
8282
.unwrap();
8383
let expected_certificate_records: Vec<CertificateRecord> = certificates
8484
.iter()
@@ -106,7 +106,7 @@ mod tests {
106106
insert_certificate_records(&connection, certificates.clone());
107107

108108
let certificate_records: Vec<CertificateRecord> = connection
109-
.fetch_and_collect(GetCertificateRecordQuery::all())
109+
.fetch_collect(GetCertificateRecordQuery::all())
110110
.unwrap();
111111
assert_eq!(expected_certificate_records, certificate_records);
112112
}

mithril-aggregator/src/database/query/certificate/insert_certificate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mod tests {
119119
for certificate in certificates {
120120
let certificate_record: CertificateRecord = certificate.into();
121121
let certificate_record_saved = connection
122-
.fetch_one(InsertCertificateRecordQuery::one(
122+
.fetch_first(InsertCertificateRecordQuery::one(
123123
certificate_record.clone(),
124124
))
125125
.unwrap();
@@ -136,7 +136,7 @@ mod tests {
136136
let connection = main_db_connection().unwrap();
137137

138138
let certificates_records_saved: Vec<CertificateRecord> = connection
139-
.fetch_and_collect(InsertCertificateRecordQuery::many(
139+
.fetch_collect(InsertCertificateRecordQuery::many(
140140
certificates_records.clone(),
141141
))
142142
.expect("saving many records should not fail");

mithril-aggregator/src/database/query/epoch_setting/get_epoch_setting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mod tests {
5555
insert_epoch_settings(&connection, &[1, 2, 3]).unwrap();
5656

5757
let epoch_setting_record = connection
58-
.fetch_one(GetEpochSettingQuery::by_epoch(Epoch(1)).unwrap())
58+
.fetch_first(GetEpochSettingQuery::by_epoch(Epoch(1)).unwrap())
5959
.unwrap()
6060
.expect("Should have an epoch setting for epoch 1.");
6161
assert_eq!(Epoch(1), epoch_setting_record.epoch_setting_id);
@@ -65,7 +65,7 @@ mod tests {
6565
);
6666

6767
let epoch_setting_record = connection
68-
.fetch_one(GetEpochSettingQuery::by_epoch(Epoch(3)).unwrap())
68+
.fetch_first(GetEpochSettingQuery::by_epoch(Epoch(3)).unwrap())
6969
.unwrap()
7070
.expect("Should have an epoch setting for epoch 3.");
7171
assert_eq!(Epoch(3), epoch_setting_record.epoch_setting_id);

mithril-aggregator/src/database/query/epoch_setting/update_epoch_setting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod tests {
5959
insert_epoch_settings(&connection, &[3]).unwrap();
6060

6161
let epoch_setting_record = connection
62-
.fetch_one(UpdateEpochSettingQuery::one(
62+
.fetch_first(UpdateEpochSettingQuery::one(
6363
Epoch(3),
6464
fake_data::protocol_parameters(),
6565
))

mithril-aggregator/src/database/query/signed_entity/get_signed_entity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ mod tests {
9696

9797
let first_signed_entity_type = signed_entity_records.first().unwrap().to_owned();
9898
let signed_entity_records: Vec<SignedEntityRecord> = connection
99-
.fetch_and_collect(GetSignedEntityRecordQuery::by_signed_entity_id(
99+
.fetch_collect(GetSignedEntityRecordQuery::by_signed_entity_id(
100100
&first_signed_entity_type.signed_entity_id,
101101
))
102102
.unwrap();
103103
assert_eq!(vec![first_signed_entity_type], signed_entity_records);
104104

105105
let signed_entity_records: Vec<SignedEntityRecord> = connection
106-
.fetch_and_collect(
106+
.fetch_collect(
107107
GetSignedEntityRecordQuery::by_signed_entity_type(
108108
&SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
109109
)
@@ -122,7 +122,7 @@ mod tests {
122122
assert_eq!(expected_signed_entity_records, signed_entity_records);
123123

124124
let signed_entity_records: Vec<SignedEntityRecord> = connection
125-
.fetch_and_collect(GetSignedEntityRecordQuery::all())
125+
.fetch_collect(GetSignedEntityRecordQuery::all())
126126
.unwrap();
127127
let expected_signed_entity_records: Vec<SignedEntityRecord> =
128128
signed_entity_records.iter().map(|c| c.to_owned()).collect();

mithril-aggregator/src/database/query/signed_entity/insert_signed_entity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mod tests {
6060

6161
for signed_entity_record in signed_entity_records {
6262
let signed_entity_record_saved = connection
63-
.fetch_one(InsertSignedEntityRecordQuery::one(
63+
.fetch_first(InsertSignedEntityRecordQuery::one(
6464
signed_entity_record.clone(),
6565
))
6666
.unwrap();

0 commit comments

Comments
 (0)