How to create a SQLite database with rust sqlx - rust

I'm trying out a conversion from rusqlite => sqlx.
Opening a connection from rusqlite calls SQLite::open, and creates the db files. The following works:
use rusqlite::Connection;
Connection::open(db_filename)
However, I'm following the docs on the sqlx side (https://github.com/launchbadge/sqlx#connecting), and they launch me immediately into creating a pool:
use sqlx::sqlite::{SqlitePoolOptions};
SqlitePoolOptions::new()
.max_connections(1)
.connect(&format!("sqlite://{}", db_filename))
.await
.map_err(|err| format!("{}\nfile: {}", err.to_string(), db_filename))?;
which in fact yields the Result<_, String> message of:
Error: "error returned from database: unable to open database file\nfile: /path/to/my.db"
I'm not clear how in the sqlx world to actually write those db files on first boot.
Tips?

I found an issue in their issue tracker, where you can use ?mode=rwc query style param on the filename.
https://github.com/launchbadge/sqlx/issues/1114#issuecomment-827815038
Adding the query solved the problem.

Related

node.js | Replacing DB client with Prisma keeping preserving DB structure - issue with uuid_generate_v4()

I am trying to add the Prisma DB client to the existing node.js project while preserving the DB structure.
Postgresql
Prisma 4.7.1
I've set up the initial Prisma configuration (env vars, etc.).
I've used the command npx prisma db pull to generate prisma.schema file according to the existing DB structure
I create the initial migration by using some empty DB npx prisma migrate dev
At this point it is expected that migration would create DB structure, but the command fails with the following error
✗ npx prisma migrate dev
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "service_prisma", schema "public" at "127.0.0.1:5432"
PostgreSQL database service_prisma created at 127.0.0.1:5432
✔ Enter a name for the new migration: … init
Applying migration `20221221095823_init`
Error: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20221221095823_init
Database error code: 42883
Database error:
ERROR: function uuid_generate_v4() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E42883), message: "function uuid_generate_v4() does not exist", detail: None, hint: Some("No function matches the given name and argument types. You might need to add explicit type casts."), position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("parse_func.c"), line: Some(521), routine: Some("ParseFuncOrColumn") }
Follow up plan would be to:
Set DB back to the original one containing tables and data
Then mark initial migration as applied with the following command npx prisma migrate resolve --applied 20221221095823_init
So, main problem is that IDs in existing tables use uuid_generate_v4() to generate random UUID for new entries. The support on DB level is definitly there, because it simple works normally with slonik DB client.
model SomeTable {
id String #id #default(dbgenerated("uuid_generate_v4()")) #db.Uuid
}
Any idea how to solve this? Thanks in advance!
I struggled a bit, but then found a solution shortly after posting the question above...
I've found some tips but they were a bit unclear to me initially.
So if anyone runs into a similar problem, then there is a solution:
Ignore the error in step 3 - the migration file gets created anyway
Thenn add the CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; into the first line of the created migration file.
Run npx prisma migrate dev again, this time it should run without errors
Continue with step 4 of the list mentioned above.
Hope this helps :)

Error with tokio postgres and database query Rust

I am working on a rust project where I am working with tokio postgres.
I have a table by name of DATA that has two columns "id" and "opt". I have the data hitting the query as two inputs where one is a String and the other is an Option.
Following tokio postges conventions, I am having to call the client with the help of the source code provided below.I am using COALESCE to check if second parameter is NULL on the sql query and this way I am making the SQL query compact.
let rows = client.query("SELECT * FROM DATA WHERE id = $1 AND opt = COALESCE(opt, $2)", &[&input1, &input2)]).await?;
I am getting this error when I perform a cargo build.
expected `&dyn ToSql + Sync`, found enum `Result`
Can this error be mitigated from Rust?
The result from client.query is of type Result. Before working with the actual rows, you'll need to retrieve them from the Result type and deal with any errors that might have occurred.
See the Rust Book section or error handling for details on how to properly separate successful from erroneous results.
You can find the full function signature at docs.rs, for example, where you'll find that the return type is of Result<Vec<Row>, Error>.

Synchronization problems with between WRITER and READER RDS servers

