-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[cli] new feature: 'migrate info': warn about changed-after-installed migrations #870
Comments
This is an excellent idea. Here is how Flyway does
I think I would suggest changing the state of the migration and indicating its a failure state with a red color:
In the same vein, I'd also suggest adding the name-of-migration to that particular error message:
|
This could also be useful as Edit: that may be an antipattern though as exit codes on Unix are unsigned mod 256 and some are already reserved for some common uses: https://tldp.org/LDP/abs/html/exitcodes.html |
I do like the idea of conveying specific statuses (more than just success/non-success) through the exit status where it makes sense to do so, but it is probably a good idea to keep the total universe of such codes small -- probably less than five, and almost certainly less than 10, but who knows... For a general purpose "check" command, it would probably be desirable at some point to convey richer information than can be conveyed through exit status numbers. For those use cases, it is likely to be better to have a small number of exit status codes to indicate coarse status ("ran successfully", "couldn't connect", "no local migrations present", etc.), and emit the richer detail (perhaps in a machine readable format) on stdout (or something). For comparison with some widely used tools:
|
It seems superfluous to have basically the same subcommand just under different names and one changes its exit code based on migration status. |
@abonander Yeah, I agree, if that would be the only difference. But if that's the case, then I misunderstood your intent. I guess I was reading too much into the word "check" in Apologies for the noise. |
So let me just ask. Is there a plan to support some kind of command to help fix this issue? As a new user of this library, I just got stuck on this. I think one thing that would help me personally is a way to re-validate a migration. Here's my current situation:
I don't really want to drop my database (or rollback) to re-run the migration, since I already went through the effort of adding the test data. What I'd like to be able to do is update the Basically, I think this use case should be considered. I'm capable of working around it, but it's going to happen again and again. EDIT: Just in case anyone else finds themselves in this situation, here's the workaround: cat migrations/<name>.sql | openssl dgst -sha384 | cut -d ' ' -f 2 UPDATE _sqlx_migrations SET checksum = '\x<hash>' WHERE version = <version>; |
I think this is kind of broken for development, especially where the developer is unfamiliar with the cli interface and the expected workflow and making mistakes (or at least poking around and breaking things). Somewhere, as a developer, we would need a full CRUD interface where we can apply, re-apply, update, rollback and remove. It's also completely unclear (and a little surprising/irritating) that there was an addition made to the database. While I think the database is a nice way to control migrations, I may not want those in my database. While developing, I'm definitely going to end up running this quite a bit: DROP TABLE IF EXISTS _sqlx_migrations Ideally, for the issue @nixpulvis mentioned, we'd have a CLI for updating the checksum. |
I wrote a little migration script to overwrite all migrations for the need of: https://github.com/windmill-labs/windmill I thought it could be useful to anyone reading this thread: import sys
import os
import subprocess
database_url = sys.argv[1]
print(f"database_url: {database_url}")
if database_url is None:
print("Please provide a database url")
sys.exit(1)
for f in os.listdir("migrations"):
if f.endswith(".up.sql"):
version = f.split("_")[0]
cmd = f"cat migrations/{f} | openssl dgst -sha384 | cut -d ' ' -f 2"
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
digest = ps.communicate()[0].decode("utf-8").strip()
cmd = f"psql '{database_url}' -c \"UPDATE _sqlx_migrations SET checksum = '\\x{digest}' WHERE version = {version};\""
ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
out = ps.communicate()[0].decode("utf-8").strip()
print(version, digest, out) |
Related to:
Related tangentially to:
sqlx migrate run
should support--dry-run
#357 (--dry-run
support forsqlx migrate run
)It would be useful to have
sqlx migrate info
warn the user about migrations whose file checksum no longer matches the value recorded in the db, similar to howsqlx database setup
does.For example, if I:
sqlx database setup
sqlx database setup
Upon the second setup invocation an error is emitted:
error: migration 20201202222356 was previously applied but has been modified
That's quite nice.
From the perspective of treating the
sqlx migrate info
command as the thing to use to answer the question, "Where does the database stand with respect to the available migrations?", it would be nice if it were able to provide similar information. Currently it shows that the migration was successfully installed at some point in the past, but does not give any indication that the migration script has been modified:The text was updated successfully, but these errors were encountered: