|
| 1 | +package io.roach.data.jpa; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.EnumSet; |
| 9 | + |
| 10 | +import org.hibernate.boot.Metadata; |
| 11 | +import org.hibernate.boot.MetadataBuilder; |
| 12 | +import org.hibernate.boot.MetadataSources; |
| 13 | +import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; |
| 14 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 15 | +import org.hibernate.dialect.CockroachDB201Dialect; |
| 16 | +import org.hibernate.dialect.PostgreSQL10Dialect; |
| 17 | +import org.hibernate.service.ServiceRegistry; |
| 18 | +import org.hibernate.tool.hbm2ddl.SchemaExport; |
| 19 | +import org.hibernate.tool.schema.TargetType; |
| 20 | + |
| 21 | +import io.roach.data.jpa.domain.Customer; |
| 22 | +import io.roach.data.jpa.domain.Order; |
| 23 | +import io.roach.data.jpa.domain.Product; |
| 24 | + |
| 25 | +/** |
| 26 | + * Hibernate DDL exporter from JPA annotations for verification of mapping. |
| 27 | + */ |
| 28 | +public class SchemaExporter { |
| 29 | + public static final Class[] ENTITY_TYPES = { |
| 30 | + Customer.class, |
| 31 | + Order.class, |
| 32 | + Product.class |
| 33 | + }; |
| 34 | + |
| 35 | + public static void _main(String[] args) throws IOException { |
| 36 | + boolean drop = true; |
| 37 | + Path outFile = null; |
| 38 | + String delimiter = ";"; |
| 39 | + Class dialect = CockroachDB201Dialect.class; |
| 40 | + |
| 41 | + for (String arg : args) { |
| 42 | + if (arg.startsWith("--")) { |
| 43 | + if ("--drop".equals(arg)) { |
| 44 | + drop = true; |
| 45 | + } else if (arg.startsWith("--output=")) { |
| 46 | + outFile = Paths.get(arg.substring(9)); |
| 47 | + } else if (arg.startsWith("--delimiter=")) { |
| 48 | + delimiter = arg.substring(12); |
| 49 | + } else if (arg.equalsIgnoreCase("--psql")) { |
| 50 | + dialect = PostgreSQL10Dialect.class; |
| 51 | + } |
| 52 | + } else { |
| 53 | + try { |
| 54 | + dialect = Class.forName(arg); |
| 55 | + } catch (ClassNotFoundException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (outFile == null) { |
| 62 | + outFile = Files.createTempFile("shop_n_drop", "schema"); |
| 63 | + } |
| 64 | + |
| 65 | + ServiceRegistry standardRegistry = |
| 66 | + new StandardServiceRegistryBuilder() |
| 67 | + .applySetting("hibernate.dialect", dialect.getName()) |
| 68 | + .applySetting("hibernate.physical_naming_strategy", |
| 69 | + "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy") |
| 70 | + .build(); |
| 71 | + |
| 72 | + MetadataSources metadataSources = new MetadataSources(standardRegistry); |
| 73 | + |
| 74 | + Arrays.stream(ENTITY_TYPES).sequential().forEach(clazz -> metadataSources.addAnnotatedClass(clazz)); |
| 75 | + |
| 76 | + MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(); |
| 77 | + metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE); |
| 78 | + Metadata metadata = metadataBuilder.build(); |
| 79 | + |
| 80 | + // Exporter doesnt seem to export if file exists |
| 81 | + if (outFile.toFile().isFile() && !outFile.toFile().delete()) { |
| 82 | + System.err.println("WARN: Unable to delete file " + outFile); |
| 83 | + } |
| 84 | + |
| 85 | + SchemaExport schemaExport = new SchemaExport() |
| 86 | + .setOutputFile(outFile.toString()) |
| 87 | + .setFormat(true) |
| 88 | + .setHaltOnError(true) |
| 89 | + .setDelimiter(delimiter); |
| 90 | + if (drop) { |
| 91 | + schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata); |
| 92 | + } else { |
| 93 | + schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata); |
| 94 | + } |
| 95 | + |
| 96 | + Files.copy(outFile, System.out); |
| 97 | + } |
| 98 | + |
| 99 | + private SchemaExporter() { |
| 100 | + } |
| 101 | +} |
0 commit comments