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

Ability to disable hash checking for some migrations #97

Open
KivApple opened this issue Jan 28, 2023 · 1 comment
Open

Ability to disable hash checking for some migrations #97

KivApple opened this issue Jan 28, 2023 · 1 comment

Comments

@KivApple
Copy link

If you use JavaScript migrations it is possible to generate an unique SQL string each time when application starts. In this case postgres-migrations will fail because of hash mismatch. It would be nice to have an ability to disable hash checking for some migrations like it's already possible to disable transactions using a special comment string.

Possible real-world scenarious when generated query might be different on every application startup:

  • Inserting rows with generated by JavaScript UUID values
  • Inserting rows with current date and/or time values obtained by JavaScript
  • Inserting salted passwords into the user table (e. g. create a first user after application installation)

Sometimes it's possible to have a workaround moving the value generation to SQL (e. g. use NOW() SQL expression, use pg_crypto module for UUID generation etc), but it's not always either possible or readable (actually complex data generation is the reason to use JavaScript migrations).

@joaoluis-pdm
Copy link

joaoluis-pdm commented Mar 24, 2024

One detail (that might happening who is searching for tips on a similar hash mismatch problem):

Linux users usually checkout git files with LF line endings, while Windows users checkout CRLF line endings.
This causes the hash to have distinct values on Windows and Linux, and makes the migration tool unusable cross-platform (for developers that share a common pg_dump with a common migrations table).

So initially, I just (forked the code) and disabled the whole hash check by commenting out the call to validateMigrationHashes() at

validateMigrationHashes(intendedMigrations, appliedMigrations)

Later, I reverted that change, and changed the hash calculation at

const hash = hashString(fileName + sql)
to be done after CRLF -> LF convertion, by replacing the sql string parameter by sql.replace(/\r\n/g, "\n"). (Only for hash calculation, as I am not very sure if multi-line SQL strings can be impacted by this change).

Hope that this helps someone else searching for a similar issue.

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

No branches or pull requests

2 participants