Skip to content

Commit 5f4ef2f

Browse files
kurtn718mp911de
authored andcommitted
Add support for schema creation using Liquibase.
We now support schema creation and schema migration by generating Liquibase changesets from mapped entities. We also support evolution of schema by comparing existing tables with mapped entities to compute differential changesets. Closes #756 Original pull request: #1520
1 parent b92586f commit 5f4ef2f

File tree

14 files changed

+886
-74
lines changed

14 files changed

+886
-74
lines changed

pom.xml

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<properties>
2222
<dist.id>spring-data-jdbc</dist.id>
2323
<springdata.commons>3.2.0-SNAPSHOT</springdata.commons>
24+
<liquibase.version>4.21.1</liquibase.version>
2425
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
2526

2627
<!-- dependency versions -->
@@ -91,6 +92,29 @@
9192
</roles>
9293
<timezone>-6</timezone>
9394
</developer>
95+
<developer>
96+
<id>ogierke</id>
97+
<name>Oliver Gierke</name>
98+
<email>ogierke(at)pivotal.io</email>
99+
<organization>Pivotal Software, Inc.</organization>
100+
<organizationUrl>https://pivotal.io</organizationUrl>
101+
<roles>
102+
<role>Project Contributor</role>
103+
</roles>
104+
<timezone>+1</timezone>
105+
</developer>
106+
<developer>
107+
<id>kurtn718</id>
108+
<name>Kurt Niemi</name>
109+
<email>kniemi(at)vmware.com</email>
110+
<organization>VMware.</organization>
111+
<organizationUrl>https://vmware.com</organizationUrl>
112+
<roles>
113+
<role>Project Contributor</role>
114+
</roles>
115+
<timezone>-5</timezone>
116+
</developer>
117+
94118
</developers>
95119

96120
<profiles>

spring-data-jdbc/pom.xml

+8-47
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,6 @@
2525

2626
<inceptionYear>2017</inceptionYear>
2727

28-
<developers>
29-
<developer>
30-
<id>schauder</id>
31-
<name>Jens Schauder</name>
32-
<email>jschauder(at)pivotal.io</email>
33-
<organization>Pivotal Software, Inc.</organization>
34-
<organizationUrl>https://pivotal.io</organizationUrl>
35-
<roles>
36-
<role>Project Lead</role>
37-
</roles>
38-
<timezone>+1</timezone>
39-
</developer>
40-
<developer>
41-
<id>gregturn</id>
42-
<name>Greg L. Turnquist</name>
43-
<email>gturnquist(at)pivotal.io</email>
44-
<organization>Pivotal Software, Inc.</organization>
45-
<organizationUrl>https://pivotal.io</organizationUrl>
46-
<roles>
47-
<role>Project Contributor</role>
48-
</roles>
49-
<timezone>-6</timezone>
50-
</developer>
51-
<developer>
52-
<id>ogierke</id>
53-
<name>Oliver Gierke</name>
54-
<email>ogierke(at)pivotal.io</email>
55-
<organization>Pivotal Software, Inc.</organization>
56-
<organizationUrl>https://pivotal.io</organizationUrl>
57-
<roles>
58-
<role>Project Contributor</role>
59-
</roles>
60-
<timezone>+1</timezone>
61-
</developer>
62-
<developer>
63-
<id>mpaluch</id>
64-
<name>Mark Paluch</name>
65-
<email>mpaluch(at)pivotal.io</email>
66-
<organization>Pivotal Software, Inc.</organization>
67-
<organizationUrl>https://pivotal.io</organizationUrl>
68-
<roles>
69-
<role>Project Contributor</role>
70-
</roles>
71-
<timezone>+1</timezone>
72-
</developer>
73-
</developers>
74-
7528
<dependencyManagement>
7629
<dependencies>
7730
<dependency>
@@ -269,6 +222,14 @@
269222
<scope>test</scope>
270223
</dependency>
271224

225+
<dependency>
226+
<groupId>org.liquibase</groupId>
227+
<artifactId>liquibase-core</artifactId>
228+
<version>${liquibase.version}</version>
229+
<scope>compile</scope>
230+
<optional>true</optional>
231+
</dependency>
232+
272233
</dependencies>
273234

274235

spring-data-r2dbc/pom.xml

-25
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,6 @@
3939

4040
<inceptionYear>2018</inceptionYear>
4141

