Skip to content

Commit d4408ab

Browse files
committed
Merge pull request #48169 from vpavic
* pr/48169: Polish "Revise "Use Liquibase for test-only migrations" section" Revise "Use Liquibase for test-only migrations" section Closes gh-48169
2 parents 2f27912 + 1650092 commit d4408ab

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/data-initialization.adoc

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -207,41 +207,20 @@ This file will not be packaged in your uber jar or your container.
207207
[[howto.data-initialization.migration-tool.liquibase-tests]]
208208
=== Use Liquibase for Test-only Migrations
209209

210-
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.
210+
If you want to create Liquibase migrations which populate your test database, you can leverage https://docs.liquibase.com/reference-guide/changelog-attributes/what-are-contexts[Liquibase contexts].
211+
See also the related https://www.liquibase.com/blog/contexts-vs-labels[blog post].
211212

212-
First, you need to configure Liquibase to use a different changelog when running the tests.
213-
One way to do this is to create a Spring Boot `test` profile and put the Liquibase properties in there.
214-
For that, create a file named `src/test/resources/application-test.properties` and put the following property in there:
213+
In practical terms, this translates into adding a `context:@test` attribute to changesets containing test data, for example:
215214

216-
[configprops,yaml]
217-
----
218-
spring:
219-
liquibase:
220-
change-log: "classpath:/db/changelog/db.changelog-test.yaml"
215+
[source,sql]
221216
----
217+
--liquibase formatted sql
222218
223-
This configures Liquibase to use a different changelog when running in the `test` profile.
224-
225-
Now create the changelog file at `src/test/resources/db/changelog/db.changelog-test.yaml`:
226-
227-
[source,yaml]
228-
----
229-
databaseChangeLog:
230-
- include:
231-
file: classpath:/db/changelog/db.changelog-master.yaml
232-
- changeSet:
233-
runOrder: "last"
234-
id: "test"
235-
changes:
236-
# Insert your changes here
219+
--changeset alice:1 context:@test
220+
insert into project (id, name) values (1, 'Spring Boot');
237221
----
238222

239-
This changelog will be used when the tests are run and it will not be packaged in your uber jar or your container.
240-
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.
241-
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.
242-
243-
The last thing to do is to configure Spring Boot to activate the `test` profile when running tests.
244-
To do this, you can add the `@ActiveProfiles("test")` annotation to your javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation] annotated test classes.
223+
And using `spring.liquibase.contexts=test` in environments where you would like changesets containing test data to be applied.
245224

246225

247226

0 commit comments

Comments
 (0)