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

Simplify migrations #1510

Merged
merged 3 commits into from
May 15, 2024
Merged

Conversation

AdrianFreundQC
Copy link
Contributor

Resolves #326

Here is a suggestion for how to simplify migrations. Some changes made to migrations were also applied to tasks for consistency.
The task configuration is 100% backwards compatible. The migration configuration is not backwards compatible. The old style should be seen as deprecated. This PR support both old and new style and shows a warning when the old style is used.

Tasks

Tasks can still be configured like before:

_tasks:
  - git init

Additionally Tasks can be a dictionary, which allows passing one or more of the optional arguments when and working_directory. working_directory defaults to the destination directory and can be either relative to it, or absolute. Both arguments can be templated.

_tasks:
  - command: git init
    working_directory: "submodule"
    when: "{{ enable_submodules }}"

Migrations

It is no longer necessary to duplicate the same command line for every version to run a migration script.

_migrations:
  - "{{ _copier_python }} {{ _copier_conf.src_path }}/migrate.py"

This runs twice (before and after) on every update and still passes environment variables to the script.
Note that it can't pass $VERSION_(PEP440_)CURRENT because there is no current version. Only a from and to version.

It is still possible to only run migrations on a specific version:

_migrations:
  - version: v1.4
    command: rm old_file.txt || true

In this case $VERSION_(PEP440_)CURRENT is still passed to the command.

This still runs both before and after.
working_directory and when are also supported for migrations. In addition all the environment variables passed to the command are also available for use in templating:

_migrations:
  - version: v1.4
    command: rm old_file.txt
    when: "{{ _stage == 'before' }}"

Current State

All the features described here work.
I still want to do a bit of code cleanup and improve the documentation in the next few days.
I would also like some feedback on this design. Is there anything you don't agree with? Something I should change?

Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the initiative and working on this long-standing feature proposal, @AdrianFreundQC! 🙏

I've done a first review iteration over the main code and docs (not the tests yet). It looks great, just a couple of minor comments so far.

copier/main.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
Copy link

codecov bot commented Feb 12, 2024

Codecov Report

Attention: Patch coverage is 99.06103% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 97.38%. Comparing base (7c174a9) to head (d80c011).

Files Patch % Lines
copier/main.py 90.47% 2 Missing ⚠️
copier/template.py 96.55% 1 Missing ⚠️
tests/helpers.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1510      +/-   ##
==========================================
- Coverage   97.39%   97.38%   -0.02%     
==========================================
  Files          48       49       +1     
  Lines        4728     5004     +276     
==========================================
+ Hits         4605     4873     +268     
- Misses        123      131       +8     
Flag Coverage Δ
unittests 97.38% <99.06%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@AdrianFreundQC AdrianFreundQC force-pushed the simplify-migrations branch 5 times, most recently from f62dbf5 to aa1c40b Compare February 15, 2024 15:41
@AdrianFreundQC AdrianFreundQC requested a review from sisp February 16, 2024 18:18
@AdrianFreundQC
Copy link
Contributor Author

I pushed the fixes as new commits to simplify review.
I can rebase them back into three commits (feat, test, docs) or you can just squash merge if you prefer that.

@AdrianFreundQC
Copy link
Contributor Author

CI failure looks unrelated. Should be fixed with a re-run

docs/configuring.md Outdated Show resolved Hide resolved
@AdrianFreundQC
Copy link
Contributor Author

One more thought I had: the migration command running twice on every update might be confusing and unexpected for new users, however it is required for more complex migrations.

Changing the default value for when from true to {{ _state == "after" }} might solve this issue. Migrations without when only run once, while experience users can still set when to true to have them run in the before and after stage.

@sisp
Copy link
Member

sisp commented Feb 24, 2024

I like this idea, it sounds like a better default behavior than running the command twice. 👌