42-
<developers>
43-
<developer>
44-
<id>mpaluch</id>
45-
<name>Mark Paluch</name>
46-
<email>mpaluch(at)pivotal.io</email>
47-
<organization>Pivotal Software, Inc.</organization>
48-
<organizationUrl>https://pivotal.io</organizationUrl>
49-
<roles>
50-
<role>Project Lead</role>
51-
</roles>
52-
<timezone>+1</timezone>
53-
</developer>
54-
<developer>
55-
<id>ogierke</id>
56-
<name>Oliver Gierke</name>
57-
<email>ogierke(at)pivotal.io</email>
58-
<organization>Pivotal Software, Inc.</organization>
59-
<organizationUrl>https://pivotal.io</organizationUrl>
60-
<roles>
61-
<role>Project Lead</role>
62-
</roles>
63-
<timezone>+1</timezone>
64-
</developer>
65-
</developers>
66-
6742
<dependencyManagement>
6843
<dependencies>
6944
<dependency>

spring-data-relational/pom.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
<artifactId>spring-core</artifactId>
5151
</dependency>
5252

53+
<dependency>
54+
<groupId>org.liquibase</groupId>
55+
<artifactId>liquibase-core</artifactId>
56+
<version>${liquibase.version}</version>
57+
<scope>compile</scope>
58+
<optional>true</optional>
59+
</dependency>
60+
5361
<dependency>
5462
<groupId>com.google.code.findbugs</groupId>
5563
<artifactId>jsr305</artifactId>
@@ -97,6 +105,6 @@
97105
<scope>test</scope>
98106
</dependency>
99107

100-
</dependencies>
108+
</dependencies>
101109

102110
</project>

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalMappingContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,5 @@ protected void applyDefaults(BasicRelationalPersistentProperty persistentPropert
128128
persistentProperty.setForceQuote(isForceQuote());
129129
persistentProperty.setExpressionEvaluator(this.expressionEvaluator);
130130
}
131-
131+
132132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.relational.core.mapping.schemasqlgeneration;
17+
18+
import org.springframework.data.relational.core.sql.SqlIdentifier;
19+
20+
import java.util.Objects;
21+
22+
23+
/**
24+
* Models a Column for generating SQL for Schema generation.
25+
*
26+
* @author Kurt Niemi
27+
* @since 3.2
28+
*/
29+
public record ColumnModel(String name, String type, boolean nullable, boolean identityColumn) {
30+
31+
public ColumnModel(String name, String type) {
32+
this(name, type, false, false);
33+
}
34+
35+
@Override
36+
public boolean equals(Object o) {
37+
if (this == o) return true;
38+
if (o == null || getClass() != o.getClass()) return false;
39+
ColumnModel that = (ColumnModel) o;
40+
return Objects.equals(name, that.name);
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
return Objects.hash(name);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.relational.core.mapping.schemasqlgeneration;
17+
18+
/**
19+
* Interface for mapping a Java type to a Database type.
20+
*
21+
* To customize the mapping an instance of a class implementing {@link DatabaseTypeMapping} interface
22+
* can be set on the {@link SchemaModel} class.
23+
*
24+
* @author Kurt Niemi
25+
* @since 3.2
26+
*/
27+
public interface DatabaseTypeMapping {
28+
public String databaseTypeFromClass(Class<?> type);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.relational.core.mapping.schemasqlgeneration;
17+
18+
import java.util.HashMap;
19+
20+
21+
/**
22+
* Class that provides a default implementation of mapping Java type to a Database type.
23+
*
24+
* To customize the mapping an instance of a class implementing {@link DatabaseTypeMapping} interface
25+
* can be set on the {@link SchemaModel} class
26+
*
27+
* @author Kurt Niemi
28+
* @since 3.2
29+
*/
30+
public class DefaultDatabaseTypeMapping implements DatabaseTypeMapping {
31+
32+
final HashMap<Class<?>,String> mapClassToDatabaseType = new HashMap<Class<?>,String>();
33+
34+
public DefaultDatabaseTypeMapping() {
35+
36+
mapClassToDatabaseType.put(String.class, "VARCHAR(255 BYTE)");
37+
mapClassToDatabaseType.put(Boolean.class, "TINYINT");
38+
mapClassToDatabaseType.put(Double.class, "DOUBLE");
39+
mapClassToDatabaseType.put(Float.class, "FLOAT");
40+
mapClassToDatabaseType.put(Integer.class, "INT");
41+
mapClassToDatabaseType.put(Long.class, "BIGINT");
42+
}
43+
public String databaseTypeFromClass(Class<?> type) {
44+
45+
return mapClassToDatabaseType.get(type);
46+
}
47+
}

0 commit comments

Comments
 (0)