Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No autocommit on "RETURNING" clauses in sqlite #3430

Closed
j-freimuth opened this issue Aug 12, 2024 · 2 comments
Closed

No autocommit on "RETURNING" clauses in sqlite #3430

j-freimuth opened this issue Aug 12, 2024 · 2 comments
Labels

Comments

@j-freimuth
Copy link

Bug Description

When using a "RETURNING" on an UPDATE query, sqlx requires a fetch command. Yet, the fetch command doesn't seem to auto commit. Returned values look correct but if the application ends without another command committing, then the value in the database is not changed.

Work around is to manually start an transaction and commit at the end.

Minimal Reproduction

Migration:

CREATE TABLE tests(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    foo VARCHAR(255) NOT NULL
);

INSERT INTO tests(foo) VALUES ('First value');

Code:

use sqlx::{Connection, SqliteConnection};

#[tokio::main]
async fn main() {
    dotenv::dotenv().unwrap();
    sqlx::migrate!("./migrations");

    does_not_work().await;
}

async fn get_connection() -> SqliteConnection {
    let connection_string = std::env::var("DATABASE_URL").unwrap();
    sqlx::SqliteConnection::connect(&connection_string)
        .await
        .unwrap()
}

async fn does_not_work() {
    let mut conn = get_connection().await;
    sqlx::query!(
        r#"UPDATE tests SET foo = ?1 WHERE id = 1 RETURNING *"#,
        "does_not_work_method"
    )
    .fetch_one(&mut conn)
    .await
    .unwrap();
}

async fn works() {
    let mut conn = get_connection().await;
    let mut tx = conn.begin().await.unwrap();
    sqlx::query!(
        r#"UPDATE tests SET foo = ?1 WHERE id = 1 RETURNING *"#,
        "works_method"
    )
    .fetch_one(&mut *tx)
    .await
    .unwrap();

    tx.commit().await.unwrap();
}

Info

  • SQLx version: 0.8.0
  • SQLx features enabled: "sqlite", "runtime-tokio"
  • Database server and version: [REQUIRED] (MySQL / Postgres / SQLite <x.y.z>)
  • Operating system: Linux Fedora Workstation 40
  • rustc --version: rustc 1.79.0 (129f3b996 2024-06-10)
@j-freimuth j-freimuth added the bug label Aug 12, 2024
@abonander
Copy link
Collaborator

This has been fixed already by #3354 and does not reproduce on the current main.

You could use [patch] to get the fix until we get around to cutting the next release. I recommend pinning to a specific revision to be safe:

[patch.crates-io.sqlx-sqlite]
git = "https://github.com/launchbadge/sqlx"
rev = "fac53b05a4fd13529d915f1350258a81d8b372aa"

@j-freimuth
Copy link
Author

Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants