How to test database evolution in Play Framework 2

I use Play Framework 2.3 and made a database evolution that is somewhat involved and requires updating new fields with values ​​computed from old fields (which are removed during the evolution). It would be nice to verify that evolution works as expected, that is, verify that the new fields are filled with the correct values. However, I could not find the best practice for testing the evolution of the database. In fact, I'm not even sure how to apply evolution during the test.

Any suggestions?

+4
source share
1 answer

, Play , FakeApplication. . https://www.playframework.com/documentation/2.2.x/JavaTest .

Evolution .

java:

@Before
public void setUp() throws IOException {
  fakeapp = Helpers.fakeApplication(testconfiguration)
  evolutions = Evolutions.applicationEvolutions(fakeapp.getWrappedApplication().path(), fakeapp.getWrappedApplication().classloader(), "database");
  Iterator<Evolution> iterator = evolutions.iterator();

  while (iterator.hasNext()) {
    Evolution current = iterator.next();
    if (latestEvolution == null) {
      latestEvolution = current;
    } else {
      latestEvolution = current.revision() > latestEvolution.revision() ? current : latestEvolution;
    }
  }

  latestEvolutionFile = new File(fakeapp.getWrappedApplication().path(), "path/to/evolution/files" + latestEvolution.revision() + ".sql");
  latestEvolutionFileContent = Files.readAllBytes(Paths.get(latestEvolutionFile.getAbsolutePath()));
}

@Test
public void testLatestDownEvolution() throws IOException {
  try {
    // arrange
    // modify the latest evolution file, so when executing the evolution plugin a 2nd time the latest evolution file will be applied again
    Evolutions.updateEvolutionScript("database", latestEvolution.revision(), "", "", "", fakeapp.getWrappedApplication());

    // act
    app.getWrappedApplication().plugin(EvolutionsPlugin.class).get().onStart();

    // when no exception has been thrown, then we are fine

  } catch (InconsistentDatabase ex) {
    Assert.fail(ex.subTitle() + " " + ex.rev() + ".sql");
  } finally {
    // just bring the modified script back to his original content, cause we don't want to commit that in our VCS...
    try (FileOutputStream stream = new FileOutputStream(latestEvolutionFile)) {
    stream.write(latestEvolutionFileContent);
  }
}

, ebean, .

+1

Source: https://habr.com/ru/post/1616042/


All Articles