-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add repeat conditional migrations #39
Conversation
@reedstrm Can you have a look at this? We can update Connexions/db-migrator when the changes are merged. |
0e30aee
to
7cfa9fb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just had a couple questions, but I like the ideas! (didn't look at how extensive the tests were though)
@@ -69,10 +69,16 @@ Example usage:: | |||
generates a file called ``migrations/20151217170514_add_id_to_users.py`` | |||
with content:: | |||
|
|||
# Uncomment should_run if this is a repeat migration | |||
# def should_run(cursor): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which case would a migration run more than once? I thought that the DB would just have a table with all the migrations that successfully ran, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a case for schema migrations, more for data migrations. Please see the original issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For something like a data-migration, you could write an up
that stored the previous data in a side table, then down
would copy that back in place. And set a should_run
to refuse to run if the backup exists (or does something clever with timestamps for the name of the backup) Not too different than the existing down
scripts, which tend to be frozen copies of old schema.
cursor.execute('INSERT INTO table VALUES (%s)', (data,)) | ||
|
||
|
||
def down(cursor): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be out-of-scope but I don't think there is a way to roll back all migrations. For example, CNXML->HTML would need to check out the previous version of the XSLT files and run them in order to roll back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For any migrations that cannot be rolled back. You can do a pass
for down
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, me thinks I'd want to put "has this migration already been applied" tests in "should_run", which then makes then idem-potent to some extent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(somehow my comments got reversed :-) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what "should_run" is, you can add a check to "up" for non repeatable migrations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true, true.
👍 |
It occurs to me that eventually, we'll have a huge number of "repeatable" migrations, which are marked "applied" (with the most recent application timestamp, I presume?) But that must be tested every time any migration happens, even 10 years later when they no longer apply. Even the test may no longer apply. I guess there's nothing that says we can't "cosolidate" migrations by deleting them, meaning we no longer support migrating from the state "just in front of" that migration. I'd like some way to tag a migration as "no need to use this one ever again on this database" - a terminal "no more repeats" .For the cases where the DBA/devops can say "the code that generates that bad data no longer runs, we do not need to migrate anymore" Since the existing table is "schema_migrations" does it make sense to instead track these as "data_migrations". The "should_run" function could be described as "run tests against existing data to determine if there is work for this data migration to do". That's what I intend to write, BTW - |
If presence of |
|
I think if we add irreversible migrations that are only run if |
7cfa9fb
to
b4839b8
Compare
b4839b8
to
00e4473
Compare
Now that I think about it, if you comment out |
Turns out that, in order to be able to do a "down" for the data migration, I needed to store my own state anyway, so no need for extra table stuff. This seems to be working as_is_ for me as well. I haven't ran it a bunch yet on a larger test data set, which is my actual use-case: I want to have an 'up' that only does part of the work, so is worth calling repeatedly. I'll assume that the date applied will update to reflect the last_date applied. THe only problem is with that isapplied is no longer enough info in |
If the migration file has `should_run` defined, it is considered a repeat migration. `should_run` is called every time `migrate` is run, and if it returns true, `up` is run.
When a migration's up function has @deferred, the migration is automatically set to "deferred". It will run when `migrate --run-deferred` is called.
If migration is not found, "mark" was displaying an error message but exiting normally. The error message is still printed and dbmigrator will now exits with code 1.
There is no way to show migrations as "repeated" when listing them. It is useful to know which migrations might run when running "migrate".
0dae398
to
32ddb9e
Compare
If the migration file has
should_run
defined, it is considered arepeat migration.
should_run
is called every timemigrate
is run,and if it returns true,
up
is run.Close Connexions/db-migrator#1