Skip to content

SQLx can't find applied migration due to collision between schema and username in search_path  #3523

Open
@GunnarMorrigan

Description

@GunnarMorrigan

Bug Description

When a user lets call them roster applies a migration that creates a schema equal to the username, roster, only the first migration will be applied correctly.

The second migration attempt SQLx will not be able to find the original migration table public._sqlx_migrations and attempts to apply the migration again. A new table is created under the schema and username roster but applying the migration actually fails as it has already been applied.

After some troubleshooting with @abonander it turns out that postgreSQL's search_path is "$user", public.
Due to the collision between the username and schema, the schema roster will be chosen to search for the migration table. As the schema did not exist before the first migration, the first migration ran successful.
https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH

Minimal Reproduction

A small code snippet or a link to a Github repo or Gist, with instructions on reproducing the bug.

  • Create a new DB with an owner called roster (can be anything but be consistent).
  • Create a migration that creates a schema with the same name as the user
  • Run the migration (success)
  • Run the migration (fail)

Info

  • SQLx version:
sqlx --version
sqlx-cli 0.8.2
  • SQLx features enabled:
sqlx = { version = "0.8", features = [
    "runtime-tokio",
    "tls-rustls",
    "chrono",
    "postgres",
    "derive",
    "macros",
    "migrate",
] }
  • Database server and version: Postgres
  • Operating system: windows and linux
  • rustc --version: rustc 1.80.1 (3f5fd8dd4 2024-08-06)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdb:postgresRelated to PostgreSQLmigrationsProposals or bugs involving migrations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions