Skip to content

Commit b35c7cb

Browse files
committed
Add documentation.
See #756 Original pull request: #1520
1 parent b2950bf commit b35c7cb

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/main/asciidoc/index.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include::{spring-data-commons-docs}/repositories.adoc[leveloffset=+1]
2323
= Reference Documentation
2424

2525
include::jdbc.adoc[leveloffset=+1]
26+
include::schema-support.adoc[leveloffset=+1]
2627

2728
[[appendix]]
2829
= Appendix

src/main/asciidoc/schema-support.adoc

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[[jdbc.schema]]
2+
= Schema Creation
3+
4+
When working with SQL databases, the schema is an essential part.
5+
Spring Data JDBC supports a wide range of schema options yet when starting with a domain model it can be challenging to come up with an initial domain model.
6+
To assist you with a code-first approach, Spring Data JDBC ships with an integration to create database change sets using https://www.liquibase.org/[Liquibase].
7+
8+
Consider the following domain entity:
9+
10+
[source,java]
11+
----
12+
@Table
13+
class Person {
14+
@Id long id;
15+
String firstName;
16+
String lastName;
17+
LocalDate birthday;
18+
boolean active;
19+
}
20+
----
21+
22+
Rendering the initial ChangeSet through the following code:
23+
24+
[source,java]
25+
----
26+
27+
RelationalMappingContext context = … // The context contains the Person entity, ideally initialized through initialEntitySet
28+
LiquibaseChangeSetWriter writer = new LiquibaseChangeSetWriter(context);
29+
30+
writer.writeChangeSet(new FileSystemResource(new File(…)));
31+
----
32+
33+
yields the following change log:
34+
35+
[source,yaml]
36+
----
37+
databaseChangeLog:
38+
- changeSet:
39+
id: '1685969572426'
40+
author: Spring Data Relational
41+
objectQuotingStrategy: LEGACY
42+
changes:
43+
- createTable:
44+
columns:
45+
- column:
46+
autoIncrement: true
47+
constraints:
48+
nullable: false
49+
primaryKey: true
50+
name: id
51+
type: BIGINT
52+
- column:
53+
constraints:
54+
nullable: true
55+
name: first_name
56+
type: VARCHAR(255 BYTE)
57+
- column:
58+
constraints:
59+
nullable: true
60+
name: last_name
61+
type: VARCHAR(255 BYTE)
62+
- column:
63+
constraints:
64+
nullable: true
65+
name: birthday
66+
type: DATE
67+
- column:
68+
constraints:
69+
nullable: false
70+
name: active
71+
type: TINYINT
72+
tableName: person
73+
----
74+
75+
Column types are computed from an object implementing the `SqlTypeMapping` strategy interface.
76+
Nullability is inferred from the type and set to `false` if a property type use primitive Java types.
77+
78+
Schema support can assist you throughout the application development lifecycle.
79+
In differential mode, you provide an existing Liquibase `Database` to the schema writer instance and the schema writer compares existing tables to mapped entities and derives from the difference which tables and columns to create/to drop.
80+
By default, no tables and no columns are dropped unless you configure `dropTableFilter` and `dropColumnFilter`.
81+
Both filter predicate provide the table name respective column name so your code can computer which tables and columns can be dropped.
82+
83+
[source,java]
84+
----
85+
writer.setDropTableFilter(tableName -> …);
86+
writer.setDropColumnFilter((tableName, columnName) -> …);
87+
----
88+
89+
NOTE: Schema support can only identify additions and removals in the sense of removing tables/columns that are not mapped or adding columns that do not exist in the database.
90+
Columns cannot be renamed nor data cannot be migrated because entity mapping does not provide details of how the schema has evolved.

0 commit comments

Comments
 (0)