Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Db refactor #295

Merged
merged 1 commit into from
Oct 14, 2021
Merged

Db refactor #295

merged 1 commit into from
Oct 14, 2021

Conversation

DeliciousHair
Copy link
Contributor

@DeliciousHair DeliciousHair commented Oct 13, 2021

DB code refactoring for #88

The primary goal was to remove all of the calls to serde_json::to_string() for the data-handling, thus enabling us to do (more or less):

Order {
    row.column,
    ...
}

as well as clean up the SQL for easier reading. This has mostly been accomplished, with further refinements easily accomplished once the upstream issues in sqlx are addressed. See #314 for issues we are tracking.

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

At the moment, there are multiple changes mangled up here. I can already see that this will prove tricky to untangle into multiple, distinct commits.

I would suggest the following:

Make a new branch off master where you just change the SQL queries to use the with syntax and move them into separate files without any of the improvements around sqlx::Type etc.

Given that you already have the queries, that should be reasonably easy. On top of that, we can then introduce sqlx::Type etc and get rid of all the serde_json stuff!

leverage integer not null,
liquidation_price text not null,
creation_timestamp_seconds integer not null,
creation_timestamp_nanoseconds integer not null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather drop the nanoseconds. What do you think @da-kami ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomaseizinger @da-kami I've left the ns in for the moment as they are now handled without issue. Can drop them, but it looks like that may lead to a bit more work upstream as the timestamps are generated by SystemTime::new() so if the ns are dropped, things may not line up.

Not a big deal either way, this just seemed the path of least resistance for the moment.

daemon/src/db.rs Outdated
@@ -296,8 +306,8 @@ pub async fn load_cfd_by_order_id(
let max_quantity = serde_json::from_str(row.max_quantity.as_str()).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing max_quantity via serde here is not going to work is it? We are only to_stringing it on the insert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, all things that involve serde_json::from_str() will go away with one exception:

    // TODO:
    // still have the use of serde_json::from_str() here, which will be dealt with
    // via https://github.com/comit-network/hermes/issues/290
    Ok(Cfd {
        order,
        quantity_usd: Usd(Decimal::from_str(&row.quantity_usd).unwrap()),
        state: serde_json::from_str(row.state.as_str()).unwrap(),
    })

daemon/src/db.rs Outdated Show resolved Hide resolved
@@ -14,7 +14,8 @@ use uuid::Uuid;

pub mod cfd;

#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, sqlx::Type)]
#[sqlx(type_name = "String")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, this only does something for postgres so we can remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unaddressed !

state.state

from order
inner join state on state.order_id = order.order_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newlines on these.

@@ -0,0 +1,69 @@
with order as (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if the changes were made in two steps:

  1. Moving queries to external files
  2. Changing up the query to use with

@thomaseizinger
Copy link
Contributor

Actually, all things considered it is not that important to split things up into multiple commits so feel free to squash everything together!

Overall, these changes are a major improvement, thank you!

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Massive improvement over what we currently have!

  • Found what I think is a regression.
  • A few unaddressed comments and commits need to be squashed together.

daemon/src/db_sql/query_load_all_cfds.sql Outdated Show resolved Hide resolved
@@ -14,7 +14,8 @@ use uuid::Uuid;

pub mod cfd;

#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, sqlx::Type)]
#[sqlx(type_name = "String")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unaddressed !

daemon/src/db.rs Outdated Show resolved Hide resolved
daemon/src/db.rs Outdated Show resolved Hide resolved
daemon/src/db.rs Show resolved Hide resolved
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

2 more tiny changes and this is good to go in. Just amend the now only commit for that (git commit --amend). Also, can you reword the commit message to something more descriptive please?

Cargo.lock Outdated
@@ -4,9 +4,9 @@ version = 3

[[package]]
name = "ahash"
version = "0.7.4"
version = "0.7.5"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These update must have been accidental. Can you undo that please?

git checkout master -- Cargo.lock should do the trick.

})
}

pub async fn insert_cfd(cfd: Cfd, conn: &mut PoolConnection<Sqlite>) -> anyhow::Result<()> {
let mut tx = conn.begin().await?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! No more transaction for this!

@@ -13,7 +13,7 @@ use uuid::Uuid;

pub mod cfd;

#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, sqlx::Type)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not actually doing anything because we are not specifying #[sqlx(transparent)].

Can you remove it to avoid confusion about what this is doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Yes, removed now.

@thomaseizinger thomaseizinger marked this pull request as ready for review October 14, 2021 04:26
The primary goal was to remove all of the calls to `serde_json::to_string()`
for the data-handling, thus enabling us to do (more or less):

```rust
Order {
    row.column,
    ...
}
```

as well as clean up the SQL for easier reading. This has mostly been
accomplished, with further refinements easily accomplished once the
upstream issues in `sqlx` are addressed. See #314 for issues we are
tracking.
Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Feel free to bors r+ once CI is green.

@thomaseizinger
Copy link
Contributor

bors r+

@bors bors bot merged commit 4900538 into master Oct 14, 2021
@bors bors bot deleted the db_refactor branch October 14, 2021 05:55
bors bot added a commit that referenced this pull request Oct 15, 2021
334: Revert "Db refactor" r=thomaseizinger a=thomaseizinger

Reverts #295

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants