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

Fix migration file names #25735

Closed
wants to merge 3 commits into from

Conversation

sorenisanerd
Copy link

The file names of the migrations and the comments in
models/migrations/migrations.go disagree on the version number.

For example:

// v259 -> v260
NewMigration("Convert scoped access tokens", v1_20.ConvertScopedAccessTokens),

...but the file containing v1_20.ConvertScopedAccessTokens is called
v1_20/v259.go.

Checking the version table indicates that the comments are correct
and the file names are wrong.

This PR fixes the filenames. It's spread over two commits in an attempt
to make git understand that they're renames. See individual commit
messages for more detail.

The file names of the migrations and the comments in
`models/migrations/migrations.go` disagree on the version number.

For example:

    // v259 -> v260
    NewMigration("Convert scoped access tokens", v1_20.ConvertScopedAccessTokens),

...but the file containing `v1_20.ConvertScopedAccessTokens` is called
`v1_20/v259.go`.

Checking the `version` table indicates that the comments are correct
and the file names are wrong.

git does not explicitly track renames. This causes a problem when we're
renaming many files. If a directory had e.g. 3 files in it:

v1_50/v500.go
v1_50/v501.go
v1_50/v502.go

and we incremented their filenames like so:

v1_50/v500.go -> v1_50/v501.go
v1_50/v501.go -> v1_50/v502.go
v1_50/v502.go -> v1_50/v503.go

git would say that we deleted `v500.go`, changed `v501.go` and `v502.go`,
and created a new file, `v503.go`.

To work around this, this change is split into two commits. This first one
increments all the file names and moves the files to a new directory. git
correctly detects these as renames. The next commit will move them all back.

This is the command used for this commit (cwd: `models/migrations`):

    for x in */v*.go
    do
      mkdir -p $(dirname $x).new
      v=$(echo $x | sed -e 's!.*/v\([0-9]*\).*!\1!g')
      git mv $x $(dirname $x).new/v$(echo $x | sed -e 's@.*/v\([0-9]*\)\(.*\)@'$(($v+1))'\2@g')
    done
This is the second part of this mass renaming. See previous commit
for rationale.

This is the command used for this commit (cwd: `models/migrations`):

    for x in *.new/v*.go
    do
      git mv $x $(basename $(dirname $x) .new)/
    done
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 6, 2023
@pull-request-size pull-request-size bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jul 6, 2023
Copy link
Member

@KN4CK3R KN4CK3R 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 the individual commits.

Out of curiosity, does Gitea display the changes only as renames or like Github does?

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jul 6, 2023
@6543
Copy link
Member

6543 commented Jul 7, 2023

the pull diff is hard to read :/

@lunny
Copy link
Member

lunny commented Jul 7, 2023

Maybe we can rename v259.go -> 260.go so that git can recognize the rename. And next PR we can also rename back if we need that.

@wxiaoguang
Copy link
Contributor

The root problem is "how to understand the 'version' and the comment".

For example:

	// v70 -> v71
	NewMigration("add issue_dependencies", v1_6.AddIssueDependencies),

Before:

  • version means the next migration's starting number
  • So the number of AddIssueDependencies is 70 (v70.go), and database.version=71 (for next)
  • The comment means "the current step is v70 and after this step and then the database version becomes 71"

After:

  • version means the current migration's starting number
  • So the number of AddIssueDependencies will be 71, and database.version=71 (for current)
  • The comment for each step can be simplified to "// v71" ...
  • The comment above (Gitea 1.5.0 ends at v69) seems unmatched, while fortunately, the database version for 1.5.x should be actually 70, so it's only a comment problem).

wxiaoguang
wxiaoguang previously approved these changes Jul 7, 2023
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jul 7, 2023
@wxiaoguang wxiaoguang self-requested a review July 7, 2023 04:26
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels Jul 7, 2023
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jul 7, 2023

I also agree with using some tricks like "v259.go -> 260.go" (or v-260.go) is better. (the v prefix seems more readable and easier to maintain)

@wolfogre
Copy link
Member

wolfogre commented Jul 7, 2023

My suggestion is v259.go -> v0260.go, so git can recognize the rename and the files order will be always correct (v0001.go ... v9999.go).

