mirror of
https://github.com/google/nomulus
synced 2026-09-20 15:04:24 +00:00
Prevent test from changing golden schema (#337)
When we add the extra test entity to the current JpaTransactionManagerRule by calling jpaRule.withEntityClass(TestEntity.class) and jpaRule.withProperty(Environment.HBM2DDL_AUTO, "update"), the rule would override the golden database scheme with the schema derived from the current entity class(This is an expected behavior by enabling HBM2DDL_AUTO). This behavior prevents us from detecting breaking changes in ORM classes. This PR fixed the issue by adding HibernateSchemaExporter to export the DDL script for given extra entity class, and make JpaTransactionManagerRule execute the DDL script to create the corresponding table for the extra entity when initializing the database. This PR also simplified the code when adding extra entity class for testing. For now, you don't need to(and shouldn't) call jpaRule.withProperty(Environment.HBM2DDL_AUTO, "update").
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// Copyright 2019 The Nomulus Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google.registry.persistence;
|
||||
|
||||
import static com.google.common.collect.ImmutableList.toImmutableList;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
import java.io.File;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Stream;
|
||||
import javax.persistence.AttributeConverter;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
|
||||
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
|
||||
/** Utility class to export DDL script for given {@link javax.persistence.Entity} classes. */
|
||||
public class HibernateSchemaExporter {
|
||||
private final String jdbcUrl;
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
private HibernateSchemaExporter(String jdbcUrl, String username, String password) {
|
||||
this.jdbcUrl = jdbcUrl;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/** Constructs a {@link HibernateSchemaExporter} instance. */
|
||||
public static HibernateSchemaExporter create(String jdbcUrl, String username, String password) {
|
||||
return new HibernateSchemaExporter(jdbcUrl, username, password);
|
||||
}
|
||||
|
||||
/** Exports DDL script to the {@code outputFile} for the given {@code entityClasses}. */
|
||||
public void export(ImmutableList<Class> entityClasses, File outputFile) {
|
||||
// Configure Hibernate settings.
|
||||
Map<String, String> settings = Maps.newHashMap();
|
||||
settings.put(Environment.DIALECT, NomulusPostgreSQLDialect.class.getName());
|
||||
settings.put(Environment.URL, jdbcUrl);
|
||||
settings.put(Environment.USER, username);
|
||||
settings.put(Environment.PASS, password);
|
||||
settings.put(Environment.HBM2DDL_AUTO, "none");
|
||||
settings.put(Environment.SHOW_SQL, "true");
|
||||
settings.put(
|
||||
Environment.PHYSICAL_NAMING_STRATEGY, NomulusNamingStrategy.class.getCanonicalName());
|
||||
|
||||
MetadataSources metadata =
|
||||
new MetadataSources(new StandardServiceRegistryBuilder().applySettings(settings).build());
|
||||
|
||||
// Note that we need to also add all converters to the Hibernate context because
|
||||
// the entity class may use the customized type.
|
||||
Stream.concat(entityClasses.stream(), findAllConverters().stream())
|
||||
.forEach(metadata::addAnnotatedClass);
|
||||
|
||||
SchemaExport export = new SchemaExport();
|
||||
export.setHaltOnError(true);
|
||||
export.setFormat(true);
|
||||
export.setDelimiter(";");
|
||||
export.setOutputFile(outputFile.getAbsolutePath());
|
||||
export.createOnly(EnumSet.of(TargetType.SCRIPT), metadata.buildMetadata());
|
||||
}
|
||||
|
||||
private ImmutableList<Class> findAllConverters() {
|
||||
ParsedPersistenceXmlDescriptor descriptor =
|
||||
PersistenceXmlParser.locatePersistenceUnits(new Properties()).stream()
|
||||
.filter(unit -> PersistenceModule.PERSISTENCE_UNIT_NAME.equals(unit.getName()))
|
||||
.findFirst()
|
||||
.orElseThrow(
|
||||
() ->
|
||||
new IllegalArgumentException(
|
||||
String.format(
|
||||
"Could not find persistence unit with name %s",
|
||||
PersistenceModule.PERSISTENCE_UNIT_NAME)));
|
||||
return descriptor.getManagedClassNames().stream()
|
||||
.map(
|
||||
className -> {
|
||||
try {
|
||||
return Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.filter(AttributeConverter.class::isAssignableFrom)
|
||||
.collect(toImmutableList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user