-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugins: adds a maven plugin to generate hibernate schemas (#19)
This PR adds a maven plugin that users can run to generate a hibernate schema in a similar way to how the gradle plugin works. To generate a schema using maven (after adding the plugin): ``` mvn -q hibernate-provider:schema ``` Installing the plugin is very standard to the maven eco-system, but it does not belong in any natural phase, so it should go to the `pluginManagement` section: ``` <build> <pluginManagement> <plugins> <plugin> <groupId>io.atlasgo</groupId> <artifactId>hibernate-provider-maven-plugin</artifactId> <version>0.1</version> <executions> <execution> <phase>test</phase> <goals> <goal>schema</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> ``` An example e2e run can be found here - https://gh.atlasgo.cloud/explore/ab450014
- Loading branch information
Showing
32 changed files
with
691 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.local-plugin-repository | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
data "external_schema" "hibernate" { | ||
program = [ | ||
"mvn", | ||
"-q", | ||
"compile", | ||
"hibernate-provider:schema" | ||
] | ||
} | ||
|
||
data "external_schema" "hibernate_postgresql" { | ||
program = [ | ||
"mvn", | ||
"-q", | ||
"compile", | ||
"hibernate-provider:schema", | ||
"-Dproperties=postgresql.properties" | ||
] | ||
} | ||
|
||
env "hibernate" { | ||
src = data.external_schema.hibernate.url | ||
dev = "docker://mysql/8/dev" | ||
migration { | ||
dir = "file://migrations" | ||
} | ||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
} | ||
|
||
env "hibernate_postgresql" { | ||
src = data.external_schema.hibernate_postgresql.url | ||
dev = "docker://postgres/15/dev" | ||
migration { | ||
dir = "file://migrations_postgresql" | ||
} | ||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
examples/maven_project_example/allowed_types/migrations/20231205144835.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-- Create "Location" table | ||
CREATE TABLE `Location` ( | ||
`id` bigint NOT NULL AUTO_INCREMENT, | ||
`title` varchar(255) NULL, | ||
PRIMARY KEY (`id`) | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; | ||
-- Create "Department" table | ||
CREATE TABLE `Department` ( | ||
`id` bigint NOT NULL AUTO_INCREMENT, | ||
`name` varchar(255) NULL, | ||
PRIMARY KEY (`id`) | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; | ||
-- Create "Employee" table | ||
CREATE TABLE `Employee` ( | ||
`id` bigint NOT NULL AUTO_INCREMENT, | ||
`name` varchar(255) NULL, | ||
`department_id` bigint NULL, | ||
PRIMARY KEY (`id`), | ||
INDEX `FK14tijxqry9ml17nk86sqfp561` (`department_id`), | ||
CONSTRAINT `FK14tijxqry9ml17nk86sqfp561` FOREIGN KEY (`department_id`) REFERENCES `Department` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION | ||
) CHARSET utf8mb4 COLLATE utf8mb4_0900_ai_ci; |
2 changes: 2 additions & 0 deletions
2
examples/maven_project_example/allowed_types/migrations/atlas.sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
h1:Dy0V88s6slIfSnpV4kis08iLeJAYX1uFL1V5kHXZKHQ= | ||
20231205144835.sql h1:nWzM/ZCeVTFmjjC8/0RmmpcD0KndYZjOKWm9BrqyhGs= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>maven_project_example</artifactId> | ||
<groupId>org.example</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>com.example.allowed</groupId> | ||
<artifactId>allowed_types</artifactId> | ||
<version>1.0</version> | ||
|
||
<name>allowed_types</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
<build> | ||
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> | ||
</build> | ||
</project> |
28 changes: 28 additions & 0 deletions
28
...ven_project_example/allowed_types/src/main/java/com/example/H2ServiceRegistryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example; | ||
|
||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.service.ServiceRegistry; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.function.Function; | ||
|
||
public class H2ServiceRegistryBuilder implements Function<Properties, ServiceRegistry> { | ||
public H2ServiceRegistryBuilder() { | ||
|
||
} | ||
|
||
@Override | ||
public ServiceRegistry apply(Properties properties) { | ||
return new StandardServiceRegistryBuilder() | ||
.applySettings(Map.of( | ||
"hibernate.connection.url","jdbc:h2:mem:testdb", | ||
AvailableSettings.SCHEMA_MANAGEMENT_TOOL, properties.get(AvailableSettings.SCHEMA_MANAGEMENT_TOOL), | ||
"hibernate.temp.use_jdbc_metadata_defaults", true, | ||
"jakarta.persistence.database-product-name", "H2", | ||
"jakarta.persistence.database-major-version", "", | ||
"hibernate.connection.provider_class", "")) | ||
.build(); | ||
} | ||
} |
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
examples/maven_project_example/allowed_types/src/main/java/com/example/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.example; | ||
|
||
import com.example.minimodel.Location; | ||
import com.example.model.Department; | ||
import com.example.model.Employee; | ||
import jakarta.persistence.Query; | ||
import org.hibernate.Session; | ||
import org.hibernate.Transaction; | ||
|
||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println("Hello world"); | ||
|
||
Session session = HibernateUtil.getSessionFactory().openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
Location location = new Location(); | ||
location.setTitle("Darkness"); | ||
session.persist(location); | ||
transaction.commit(); | ||
Query q = session.createQuery("From Location ", Location.class); | ||
List<Location> resultList = q.getResultList(); | ||
System.out.println("num of locations:" + resultList.size()); | ||
for (Location next : resultList) { | ||
System.out.println("next location: (" + next.id + ") - " + next.title); | ||
} | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
examples/maven_project_example/allowed_types/src/main/java/com/example/MetadataBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example; | ||
|
||
import com.example.minimodel.Location; | ||
import org.hibernate.boot.Metadata; | ||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.service.ServiceRegistry; | ||
|
||
import java.util.function.Function; | ||
|
||
public class MetadataBuilder implements Function<ServiceRegistry, Metadata> { | ||
@Override | ||
public Metadata apply(ServiceRegistry registry) { | ||
return new MetadataSources(registry) | ||
.addAnnotatedClasses(Location.class) | ||
.buildMetadata(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...les/maven_project_example/allowed_types/src/main/java/com/example/minimodel/Location.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.minimodel; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
|
||
@Entity | ||
public class Location { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
public Long id; | ||
|
||
public String title = "hello"; | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
examples/maven_project_example/allowed_types/src/main/resources/hibernate.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jakarta.persistence.database-product-name=MySQL | ||
jakarta.persistence.database-major-version=8 |
1 change: 1 addition & 0 deletions
1
examples/maven_project_example/allowed_types/src/main/resources/postgresql.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
jakarta.persistence.database-product-name=PostgreSQL |
4 changes: 4 additions & 0 deletions
4
examples/maven_project_example/allowed_types/src/test/expected_default_schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
create table Department (id bigint not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB; | ||
create table Employee (id bigint not null auto_increment, name varchar(255), department_id bigint, primary key (id)) engine=InnoDB; | ||
create table Location (id bigint not null auto_increment, title varchar(255), primary key (id)) engine=InnoDB; | ||
alter table Employee add constraint FK14tijxqry9ml17nk86sqfp561 foreign key (department_id) references Department (id); |
3 changes: 3 additions & 0 deletions
3
examples/maven_project_example/allowed_types/src/test/expected_employee_output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
create table Department (id bigint not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB; | ||
create table Employee (id bigint not null auto_increment, name varchar(255), department_id bigint, primary key (id)) engine=InnoDB; | ||
alter table Employee add constraint FK14tijxqry9ml17nk86sqfp561 foreign key (department_id) references Department (id); |
4 changes: 4 additions & 0 deletions
4
examples/maven_project_example/allowed_types/src/test/expected_h2_output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
create table Department (id bigint generated by default as identity, name varchar(255), primary key (id)); | ||
create table Employee (id bigint generated by default as identity, name varchar(255), department_id bigint, primary key (id)); | ||
create table Location (id bigint generated by default as identity, title varchar(255), primary key (id)); | ||
alter table Employee add constraint FK14tijxqry9ml17nk86sqfp561 foreign key (department_id) references Department; |
1 change: 1 addition & 0 deletions
1
examples/maven_project_example/allowed_types/src/test/expected_mini_output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
create table Location (id bigint not null auto_increment, title varchar(255), primary key (id)) engine=InnoDB; |
4 changes: 4 additions & 0 deletions
4
examples/maven_project_example/allowed_types/src/test/expected_postgresql_output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
create table Department (id bigint not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB; | ||
create table Employee (id bigint not null auto_increment, name varchar(255), department_id bigint, primary key (id)) engine=InnoDB; | ||
create table Location (id bigint not null auto_increment, title varchar(255), primary key (id)) engine=InnoDB; | ||
alter table Employee add constraint FK14tijxqry9ml17nk86sqfp561 foreign key (department_id) references Department (id); |
Empty file.
Oops, something went wrong.