I have a basic structure on AWS that has an Aurora MySQL cluster and two servers, a WRITER (db.t2.medium) and a READER (db.r5.large).
I have an application in NodeJS that runs the following routine:
Insert a row in the database using WRITER server
Search this line ID in the table using READER server
Insert relational information into other tables using the generated
ID using WRITER server
In terms of code its structured like this (considering that db.writer is an instance of knex that executes queries on the WRITER server and db.reader is the instance that executes on the READER server):
let theNewId = await db.writer.raw("INSERT INTO users (`name`,`email`) VALUES ('John Doe','mail#contoso.org')").catch(err => { console.log(err)}).then(async R=>{
return await db.reader.raw("SELECT * FROM users WHERE `email`='mail#contoso.org'").catch(err => { console.log(err)}).then( async result=>{
let theId = result[0].id;
await db.writer.raw(""INSERT INTO users_emails (`user_id`,`email`) VALUES ('"+theId+"','mail#contoso.org')"");
return theId;
});
});
Note: I'm not executing RAW queryes, I'm just using it as example.
The problem I am having in all the similar functionalities of the code: the reader, even though it is running inside the result of the insert function, doesn't find the line that, when I will check, was inserted by WRITER correctly.
Is there any type of configuration or best programming practice that can be done so that this asynchronism does not occur in this type of situation?
Just to finish this question: after a lot of research I discovered that there is an asynchrony between the reading and writing instances that forces me to use, in these specific cases, the same write server to perform the searches.

KnexJS Raw Method doesn't work in one case

Working with KnexJS on a project and have been using the .raw() method throughout the project with out a single issue.
I now have one case where the .raw is just not being built into the SQL, and so I end up with the syntax error at end of input error. If I dump the SQL string, I can see why as it's failing
'update "mytable" set "a" = ? returning '
I can see why it's having an issue, the problem I'm having is that the returning is a Raw value as such, and so I just can't figure out why it's not being compiled with the SQL.
knexQuery.update(data).into('mytable').returning( knexQuery.raw('mytable::json') );`
If I use a string, in place of the raw in the returning() method, it will compile the string into it.
If I console out the knexQuery.raw('mytable::json') part, it shows as a Raw object, with the right object data...
Raw {
client:
Client_PG {
... },
sql: 'mytable::json',
bindings: undefined,
_wrappedBefore: undefined,
_wrappedAfter: undefined,
_debug: undefined } }
I know that the SQL works, as I've filled in the missing part and it works, but the SQL is not the issue, I just can't figure out why the raw() method is not being compiled with the SQL string.
I also have another piece of code (and INSERT one) somewhere else in my code that is using the same returning( knexQuery.raw(....) ) and that works perfectly fine.
I'm starting to think this is a bug in the code, but after spending an hour going through the KnexJS library code, I can't see any reason why it would not work.
So why does this code not build the raw into the query, while my other one has no problem and works?
I'm getting to the point where I just want to use something else to get around the problem, but it's just not possible without using this method.
This is bug in knex https://runkit.com/mikaelle/592412c3a631940012a51928 please open issue in knex github. Looks like handling raw input just haven't been implemented to returning builder method.
Anyways looks like this works as a workaround:
knex('TestTable')
.insert({ foo: 'bar' })
.returning([knex.raw('mytable::json')]) // raw wrapped in array
.toSQL();

Resource Conflict after syncing with PouchDB

I am new to CouchDB / PouchDB and until now I somehow could manage the start of it all. I am using the couchdb-python library to send initial values to my CouchDB before I start the development of the actual application. Here I have one database with templates of the data I want to include and the actual database of all the data I will use in the application.
couch = couchdb.Server()
templates = couch['templates']
couch.delete('data')
data = couch.create('data')
In Python I have a loop in which I send one value after another to CouchDB:
value = templates['Template01']
value.update({ '_id' : 'Some ID' })
value.update({'Other Attribute': 'Some Value'})
...
data.save(value)
It was working fine the whole time, I needed to run this several times as my data had to be adjusted. After I was satisfied with the results I started to create my application in Javascript. Now I synced PouchDB with the data database and it was also working. However, I found out that I needed to change something in the Python code, so I ran the first python script again, but now I get this error:
couchdb.http.ResourceConflict: (u'conflict', u'Document update conflict.')
I tried to destroy() the pouchDB database data and delete the CouchDB database as well. But I still get this error at this part of the code:
data.save(value)
What I also don't understand is, that a few values are actually passed to the database before this error comes. So some values are saved() into the db.
I read it has something to do with the _rev values of the documents, but I cannot get an answer. Hope someone can help here.

Resources