If one day we get v9999.go, we should be celebrating, not annoying. 👀

@KN4CK3R
Copy link
Member

KN4CK3R commented Jul 7, 2023

the pull diff is hard to read :/

If you look at the two individual commits you only see renames, so it's fine.

@wolfogre
Copy link
Member

wolfogre commented Jul 7, 2023

the pull diff is hard to read :/

If you look at the two individual commits you only see renames, so it's fine.

I don't think it's fine, if the final merged commit cannot recognize the rename, it can be annoying when git blame.

@wxiaoguang
Copy link
Contributor

Agree with lunny and wolfogre. When this PR gets "squashed" into main branch, we should make sure there is a clear commit history. Although these two separate commits are clear one by one, but, when they get squashed, the commit history will be a mess.

So, there are some suggestions for "only one clear commit".

  • lunny: v259.go -> 260.go
  • wxiaoguang: v259.go -> v-260.go (or use some suffix?) I think a proper prefix really helps about maintainability.
  • wolfogre: v259.go -> v0260.go

@KN4CK3R
Copy link
Member

KN4CK3R commented Jul 7, 2023

Alternative 260-short_description.go like the text in NewMigration.

GitLab does the same: https://gitlab.com/gitlab-org/gitlab/-/tree/master/db/migrate

@6543
Copy link
Member

6543 commented Jul 7, 2023

Hmm or make an exemption and dont squashmerge?

@wxiaoguang
Copy link
Contributor

Alternative 260-short_description.go like the text in NewMigration.

GitLab does the same: https://gitlab.com/gitlab-org/gitlab/-/tree/master/db/migrate

I also have asked so, but at that time, in discord discussion, maintainers explained that "Only use the v123.go, to make sure the filename conflicts for different PRs, that's the conflict-based workflow".

I am not sure whether maintainers have new ideas about this topic nowadays.

@techknowlogick
Copy link
Member

@wxiaoguang I wrote https://github.com/techknowlogick/xormigrate to 1. backport migrations, 2. prevent conflicts from migrates having the same name, and 3. a cleaner developer experience.

@wxiaoguang
Copy link
Contributor

Agree with lunny and wolfogre. When this PR gets "squashed" into main branch, we should make sure there is a clear commit history. Although these two separate commits are clear one by one, but, when they get squashed, the commit history will be a mess.

So, there are some suggestions for "only one clear commit".

* lunny: v259.go -> `260.go`

* wxiaoguang: v259.go -> `v-260.go` (or use some suffix?) I think a proper prefix really helps about maintainability.

* wolfogre: v259.go -> `v0260.go`

I think there should be some better solutions than this change, this change's "squash" merge would make the commit unreadable.

@wxiaoguang wxiaoguang added the issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail label Aug 12, 2023
@wxiaoguang wxiaoguang removed their request for review August 12, 2023 07:03
@GiteaBot GiteaBot removed the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Aug 12, 2023
@GiteaBot GiteaBot added the lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. label Aug 12, 2023
@wxiaoguang wxiaoguang dismissed their stale review August 12, 2023 07:04

dismiss review

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels Aug 12, 2023
@wxiaoguang wxiaoguang added the status/blocked This PR cannot be merged yet, i.e. because it depends on another unmerged PR label Aug 12, 2023
@sorenisanerd
Copy link
Author

Alternatively, we could just get rid of the individual comments in migrations.go? They're not really adding anything but confusion. The order they're listed in is really what matters.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Aug 15, 2023

Alternatively, we could just get rid of the individual comments in migrations.go? They're not really adding anything but confusion. The order they're listed in is really what matters.

IMO the comments should be kept, that's the only way (at the moment, by reading code) to know the "version number" of a migration step.

@wxiaoguang
Copy link
Contributor

Stale and a lot of conflicts.

Feel free to reopen if some comments (like #25735 (comment)) are addressed. It needs a clear "squahed" result.

@wxiaoguang wxiaoguang closed this Sep 7, 2023
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Dec 6, 2023
@wxiaoguang
Copy link
Contributor

-> Refactor the DB migration system slightly #32344

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail lgtm/need 1 This PR needs approval from one additional maintainer to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. status/blocked This PR cannot be merged yet, i.e. because it depends on another unmerged PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants