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

Improve status command with missing migrations #145

Merged
merged 5 commits into from
Jun 22, 2019
Merged

Improve status command with missing migrations #145

merged 5 commits into from
Jun 22, 2019

Conversation

ffissore
Copy link
Contributor

The status command compares the list of local migrations with those found on db. This may lead to misleading output when two or more developers are working on the same db but from different git branches: a migration which is applied but locally missing won't be shown by the status command.

This PR introduces "missing" migrations. The changelog is merged with the list of local migrations, and changelog entries which are not part of the list of local migrations are shown as "...missing...". Example output

------------------------------------------------------------------------
-- MyBatis Migrations - status
------------------------------------------------------------------------
ID             Applied At          Description
================================================================================
20190620071710 2019-06-20 09:41:51 create changelog
20190620071711 2019-06-20 09:41:51 first migration
20190620074320    ...missing...    migration1
20190620085416 2019-06-20 10:54:19 migration2

Here someone has applied "migration1" which is missing from our local branch.

those stored on db, and it also looks for migrations which are locally
missing but have been applied on db.
A missing migration is thus a migration applied by someone else (eg:
another developer) and locally missing.
Output of `migrate status` will look like

ID             Applied At          Description
================================================================================
20190620071710 2019-06-20 09:41:51 create changelog
20190620071711 2019-06-20 09:41:51 first migration
20190620074320    ...missing...    migration1
20190620085416 2019-06-20 10:54:19 migration2
@harawata
Copy link
Member

Hi @ffissore ,

Thanks for the PR!
I agree that the current output is not great, so +1 for the improvement.

In the proposed output, though, the 'missing' migration is actually applied to the database, so omitting the timestamp could be confusing in a different way, IMHO.

How about reusing the DatabaseOperation.checkSkippedOrMissing() method added in #125 ?

So, there will be a WARNING after the list of migrations. e.g.

------------------------------------------------------------------------
-- MyBatis Migrations - status
------------------------------------------------------------------------
ID             Applied At          Description
================================================================================
20190620071710 2019-06-20 09:41:51 create changelog
20190620071711 2019-06-20 09:41:51 first migration
20190620074320 2019-06-20 09:46:12 migration1
20190620085416 2019-06-20 10:54:19 migration2

WARNING: Missing migration script. id='20190620074320', description='migration1'.

Printing the warning is pretty easy.

if (changelog != null) {
  checkSkippedOrMissing(changelog, migrations, printStream);
}

Plus, we don't need to modify Change for this enhancement (I prefer it that way).
If highlighting the missing migration in the list is important, adding a subclass of Change to the list would do the job.

class MissingScript extends Change {
  MissingScript(BigDecimal id, String appliedTimestamp, String description) {
    super(id, appliedTimestamp, description);
  }

  @Override
  public String toString() {
    return getId() + " " + getAppliedTimestamp() + " "
        + getDescription() + " <=== MISSING!";
  }
}

The output would be...

ID             Applied At          Description
================================================================================
20190620071710 2019-06-20 09:41:51 create changelog
20190620071711 2019-06-20 09:41:51 first migration
20190620074320 2019-06-20 09:46:12 migration1 <=== MISSING!
20190620085416 2019-06-20 10:54:19 migration2

WARNING: Missing migration script. id='20190620074320', description='migration1'.

version, added MissingScript to mark a Change as missing, added output
of `checkSkippedOrMissing` to the bottom of `status` command
@ffissore
Copy link
Contributor Author

Thank you @harawata I've addressed your feedback. Output is now

ID             Applied At          Description
================================================================================
20190620071710 2019-06-21 09:02:01 create changelog
20190620071711 2019-06-21 09:02:01 first migration
20190620074320 2019-06-21 09:02:01 migration1 <=== MISSING!
20190620085416 2019-06-21 09:02:01 migration2

WARNING: Missing migration script. id='20190620074320', description='migration1'.

@harawata harawata merged commit 0089d55 into mybatis:master Jun 22, 2019
@harawata
Copy link
Member

Thank you, @ffissore !

@harawata harawata self-assigned this Jun 22, 2019
@harawata harawata added this to the 3.3.6 milestone Jun 22, 2019
@ffissore
Copy link
Contributor Author

Thank you for caring about community contributions, @harawata!

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

Successfully merging this pull request may close these issues.

2 participants