By the way, I haven't forgotten about reviewing this PR, just haven't had enough time yet. 🙁 I'll try to do it on Monday. 🤞

Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done a full review this time. The implementation looks good to me. The legacy format test does not ensure identical behavior as before anymore, and I think the new format needs a bit more systematic testing (because it's more powerful/flexible). I think keeping the legacy tests unchanged and adapting and extending a copy thereof for the new format would be better than surgically adding some testing of the new format and mixing it with the legacy format (see my inline comment for more details).

copier/main.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
docs/configuring.md Show resolved Hide resolved
tests/demo_legacy_migrations/migrations.py Outdated Show resolved Hide resolved
tests/test_migrations.py Show resolved Hide resolved
Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely looks like the path forward. Thanks for taking care of all this!

copier/main.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
docs/configuring.md Outdated Show resolved Hide resolved
tests/test_migrations.py Show resolved Hide resolved
docs/configuring.md Show resolved Hide resolved
docs/configuring.md Show resolved Hide resolved
@AdrianFreundQC
Copy link
Contributor Author

Sorry for the delay. I've been quite busy recently.
I'm still working on the tests for the new migrations implementation.
I'll have some time in the next two weeks and if that's not enough I'll probably be able to finish it in the first half of May.

@sisp
Copy link
Member

sisp commented Apr 2, 2024

Thanks for the update and your continuous work on this PR, @AdrianFreundQC! 🙏

@AdrianFreundQC AdrianFreundQC force-pushed the simplify-migrations branch 4 times, most recently from 22d1ce4 to 7d5c7be Compare April 2, 2024 15:18
@AdrianFreundQC AdrianFreundQC force-pushed the simplify-migrations branch 4 times, most recently from 131b22c to 9d31db2 Compare April 25, 2024 15:58
@AdrianFreundQC
Copy link
Contributor Author

AdrianFreundQC commented Apr 25, 2024

I have no idea why the MacOS flake test is suddenly failing now. Maybe a broken cache?

/Users/runner/work/_temp/be5ceacc-d596-4a1f-b513-54880e12e09d.sh: /Users/runner/work/copier/copier/.venv/bin/copier: /Users/runner/work/copier/copier/.venv/bin/python: bad interpreter: No such file or directory

The flake check runs fine on my machine. Also all the other tests work now.

If you want you can already review the new tests. I'll go through all the discussions again and see if there's anything else I still have to do.

@sisp
Copy link
Member

sisp commented Apr 25, 2024

@AdrianFreundQC Sorry for the inconvenience. It's because macos-latest on GitHub-hosted runners got bumped to macOS 14 (ARM) for which there are no binaries for Python 3.8 and 3.9. Before, macos-latest was referring to macOS 12 (x64) for which those binaries existed. See #1602 (review) for more details.

We're waiting for @yajo to resolve #1604 (because of a project-level settings change only he can make). Then, after rebasing your PR, CI checks should work again.

@sisp
Copy link
Member

sisp commented Apr 25, 2024

One sec, your Python 3.8 and 3.9 CI checks are passing. 🤔 It seems GitHub has added those missing binaries. Let me get back to you.

@AdrianFreundQC AdrianFreundQC force-pushed the simplify-migrations branch 2 times, most recently from de08c15 to 4756477 Compare April 25, 2024 17:51
@AdrianFreundQC
Copy link
Contributor Author

I think all discussions should have been addressed now and the CI seems to be working (assuming my last change didn't break anything).

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very amazing work, Thanks!

There's just one minor suggestion to enhance docs. Would you like to push it before merging?

docs/configuring.md Outdated Show resolved Hide resolved
Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, @AdrianFreundQC! 👌 Just some totally minor remarks from my side. Plus in general, some lines seem very long. Would you mind running Ruff's formatter once?

About code formatting: IIRC, there is no Nix-based pre-commit hook for Ruff's formatter, which would explain why the flake-check checks pass although I'm sure Ruff's formatter would disagree. @yajo Is there a way to enable Ruff's formatter to check code formatting? And does the Nix-locked version of Ruff even have the formatter already? We might need to update the lock file.

copier/template.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
@AdrianFreundQC AdrianFreundQC force-pushed the simplify-migrations branch 2 times, most recently from 0713185 to 1dd1228 Compare May 2, 2024 11:35
@AdrianFreundQC
Copy link
Contributor Author

I ran addressed the two comments above and ran ruff format (nix pinned version) on main.py and template.py. I didn't run it on all files to not clutter this PR.

I also rebased on master again.

@AdrianFreundQC AdrianFreundQC requested a review from sisp May 14, 2024 13:19
Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. 🫣

Content-wise it looks good to me. I still see some minor formatting issues. Would you mind trying to fix them?

tests/helpers.py Outdated Show resolved Hide resolved
tests/test_migrations.py Show resolved Hide resolved
tests/test_migrations.py Show resolved Hide resolved
tests/test_migrations.py Show resolved Hide resolved
tests/test_migrations.py Outdated Show resolved Hide resolved
tests/test_migrations.py Outdated Show resolved Hide resolved
tests/test_tasks.py Outdated Show resolved Hide resolved
tests/test_migrations.py Outdated Show resolved Hide resolved
@AdrianFreundQC
Copy link
Contributor Author

No problem.
I reformatted the files and rebased on master again.

Copy link
Member

@sisp sisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now! 🎉 Thank you very much for the hard work, implementing this great feature with proper tests, and patience with the reviews even on pedantic formatting fixes! 🙏 Fantastic job! 🥇 Let's get this merged! 🚀

@sisp sisp merged commit d6f226f into copier-org:master May 15, 2024
21 checks passed
@AdrianFreundQC AdrianFreundQC deleted the simplify-migrations branch May 15, 2024 13:14
@AdrianFreundQC
Copy link
Contributor Author

Do you have an ETA on when the next version might get released?

@yajo
Copy link
Member

yajo commented Jun 28, 2024

I'd like to at least have #1598 fixed before release. It appeared because of this merge IIRC. It should be quite easy to fix though, I just had very little time lately. Once that one's fixed I'll push the new release. Would you want to contribute it?

@yajo
Copy link
Member

yajo commented Jun 28, 2024

Ah, nevermind. I just opened #1681. It was an easy one.

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

Successfully merging this pull request may close these issues.

Simplify migrations
6 participants