Skip to content

Commit

Permalink
chore(deps): bump libsqlite3-sys from 0.22.2 to 0.25.1 (#5186)
Browse files Browse the repository at this point in the history
* chore(deps): bump libsqlite3-sys from 0.22.2 to 0.25.1

Bumps [libsqlite3-sys](https://github.com/rusqlite/rusqlite) from 0.22.2 to 0.25.1.
- [Release notes](https://github.com/rusqlite/rusqlite/releases)
- [Changelog](https://github.com/rusqlite/rusqlite/blob/master/Changelog.md)
- [Commits](https://github.com/rusqlite/rusqlite/commits/v0.25.1)

---
updated-dependencies:
- dependency-name: libsqlite3-sys
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* Update Diesel dependency

libsqlite3 was pinned to the highest version we could run as defined by
the version of diesel we were running. We were running the highest
version of diesel available without making a major version increment. To
update libsqlite we also need to update diesel. It's a slightly larger
undertaking but should be worthwhile long term.

* Update signatures and references to mutable refs

Diesel 2 made a change that requires a mutable connection at all times.
See the [migration guide](https://diesel.rs/guides/migration_guide.html#2-0-0-mutable-connection) for more details.

* Migrate Derived attribute formats

Derive attribtues were updated to match community standards and wrap all
related attributes with `diesel`. For more info see the [migration
guide](https://diesel.rs/guides/migration_guide.html#2-0-0-derive-attributes)

* Transaction takes function with 1 argument

The `transaction` function now take a function with a single argument
which is the connection itself. So the connection may easily be passed
into the function block for additional use.

* Update to new migration harness

Diesel has overhauld the way it handles migrations and this updates to
newer preactices. It does it's best to replicate old behaviour, logging
migrations in tests, and returning migration related data otherwise.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: brianp <brian.o.pearce@gmail.com>
  • Loading branch information
dependabot[bot] and brianp committed Feb 20, 2023
1 parent 2a92fa4 commit 62428f2
Show file tree
Hide file tree
Showing 22 changed files with 791 additions and 636 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions base_layer/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ borsh = "0.9.3"
sha2 = "0.9.5"
chrono = { version = "0.4.19", default-features = false, features = ["serde"] }
derivative = "2.2.0"
diesel = { version = "1.4.8", features = ["sqlite", "serde_json", "chrono", "64-column-tables"] }
diesel_migrations = "1.4.0"
diesel = { version = "2.0.3", features = ["sqlite", "serde_json", "chrono", "64-column-tables"] }
diesel_migrations = "2.0.0"
digest = "0.9.0"
fs2 = "0.4.0"
futures = { version = "^0.3.1", features = ["compat", "std"] }
libsqlite3-sys = { version = "0.22.2", features = ["bundled"], optional = true }
libsqlite3-sys = { version = "0.25.1", features = ["bundled"], optional = true }
log = "0.4.6"
rand = "0.7.3"
serde = { version = "1.0.89", features = ["derive"] }
Expand Down
107 changes: 64 additions & 43 deletions base_layer/wallet/src/contacts_service/storage/sqlite_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ impl ContactsServiceSqliteDatabase {

impl ContactsBackend for ContactsServiceSqliteDatabase {
fn fetch(&self, key: &DbKey) -> Result<Option<DbValue>, ContactsServiceStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
let mut conn = self.database_connection.get_pooled_connection()?;

let result = match key {
DbKey::Contact(address) => match ContactSql::find_by_address(&address.to_bytes(), &conn) {
DbKey::Contact(address) => match ContactSql::find_by_address(&address.to_bytes(), &mut conn) {
Ok(c) => Some(DbValue::Contact(Box::new(Contact::try_from(c)?))),
Err(ContactsServiceStorageError::DieselError(DieselError::NotFound)) => None,
Err(e) => return Err(e),
},
DbKey::ContactId(id) => match ContactSql::find_by_node_id(&id.to_vec(), &conn) {
DbKey::ContactId(id) => match ContactSql::find_by_node_id(&id.to_vec(), &mut conn) {
Ok(c) => Some(DbValue::Contact(Box::new(Contact::try_from(c)?))),
Err(ContactsServiceStorageError::DieselError(DieselError::NotFound)) => None,
Err(e) => return Err(e),
},
DbKey::Contacts => Some(DbValue::Contacts(
ContactSql::index(&conn)?
ContactSql::index(&mut conn)?
.iter()
.map(|c| Contact::try_from(c.clone()))
.collect::<Result<Vec<_>, _>>()?,
Expand All @@ -76,30 +76,31 @@ impl ContactsBackend for ContactsServiceSqliteDatabase {
}

fn write(&self, op: WriteOperation) -> Result<Option<DbValue>, ContactsServiceStorageError> {
let conn = self.database_connection.get_pooled_connection()?;
let mut conn = self.database_connection.get_pooled_connection()?;

match op {
WriteOperation::Upsert(kvp) => match *kvp {
DbKeyValuePair::Contact(k, c) => {
if ContactSql::find_by_address_and_update(&conn, &k.to_bytes(), UpdateContact {
if ContactSql::find_by_address_and_update(&mut conn, &k.to_bytes(), UpdateContact {
alias: Some(c.clone().alias),
last_seen: None,
latency: None,
})
.is_err()
{
ContactSql::from(c).commit(&conn)?;
ContactSql::from(c).commit(&mut conn)?;
}
},
DbKeyValuePair::LastSeen(..) => return Err(ContactsServiceStorageError::OperationNotSupported),
},
WriteOperation::UpdateLastSeen(kvp) => match *kvp {
DbKeyValuePair::LastSeen(node_id, date_time, latency) => {
let contact = ContactSql::find_by_node_id_and_update(&conn, &node_id.to_vec(), UpdateContact {
alias: None,
last_seen: Some(Some(date_time)),
latency: Some(latency),
})?;
let contact =
ContactSql::find_by_node_id_and_update(&mut conn, &node_id.to_vec(), UpdateContact {
alias: None,
last_seen: Some(Some(date_time)),
latency: Some(latency),
})?;
return Ok(Some(DbValue::TariAddress(Box::new(
TariAddress::from_bytes(&contact.address)
.map_err(|_| ContactsServiceStorageError::ConversionError)?,
Expand All @@ -108,14 +109,14 @@ impl ContactsBackend for ContactsServiceSqliteDatabase {
DbKeyValuePair::Contact(..) => return Err(ContactsServiceStorageError::OperationNotSupported),
},
WriteOperation::Remove(k) => match k {
DbKey::Contact(k) => match ContactSql::find_by_address_and_delete(&conn, &k.to_bytes()) {
DbKey::Contact(k) => match ContactSql::find_by_address_and_delete(&mut conn, &k.to_bytes()) {
Ok(c) => {
return Ok(Some(DbValue::Contact(Box::new(Contact::try_from(c)?))));
},
Err(ContactsServiceStorageError::DieselError(DieselError::NotFound)) => (),
Err(e) => return Err(e),
},
DbKey::ContactId(id) => match ContactSql::find_by_node_id_and_delete(&conn, &id.to_vec()) {
DbKey::ContactId(id) => match ContactSql::find_by_node_id_and_delete(&mut conn, &id.to_vec()) {
Ok(c) => {
return Ok(Some(DbValue::Contact(Box::new(Contact::try_from(c)?))));
},
Expand All @@ -132,7 +133,7 @@ impl ContactsBackend for ContactsServiceSqliteDatabase {

/// A Sql version of the Contact struct
#[derive(Clone, Debug, Queryable, Insertable, PartialEq, Eq)]
#[table_name = "contacts"]
#[diesel(table_name = contacts)]
struct ContactSql {
address: Vec<u8>,
node_id: Vec<u8>,
Expand All @@ -143,35 +144,41 @@ struct ContactSql {

impl ContactSql {
/// Write this struct to the database
pub fn commit(&self, conn: &SqliteConnection) -> Result<(), ContactsServiceStorageError> {
pub fn commit(&self, conn: &mut SqliteConnection) -> Result<(), ContactsServiceStorageError> {
diesel::insert_into(contacts::table)
.values(self.clone())
.execute(conn)?;
Ok(())
}

/// Return all contacts
pub fn index(conn: &SqliteConnection) -> Result<Vec<ContactSql>, ContactsServiceStorageError> {
pub fn index(conn: &mut SqliteConnection) -> Result<Vec<ContactSql>, ContactsServiceStorageError> {
Ok(contacts::table.load::<ContactSql>(conn)?)
}

/// Find a particular Contact by their address, if it exists
pub fn find_by_address(address: &[u8], conn: &SqliteConnection) -> Result<ContactSql, ContactsServiceStorageError> {
pub fn find_by_address(
address: &[u8],
conn: &mut SqliteConnection,
) -> Result<ContactSql, ContactsServiceStorageError> {
Ok(contacts::table
.filter(contacts::address.eq(address))
.first::<ContactSql>(conn)?)
}

/// Find a particular Contact by their node ID, if it exists
pub fn find_by_node_id(node_id: &[u8], conn: &SqliteConnection) -> Result<ContactSql, ContactsServiceStorageError> {
pub fn find_by_node_id(
node_id: &[u8],
conn: &mut SqliteConnection,
) -> Result<ContactSql, ContactsServiceStorageError> {
Ok(contacts::table
.filter(contacts::node_id.eq(node_id))
.first::<ContactSql>(conn)?)
}

/// Find a particular Contact by their address, and update it if it exists, returning the affected record
pub fn find_by_address_and_update(
conn: &SqliteConnection,
conn: &mut SqliteConnection,
address: &[u8],
updated_contact: UpdateContact,
) -> Result<ContactSql, ContactsServiceStorageError> {
Expand All @@ -185,7 +192,7 @@ impl ContactSql {

/// Find a particular Contact by their address, and delete it if it exists, returning the affected record
pub fn find_by_address_and_delete(
conn: &SqliteConnection,
conn: &mut SqliteConnection,
address: &[u8],
) -> Result<ContactSql, ContactsServiceStorageError> {
// Note: `get_result` not implemented for SQLite
Expand All @@ -198,7 +205,7 @@ impl ContactSql {

/// Find a particular Contact by their node ID, and update it if it exists, returning the affected record
pub fn find_by_node_id_and_update(
conn: &SqliteConnection,
conn: &mut SqliteConnection,
node_id: &[u8],
updated_contact: UpdateContact,
) -> Result<ContactSql, ContactsServiceStorageError> {
Expand All @@ -212,7 +219,7 @@ impl ContactSql {

/// Find a particular Contact by their node ID, and delete it if it exists, returning the affected record
pub fn find_by_node_id_and_delete(
conn: &SqliteConnection,
conn: &mut SqliteConnection,
node_id: &[u8],
) -> Result<ContactSql, ContactsServiceStorageError> {
// Note: `get_result` not implemented for SQLite
Expand Down Expand Up @@ -258,7 +265,7 @@ impl From<Contact> for ContactSql {
}

#[derive(AsChangeset)]
#[table_name = "contacts"]
#[diesel(table_name = contacts)]
pub struct UpdateContact {
alias: Option<String>,
last_seen: Option<Option<NaiveDateTime>>,
Expand All @@ -267,9 +274,10 @@ pub struct UpdateContact {

#[cfg(test)]
mod test {
use std::convert::TryFrom;
use std::{convert::TryFrom, io::Write};

use diesel::{Connection, SqliteConnection};
use diesel::{sql_query, Connection, RunQueryDsl, SqliteConnection};
use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
use rand::rngs::OsRng;
use tari_common::configuration::Network;
use tari_common_types::{
Expand All @@ -290,13 +298,25 @@ mod test {
let db_name = format!("{}.sqlite3", string(8).as_str());
let db_path = format!("{}/{}", dir_path.to_str().unwrap(), db_name);

embed_migrations!("./migrations");
let conn =
const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
let mut conn =
SqliteConnection::establish(&db_path).unwrap_or_else(|_| panic!("Error connecting to {}", db_path));

embedded_migrations::run_with_output(&conn, &mut std::io::stdout()).expect("Migration failed");

conn.execute("PRAGMA foreign_keys = ON").unwrap();
conn.run_pending_migrations(MIGRATIONS)
.map(|v| {
v.into_iter()
.map(|b| {
let m = format!("Running migration {}", b);
std::io::stdout()
.write_all(m.as_ref())
.expect("Couldn't write migration number to stdout");
m
})
.collect::<Vec<String>>()
})
.expect("Migrations failed");

sql_query("PRAGMA foreign_keys = ON").execute(&mut conn).unwrap();

let names = ["Alice".to_string(), "Bob".to_string(), "Carol".to_string()];

Expand All @@ -305,38 +325,39 @@ mod test {
let pub_key = PublicKey::from_secret_key(&PrivateKey::random(&mut OsRng));
let address = TariAddress::new(pub_key, Network::default());
contacts.push(Contact::new(names[i].clone(), address, None, None));
ContactSql::from(contacts[i].clone()).commit(&conn).unwrap();
ContactSql::from(contacts[i].clone()).commit(&mut conn).unwrap();
}

let retrieved_contacts = ContactSql::index(&conn).unwrap();
let retrieved_contacts = ContactSql::index(&mut conn).unwrap();

for i in &contacts {
assert!(retrieved_contacts.iter().any(|v| v == &ContactSql::from(i.clone())));
}

assert_eq!(
contacts[1],
Contact::try_from(ContactSql::find_by_address(&contacts[1].address.to_bytes(), &conn).unwrap())
Contact::try_from(ContactSql::find_by_address(&contacts[1].address.to_bytes(), &mut conn).unwrap())
.unwrap()
);

ContactSql::find_by_address_and_delete(&conn, &contacts[0].address.clone().to_bytes()).unwrap();
ContactSql::find_by_address_and_delete(&mut conn, &contacts[0].address.clone().to_bytes()).unwrap();

let retrieved_contacts = ContactSql::index(&conn).unwrap();
let retrieved_contacts = ContactSql::index(&mut conn).unwrap();
assert_eq!(retrieved_contacts.len(), 2);

assert!(!retrieved_contacts
.iter()
.any(|v| v == &ContactSql::from(contacts[0].clone())));

let _c = ContactSql::find_by_address_and_update(&conn, &contacts[1].address.to_bytes(), UpdateContact {
alias: Some("Fred".to_string()),
last_seen: None,
latency: None,
})
.unwrap();
let _c =
ContactSql::find_by_address_and_update(&mut conn, &contacts[1].address.to_bytes(), UpdateContact {
alias: Some("Fred".to_string()),
last_seen: None,
latency: None,
})
.unwrap();

let c_updated = ContactSql::find_by_address(&contacts[1].address.to_bytes(), &conn).unwrap();
let c_updated = ContactSql::find_by_address(&contacts[1].address.to_bytes(), &mut conn).unwrap();
assert_eq!(c_updated.alias, "Fred".to_string());
});
}
Expand Down
Loading

0 comments on commit 62428f2

Please sign in to comment.