From 028a0dbe9f88927c16d13af07b3e0549a6a643a1 Mon Sep 17 00:00:00 2001
From: ovidiu-mihai98 <138307181+ovidiumihaitacu@users.noreply.github.com>
Date: Sun, 3 Mar 2024 20:25:07 +0200
Subject: [PATCH 1/3] [BAEL-7421] - Creating Postgres Schema with Liquibase
---
persistence-modules/liquibase/pom.xml | 54 ++++++++++++++++---
.../LiquibaseCreateSchemaApplication.java | 12 +++++
.../configuration/SchemaInitializer.java | 31 +++++++++++
.../com/baeldung/liquibase/entity/User.java | 30 +++++++++++
.../src/main/resources/application.properties | 5 ++
.../main/resources/liquibase/changelog.xml | 23 ++++++++
.../resources/preliquibase/postgresql.sql | 1 +
7 files changed, 150 insertions(+), 6 deletions(-)
create mode 100644 persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/LiquibaseCreateSchemaApplication.java
create mode 100644 persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/configuration/SchemaInitializer.java
create mode 100644 persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/entity/User.java
create mode 100644 persistence-modules/liquibase/src/main/resources/application.properties
create mode 100644 persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml
create mode 100644 persistence-modules/liquibase/src/main/resources/preliquibase/postgresql.sql
diff --git a/persistence-modules/liquibase/pom.xml b/persistence-modules/liquibase/pom.xml
index a6e0ed85e9..9ed1791e81 100644
--- a/persistence-modules/liquibase/pom.xml
+++ b/persistence-modules/liquibase/pom.xml
@@ -4,13 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
liquibase
+ com.baeldung
liquibase
-
-
- com.baeldung
- persistence-modules
- 1.0.0-SNAPSHOT
-
+ 0.0.1-SNAPSHOT
@@ -18,6 +14,38 @@
mysql-connector-j
${mysql-connector-java.version}
+
+ org.springframework.boot
+ spring-boot-starter-web
+ 3.2.3
+
+
+ org.slf4j
+ slf4j-log4j12
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+ 3.2.3
+
+
+ org.liquibase
+ liquibase-core
+ 4.24.0
+
+
+ org.postgresql
+ postgresql
+ 42.6.1
+ runtime
+
+
+ net.lbruun.springboot
+ preliquibase-spring-boot-starter
+ 1.5.0
+
@@ -32,12 +60,26 @@
liquibase/db-changelog.xml
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ ${maven.compiler.release}
+ --enable-preview
+ ${maven.compiler.source.version}
+ ${maven.compiler.target.version}
+
+
8.2.0
4.25.0
+ 17
+ 17
+ 17
+ 17
\ No newline at end of file
diff --git a/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/LiquibaseCreateSchemaApplication.java b/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/LiquibaseCreateSchemaApplication.java
new file mode 100644
index 0000000000..f501cbf82a
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/LiquibaseCreateSchemaApplication.java
@@ -0,0 +1,12 @@
+package com.baeldung.liquibase;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class LiquibaseCreateSchemaApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(LiquibaseCreateSchemaApplication.class, args);
+ }
+}
diff --git a/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/configuration/SchemaInitializer.java b/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/configuration/SchemaInitializer.java
new file mode 100644
index 0000000000..cf122070c2
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/configuration/SchemaInitializer.java
@@ -0,0 +1,31 @@
+package com.baeldung.liquibase.configuration;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class SchemaInitializer implements BeanPostProcessor {
+
+ @Value("${spring.liquibase.default-schema}")
+ private String schemaName;
+
+ @Override
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+ if (bean instanceof DataSource) {
+ DataSource dataSource = (DataSource) bean;
+ try {
+ Connection conn = dataSource.getConnection();
+ Statement statement = conn.createStatement();
+ statement.execute(String.format("CREATE SCHEMA IF NOT EXISTS %s", schemaName));
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return bean;
+ }
+}
\ No newline at end of file
diff --git a/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/entity/User.java b/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/entity/User.java
new file mode 100644
index 0000000000..9beedfb55f
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/java/com/baeldung/liquibase/entity/User.java
@@ -0,0 +1,30 @@
+package com.baeldung.liquibase.entity;
+
+import jakarta.persistence.Entity;
+import jakarta.persistence.Id;
+import jakarta.persistence.Table;
+
+@Entity
+@Table(name = "users")
+public class User {
+
+ @Id
+ private Long id;
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
diff --git a/persistence-modules/liquibase/src/main/resources/application.properties b/persistence-modules/liquibase/src/main/resources/application.properties
new file mode 100644
index 0000000000..77740c8e2b
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/resources/application.properties
@@ -0,0 +1,5 @@
+#spring.datasource.url=jdbc:postgresql://some-postgresql-host/some-postgresql-database
+#spring.datasource.username=your-postgresql-username
+#spring.datasource.password=your-postgresql-password
+#spring.liquibase.change-log=classpath:liquibase/changelog.xml
+#spring.liquibase.default-schema=user_management
\ No newline at end of file
diff --git a/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml b/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml
new file mode 100644
index 0000000000..cec8ae138f
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/persistence-modules/liquibase/src/main/resources/preliquibase/postgresql.sql b/persistence-modules/liquibase/src/main/resources/preliquibase/postgresql.sql
new file mode 100644
index 0000000000..03aad1cbd4
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/resources/preliquibase/postgresql.sql
@@ -0,0 +1 @@
+CREATE SCHEMA IF NOT EXISTS user_management;
\ No newline at end of file
From 09cc4a18ca94b0d9f3c964f166390c2cea5dad24 Mon Sep 17 00:00:00 2001
From: ovidiu-mihai98 <138307181+ovidiumihaitacu@users.noreply.github.com>
Date: Thu, 14 Mar 2024 21:22:33 +0200
Subject: [PATCH 2/3] Updated versions
---
persistence-modules/liquibase/pom.xml | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/persistence-modules/liquibase/pom.xml b/persistence-modules/liquibase/pom.xml
index 9ed1791e81..5caf7047ac 100644
--- a/persistence-modules/liquibase/pom.xml
+++ b/persistence-modules/liquibase/pom.xml
@@ -17,7 +17,7 @@
org.springframework.boot
spring-boot-starter-web
- 3.2.3
+ ${spring-boot.version}
org.slf4j
@@ -28,23 +28,23 @@
org.springframework.boot
spring-boot-starter-data-jpa
- 3.2.3
+ ${spring-boot.version}
org.liquibase
liquibase-core
- 4.24.0
+ ${liquibase.version}
org.postgresql
postgresql
- 42.6.1
+ ${postgresql.version}
runtime
net.lbruun.springboot
preliquibase-spring-boot-starter
- 1.5.0
+ ${preliquibase.version}
@@ -80,6 +80,10 @@
17
17
17
+ 3.2.3
+ 4.26.0
+ 42.7.2
+ 1.5.0
\ No newline at end of file
From 8f2b321720ef1a9147a258e156046064c5dd06ca Mon Sep 17 00:00:00 2001
From: ovidiu-mihai98 <138307181+ovidiumihaitacu@users.noreply.github.com>
Date: Thu, 21 Mar 2024 18:36:10 +0200
Subject: [PATCH 3/3] Split Changelogs
---
.../liquibase/changelog-customChangeset.xml | 22 +++++++++++++++++++
.../main/resources/liquibase/changelog.xml | 6 -----
2 files changed, 22 insertions(+), 6 deletions(-)
create mode 100644 persistence-modules/liquibase/src/main/resources/liquibase/changelog-customChangeset.xml
diff --git a/persistence-modules/liquibase/src/main/resources/liquibase/changelog-customChangeset.xml b/persistence-modules/liquibase/src/main/resources/liquibase/changelog-customChangeset.xml
new file mode 100644
index 0000000000..e33bc1610a
--- /dev/null
+++ b/persistence-modules/liquibase/src/main/resources/liquibase/changelog-customChangeset.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+ CREATE SCHEMA IF NOT EXISTS user_management;
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml b/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml
index cec8ae138f..6ae5187a92 100644
--- a/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml
+++ b/persistence-modules/liquibase/src/main/resources/liquibase/changelog.xml
@@ -4,13 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
-
-
-
-
-
-