launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.15k stars 1.24k forks source link

Fixtures fail with "syntax error at or near" for valid SQL #3452

Closed kfsass closed 1 month ago

kfsass commented 1 month ago

Bug Description

Hi, Bonander! <3

I'm working on my portfolio and using Axum + SQLx for an API project (I've been shamelessly modeling my code off your realworld example). I'm running into an issue using fixtures where a syntax error at or near \"\u{feff}INSERT\" error is thrown for valid SQL. The query runs fine outside of SQLx. I don't know where the unicode ZWNBSP is coming from...

I was hesitant to open an issue because I feel like I'm missing something so obvious or weird... text encoding? line endings? 🥴

Minimal Reproduction

offending query:

INSERT INTO "user" (user_id, username, email, password_hash) 
VALUES ('84e18b03-c91c-47c3-92f2-20627f4fdbee', 'test', 'test@mail.com', 'argon2 hash');

user table definition:

CREATE TABLE "user"
(
    user_id        uuid        PRIMARY KEY default uuid_generate_v1mc(),
    username       text        COLLATE "case_insensitive" UNIQUE NOT NULL,
    email          text        COLLATE "case_insensitive" UNIQUE NOT NULL,
    password_hash  text        NOT NULL,

    created_at     timestamptz NOT NULL default now(),
    updated_at     timestamptz
);

SELECT trigger_updated_at('"user"');

test snippet:

#[sqlx::test(fixtures("1_user", "2_category"))]
async fn get_all_categories_succeeds(db: PgPool) {
    let app = spawn_app(&db);

    let response = app.get("/categories").authorization_bearer(TEST_JWT).await;

    response.assert_status_ok();
    let body: CategoriesBody = response.json::<CategoriesBody>();

    assert_eq!(body.categories.len(), 2)
}

Info

I haven't tried to repro on my MacBook yet. TIA for your help. I miss ya 🥰

abonander commented 1 month ago

Looks like a byte-order mark https://en.wikipedia.org/wiki/Byte_order_mark

It's added by Windows programs such as Notepad. There's probably an option somewhere to save the file without it.

kfsass commented 1 month ago

Yup! RustRover had an option to remove it in the file encoding dropdown. I'd guess it hasn't come up prior since I've been creating files on Mac (?) - I have like 5 prior migration files with no BOM ever prepended, whereas all the files I've created today do. Tacking another reason to hate Windows onto the list. Thanks for the help! 🥰

kfsass commented 1 month ago

Git probably would have stripped them out on-commit, huh 🙃