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

Conversation

@Septias
Copy link
Contributor

@Septias Septias commented Jun 6, 2023

Finally get rid of surrealdb in favor of sqlx.

I've changed the structure of the db which was a struct before to a bare module which takes a sqliteConnection as an argument. This way the bot might utilize transactions later to rollback complex db changes in case of failure.

closes #2

@Septias Septias mentioned this pull request Jun 6, 2023
@hpk42
Copy link
Contributor

hpk42 commented Jun 7, 2023 via email

@link2xt
Copy link
Contributor

link2xt commented Jun 7, 2023

sqlx has async interfaces and can switch from SQLite to MySQL and PostgreSQL if needed.

The reason we switched back from sqlx to rusqlite was a performance problem due to sqlx using SQLite database from multiple threads, submitting query in one thread and stepping over the results in a different thread. This problem is reportedly fixed in sqlx 0.7 and we don't plan to put too much load on this database anyway. If the database ever becomes a bottleneck, we can switch to PostgreSQL easily.

@Septias Septias force-pushed the sk/use_rusqlite branch from 5fe16f2 to e5a10fe Compare June 7, 2023 09:22
@@ -0,0 +1,49 @@
-- Add migration script here
CREATE TABLE IF NOT EXISTS config (
Copy link
Contributor

@link2xt link2xt Jun 7, 2023

Choose a reason for hiding this comment

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

How do we add a column to this table? If we need a new configuration value, this is not extensible. I already had troubles with import trying to insert serial when the rest of the config is not there: #28

Also having multiple rows in such table does not make sense. What is the id column, can we have multiple configs with different IDs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've never written any raw sql statements just yet, so I don't get why you can't add a new column.
Regarding the second point, the bot will not configure as soon as you start it so the config should be complete.
The last point is also due to my lacking knowledge of sql. I thought it would be fine to have a table where only one row exists (the one with the current config).

Since you cleary have a better understandig of sql, I'm open for any suggestions!

Copy link
Contributor

Choose a reason for hiding this comment

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

I would do similar to how the core does this: https://github.com/deltachat/deltachat-core-rust/blob/e6d9a49187786fe1da72e6163c0684e3beca46b0/src/sql/tables.sql#L1-L5

Afaik this is also what firefox does.

I've never written any raw sql statements just yet, so I don't get why you can't add a new column.

You can add a new column, but then you need a migration each time you do this, and the value will need to have some default (or not be NOT NULL).

Copy link
Contributor Author

@Septias Septias Jun 7, 2023

Choose a reason for hiding this comment

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

To build the whole config I would then have to do 5 requests? Sounds kinda iffy, if we keep the central approach of the config. If we change to a table with [key, value] entries I would suggest to also fracture the config loading.
Also, is the not-null contstraint really a problem if we always initialize the config at the top of main ?

Copy link
Contributor

Choose a reason for hiding this comment

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

To build the whole config I would then have to do 5 requests?

You can do this with one INSERT, INSERT INTO config (key, value) VALUES ('invite_qr', 'foo'), ('genesis_qr', 'bar'). And even with multiple INSERTs it is not a problem, you can also group them in a transaction and it will be just as fast.

Also, is the not-null contstraint really a problem if we always initialize the config at the top of main ?

I am thinking about the case when we already have a bot set up, users have added applications there etc. Then we want to introduce a new configuration option. How would a migration look like? Add a new column with a NULL value, add a new column with some default value (which may not make sense e.g. if it is another QR code)? If you have a key-value table, then you don't even need a migration.

Copy link
Contributor Author

@Septias Septias Jun 7, 2023

Choose a reason for hiding this comment

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

Well if it is something like a qr-code which needs non-trivial setup, you have to write code for it anyway. That would then be exactly what you would also put into the migration code, no? Just create it with a sensible default and pick up on initialization in bot code.

I mean I see that it is in general more flexibel with a kv-store, but for the bot I don't see the immediate benefit. Maybe we can postpone this discussion to a later point / create an issues?

env={"RUST_LOG": "github_bot=trace"},
env={
"RUST_LOG": "github_bot=trace",
"addr": config["addr"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does import need an address now? Previously I could import .xdcs and then configure the bot.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this change can just be reverted, will the test still pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, I made it so you always have to configure when executing the bot binary

@link2xt
Copy link
Contributor

link2xt commented Jun 7, 2023

cargo test is failing by the way, it is not tested in CI currently.

Comment on lines +147 to +152
review_helper: MsgId::new(row.try_get("review_helper")?),
submit_helper: MsgId::new(row.try_get("submit_helper")?),
review_chat: ChatId::new(row.try_get("review_chat_id")?),
submit_chat: ChatId::new(row.try_get("submit_chat_id")?),
publisher: ContactId::new(row.try_get("publisher")?),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it not possible to implement some traits for MsgId,ContactIdand ChatId so that sqlx can do this automatically for you?

Copy link
Contributor Author

@Septias Septias Jun 7, 2023

Choose a reason for hiding this comment

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

Yes, you need Encode and Decode. I didn't want to add it to dc because I didn't think of putting it behind a feature gate. At some point we can make this optimization.
At this point this little extra verbosity is not too bad I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might also be enough to only implement Deref for chatId etc.

@Septias Septias force-pushed the sk/use_rusqlite branch from 654a9da to 1165793 Compare June 7, 2023 12:37
@link2xt
Copy link
Contributor

link2xt commented Jun 7, 2023

I think we can still reshape even the initial database schema before it is deployed, so better merge it and then it can be improved in smaller PRs.

@Septias Septias merged commit ff69906 into main Jun 7, 2023
@Septias Septias deleted the sk/use_rusqlite branch June 7, 2023 19:05
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.

Remove SurrealDB dependency [was: remove rocksdb dependency]

5 participants