Skip to content

migration checksums different on unix/non-unix systems due to CRLF/LF line endingsΒ #2659

@DawidPietrykowski

Description

@DawidPietrykowski

Bug Description

When a migration checksum is generated, it seems like it's putting the whole file as bytes through SHA384 generator as stated by this code:

impl Migration {
    pub fn new(
        version: i64,
        description: Cow<'static, str>,
        migration_type: MigrationType,
        sql: Cow<'static, str>,
    ) -> Self {
        let checksum = Cow::Owned(Vec::from(Sha384::digest(sql.as_bytes()).as_slice()));
        Migration {
            version,
            description,
            migration_type,
            sql,
            checksum,
        }
    }
}

What that means is when a migration is checked with version control (git in my case) it might manipulate the CRLF/LF line endings based on system. You could also run into this problem when creating/modifying the file on 2 different system even without version control I believe.

This was causing version mismatch error in my program, as I run migrate! in my main function to ensure that the db is set up correctly.

I work on 2 different systems and as of now it seems like I have to comment out my migrate! line if I'm sure it was run, don't know how to handle it in production though.

I'm not sure how to best handle this issue, but maybe conversion CRLF->LF before passing the text to sha would make sense?

Minimal Reproduction

  1. create a repository with a migration file on windows
  2. run migration
  3. check out with git while having core.autocrlf=true
  4. clone repository on mac/linux
  5. run migration

Info

  • SQLx version: 0.7.1
  • SQLx features enabled: "postgres", "runtime-tokio"
  • Database server and version: Postgres
  • Operating system: 2 different
  • rustc --version: rustc 1.70.0 (90c541806 2023-05-31)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions