Skip to content

Commit dbe882a

Browse files
committed
Merge branch '3.1.x'
Closes gh-38231
2 parents d59b385 + f381a9c commit dbe882a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/data-initialization.adoc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ This will defer data source initialization until after any `EntityManagerFactory
6565
If you are using a <<howto#howto.data-initialization.migration-tool,Higher-level Database Migration Tool>>, like Flyway or Liquibase, you should use them alone to create and initialize the schema.
6666
Using the basic `schema.sql` and `data.sql` scripts alongside Flyway or Liquibase is not recommended and support will be removed in a future release.
6767

68+
If you need to initialize test data using a higher-level database migration tool, please see the sections about <<howto#howto.data-initialization.migration-tool.flyway-tests, Flyway>> and <<howto#howto.data-initialization.migration-tool.liquibase-tests, Liquibase>>.
69+
6870

6971

7072
[[howto.data-initialization.batch]]
@@ -185,6 +187,55 @@ See {spring-boot-autoconfigure-module-code}/liquibase/LiquibaseProperties.java[`
185187

186188

187189

190+
[[howto.data-initialization.migration-tool.flyway-tests]]
191+
==== Use Flyway for test-only migrations
192+
If you want to create Flyway migrations which populate your test database, place them in `src/test/resources/db/migration`.
193+
A file named, for example, `src/test/resources/db/migration/V9999__test-data.sql` will be executed after your production migrations and only if you're running the tests.
194+
You can use this file to create the needed test data.
195+
This file will not be packaged in your uber jar or your container.
196+
197+
198+
199+
[[howto.data-initialization.migration-tool.liquibase-tests]]
200+
==== Use Liquibase for test-only migrations
201+
If you want to create Liquibase migrations which populate your test database, you have to create a test changelog which also includes the production changelog.
202+
203+
First, you need to configure Liquibase to use a different changelog when running the tests.
204+
One way to do this is to create a Spring Boot `test` profile and put the Liquibase properties in there.
205+
For that, create a file named `src/test/resources/application-test.properties` and put the following property in there:
206+
207+
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
208+
----
209+
spring:
210+
liquibase:
211+
change-log: "classpath:/db/changelog/db.changelog-test.yaml"
212+
----
213+
214+
This configures Liquibase to use a different changelog when running in the `test` profile.
215+
216+
Now create the changelog file at `src/test/resources/db/changelog/db.changelog-test.yaml`:
217+
218+
[source,yaml,indent=0,subs="verbatim"]
219+
----
220+
databaseChangeLog:
221+
- include:
222+
file: classpath:/db/changelog/db.changelog-master.yaml
223+
- changeSet:
224+
runOrder: "last"
225+
id: "test"
226+
changes:
227+
# Insert your changes here
228+
----
229+
230+
This changelog will be used when the tests are run and it will not be packaged in your uber jar or your container.
231+
It includes the production changelog and then declares a new changeset, whose `runOrder: last` setting specifies that it runs after all the production changesets have been run.
232+
You can now use for example the https://docs.liquibase.com/change-types/insert.html[insert changeset] to insert data or the https://docs.liquibase.com/change-types/sql.html[sql changeset] to execute SQL directly.
233+
234+
The last thing to do is to configure Spring Boot to activate the `test` profile when running tests.
235+
To do this, you can add the `@ActiveProfiles("test")` annotation to your `@SpringBootTest` annotated test classes.
236+
237+
238+
188239
[[howto.data-initialization.dependencies]]
189240
=== Depend Upon an Initialized Database
190241
Database initialization is performed while the application is starting up as part of application context refresh.

0 commit comments

Comments
 (0)