Added server ^•^ #8
Loading…
Reference in a new issue
No description provided.
Delete branch "server"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I can see why you didn't like writing tests, it's quite un-ergonomic.
One way you could make it better for yourself is to reduce the amount of raw sql you're writing, or move it out into seperate helper functions. For example the cleanup_db functions i think could be calls to the existing
delete()function.@ -12,0 +171,4 @@if !LOGGER_INITIALISED.swap(true, std::sync::atomic::Ordering::Relaxed) {env_logger::init();}dotenvy::dotenv().expect("Couldn't load environment variables!");shouldn't this be loaded before initializing the env_logger? Then the
.envfile could contain logging settings@ -12,0 +282,4 @@);assert_eq!(String::from("Message for test user 2").into_bytes(),This string is somewhat magic. You should probably make the
SendRequests::debug()a variable and grab the string out from that instead.@ -12,0 +394,4 @@.await.expect("Failed to populate database with test user");sqlx::query!("InSERT INTO messages VALUES ('test_user_5', $1, 1)",typo :p
@ -0,0 +76,4 @@});}pub fn debug() -> Self {You can make the "this function is unused" warning go away by adding
#[cfg(test)]so it only exists when compiling for testsAdded server ^•^to WIP: Added server ^•^WIP: Added server ^•^to Added server ^•^a3c5902f81to0b74fe49fflgtm!
@ -0,0 +2,4 @@DROP TABLE IF EXISTS users;DROP TABLE IF EXISTS messages;CREATE TABLE users (uuid varchar NOT NULL,this could use the postgres uuid type that adheres to RFC 9562
also, the uuid doesnt have much reason to be generated on the client and probably also doesnt need to be generated in the backend either, it isnt a thing the user would care about "customizing" and having it generated on the backend/database allows more control over rules for how its generated (incase these were to be changed later), when creating a new user, the client could just provide the token and the server writes to the database that token and either writes a uuid alongside it or lets the database generate one and the database query will return with the user object and the uuid can be sent back to the client
as discussed in vc, keeping as fun easter egg, yes? :3
@ -0,0 +3,4 @@DROP TABLE IF EXISTS messages;CREATE TABLE users (uuid varchar NOT NULL,token varchar NOT NULL,maybe renaming this to be "password" or something would make more sense? a token implies something like a jwt generated on the server but this moreso functions as just a users password/key and also doesnt have any restrictions for what form it takes (whether or not such restrictions should be added is kind of unimportant, users should just have a secure password lmao)
@ -0,0 +8,4 @@);CREATE TABLE messages (recipient varchar NOT NULL,should probably have an index for recipient just since thats going to be queried for a lot, also making it a foreign key to uuid in users is "more correct" id say and would allow adding cascade on delete so all a users messages are automatically removed when the user is removed
would use something like:
removing the foreign key (doesnt delete the column, just reverts it back to being not "attached" to the users table column) can be done like so:
awesome yay tyyy ❤️❤️❤️
@ -0,0 +10,4 @@CREATE TABLE messages (recipient varchar NOT NULL,message bytea NOT NULL,message_receive_time int8 NOT NULLthis could use the timestamp data type which is internally stored as a int8 aswell but has standardized precision and is just a more fitting data type (could be more useful if future operations want to do comparisons with time and such)
it would also skip the need for the complicated conversion on write on line 108 (instead just storing
NOW()) and would only need the inverseextract(epoch FROM message_receive_time)at read if it being read as an integer is requiredWhat's the time complexity on ordering according to receive time? That's the concern by which dawn made the case for using int8 instead of timestamp...
the time complexity of both timestamp and int8 would be O(n log n) if there is no index created as the query requires doing a sort (which defaults to b-tree), both types could have a b-tree index added if a single recipient ends up having more than like... a thousand messages processed at once
@ -7,0 +45,4 @@let insertion_result = sqlx::query!("INSERT INTO users VALUES ( $1, $2 );",auth_info.uuid,auth_info.tokenif token is arbitrary user input the data type should atleast specify n as like 512 or something. without specifying a length, varchar can store up to 1 GB of data, as specified on the Character Types documentation
I can actually set an exact fixed length as all passkeys will eventually be hashed. UUID can also just have the uuid length as max length. Still deciding whether to set a max size on requests and what it should be. would like to discuss over call once other things r resolved
@ -7,0 +88,4 @@let mut invalid_recipients: Vec<String> = vec![];let mut valid_recipients: Vec<SendRequest> = vec![];for message in send_request.0 {just going off vibes but i feel like this could be instead replaced with one sql query, maybe define a sql function on the database? dunno if this is worth looking into but im just saying anything that comes to mind
wait no ur so true I gotta hop on that
So turns out sqlx query! macro literally can't handle WHERE / IN patterns ?? found a blog post whinging about this so it's not just me. They had a solution but it was messy and came with enough caveats that I ditched it.
anyways tossing up using the non-statically-checked query() function. Think I will implement it as such rn but want to discuss further before merging.
imo, not having static checking is fine since the sql will be run by the tests.
ough trying to implement this is hell
without the macros u dont get the compile-time anonymous structure so u have to manually parse it and coerce it into strings and handle the result for every single thing and genuinely it's almost more painful...
turns out it doesn't work as a runtime query either. fuck
still unresolved; genuinely not sure what to do with this
I think it's fine to ignore this for now but if there was somehow a bottleneck here in terms of like performance or something then it could be good to optimise later
it sounds like the implementation is made difficult by SQLX and not SQL itself so not much can be done by lycanea I think....
yea this one isn't a lycanea solveable, alas. If optimisation becomes a requirement we can have another stab, but much like the uuid thing i dont think that's necessary yet
@ -7,0 +104,4 @@// Actually make the requestlet insertion_result = sqlx::query!("INSERT INTO messages(recipient, message, message_receive_time) \this is silly but you could just make the message_receive_time a default field in the schema and then itll be filled automatically when new rows are made
@ -7,0 +150,4 @@};// Order by receive ordermessages.sort_by_key(|message| message.message_receive_time);this sorting could happen in the query with something like
ORDER BY message_receive_timeboth if int8 is used and if timestamp is used and imo thatd be simpler (i think stylistically its better to have finished as much simple data processing as possible before it reaches the actual backend code)@ -7,0 +156,4 @@.map(|message| message.message).collect::<PollResponse>();// Only try deleting messages if there were messages TO delete (to prevent unnecessary requests)i believe it could actually be possible to make the above query a delete query and itd return all deleted rows and thus wouldnt need a separate delete query, dunno if the library being used would like... prevent this for some reason?
struggling to make this work. if madam SQL cares to help me craft a query i'll happily do it but I can't seem to get it functional
this query replaces the above SELECT query and will return exactly the same while also deleting everything along the way, 'recipient' could be removed from both the RETURNING and SELECT lines because we already know the recipient for every row but thats up to you
they call me the girl who implements lycanea's awesome sql
!8 (commit
942613f441)@ -7,0 +188,4 @@// Delete pending messages for that userlet message_deletion_result =sqlx::query!("DELETE FROM messages WHERE recipient = $1", auth_info.uuid)if the messages schema has recipient as a foreign key it can be set to "cascade on delete" and once the user is deleted below the messages the user is set to receive will also be deleted
Done. Dawn says u should press resolve on ur comments if u think it's resolved rather than me resolving them so if ur satisfied do that again
rewrite in Javascript
I since since there is no data to migrate yet, it would make sense to merge all the migration files into one. That will make it much easier to work out the current schema. That's what I'm doing in my client code.
@ -0,0 +2,4 @@# Dev setup1. Install postgresql2. Create a db for bigsister with `createdb bigsister`could this all be put into a script?
idk all this is handled for me on macos. dawn's the one who had to do this; ask her @dawn
I think the initial database setup can't really/shouldn't be a SQL script, you wouldn't have a database to run the script on and its best that scripts are ran as the bigsister user instead of the database admins user, this is all the dev setup instructions though and not instructions for setting up a "production instance" so I guess more "rules" can be broken for simplicity but still 🤷
@ -4,3 +34,3 @@#[get("/")]fn index() -> &'static str {"Hello, world!""Goodbye, world!"cat with legs
@ -7,0 +55,4 @@return Status::InternalServerError; // Insertion failed}Status::Ok // Yay Yippee welcome to Big Sister !!!going on with what lycanea said in her comment, are we sure we should let users specify uuids? it seems kinda unnecessary to me
also if the endpoint isn't going to return any actual data wouldn't it be more appropriate to respond with a status code 204 No Content?
it's not necessary, but the characters are limited so it's not a risk and it's funny to let us set our own uuids
in terms of the non technical part of "should users be allowed to specify uuids" it doesnt really matter apart from just being a bit less consistent but maddie finds it to be a fun idea and i dont really see the harm in it (apart from what i specify below) so im all for fun and games
the only actual downside is that its a bit less efficient/performant and uses more storage, using the varchar requires 36 bytes to store each uuid while an actual uuid type would only require 16, its really not that massive of a difference and i doubt the impact in terms of performance on a larger scale would be very noticeable but it would result in the uuids and any indexes containing them being over double the size
yeah we can handle this if userbase explodes
dont have any more sql-y database stuff to comment on and i am not knowledgeable about rust soo.... looks... good to me !