use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class SequenceStyleConfigUnitTest method testDefaultedTableBackedConfiguration.
/**
* Test all params defaulted with a dialect which does not support sequences
*/
@Test
public void testDefaultedTableBackedConfiguration() {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, TableDialect.class.getName()).build();
try {
Properties props = buildGeneratorPropertiesBase(serviceRegistry);
SequenceStyleGenerator generator = new SequenceStyleGenerator();
generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
assertClassAssignability(NoopOptimizer.class, generator.getOptimizer().getClass());
assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class SequenceStyleConfigUnitTest method testForceTableUse.
/**
* Test forcing of table as backing structure with dialect supporting sequences
*/
@Test
public void testForceTableUse() {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, SequenceDialect.class.getName()).build();
try {
Properties props = buildGeneratorPropertiesBase(serviceRegistry);
props.setProperty(SequenceStyleGenerator.FORCE_TBL_PARAM, "true");
SequenceStyleGenerator generator = new SequenceStyleGenerator();
generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
assertClassAssignability(NoopOptimizer.class, generator.getOptimizer().getClass());
assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class SequenceStyleConfigUnitTest method testExplicitOptimizerWithExplicitIncrementSize.
/**
* Test explicitly specifying both optimizer and increment
*/
@Test
public void testExplicitOptimizerWithExplicitIncrementSize() {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, SequenceDialect.class.getName()).build();
// optimizer=none w/ increment > 1 => should honor optimizer
try {
Properties props = buildGeneratorPropertiesBase(serviceRegistry);
props.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.NONE.getExternalName());
props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
SequenceStyleGenerator generator = new SequenceStyleGenerator();
generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
assertClassAssignability(NoopOptimizer.class, generator.getOptimizer().getClass());
assertEquals(1, generator.getOptimizer().getIncrementSize());
assertEquals(1, generator.getDatabaseStructure().getIncrementSize());
// optimizer=hilo w/ increment > 1 => hilo
props = buildGeneratorPropertiesBase(serviceRegistry);
props.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.HILO.getExternalName());
props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
generator = new SequenceStyleGenerator();
generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
assertClassAssignability(SequenceStructure.class, generator.getDatabaseStructure().getClass());
assertClassAssignability(HiLoOptimizer.class, generator.getOptimizer().getClass());
assertEquals(20, generator.getOptimizer().getIncrementSize());
assertEquals(20, generator.getDatabaseStructure().getIncrementSize());
// optimizer=pooled w/ increment > 1 => hilo
props = buildGeneratorPropertiesBase(serviceRegistry);
props.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
props.setProperty(SequenceStyleGenerator.INCREMENT_PARAM, "20");
generator = new SequenceStyleGenerator();
generator.configure(StandardBasicTypes.LONG, props, serviceRegistry);
generator.registerExportables(new Database(new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry)));
// because the dialect reports to not support pooled seqyences, the expectation is that we will
// use a table for the backing structure...
assertClassAssignability(TableStructure.class, generator.getDatabaseStructure().getClass());
assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
assertEquals(20, generator.getOptimizer().getIncrementSize());
assertEquals(20, generator.getDatabaseStructure().getIncrementSize());
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class AbstractSchemaMigrator method performMigration.
private void performMigration(Metadata metadata, DatabaseInformation existingDatabase, ExecutionOptions options, Dialect dialect, GenerationTarget... targets) {
final boolean format = Helper.interpretFormattingEnabled(options.getConfigurationValues());
final Formatter formatter = format ? FormatStyle.DDL.getFormatter() : FormatStyle.NONE.getFormatter();
final Set<String> exportIdentifiers = new HashSet<String>(50);
final Database database = metadata.getDatabase();
// Drop all AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, dialect.getAuxiliaryDatabaseObjectExporter().getSqlDropStrings(auxiliaryDatabaseObject, metadata), formatter, options, targets);
}
}
// Create beforeQuery-table AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (!auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
}
}
boolean tryToCreateCatalogs = false;
boolean tryToCreateSchemas = false;
if (options.shouldManageNamespaces()) {
if (dialect.canCreateSchema()) {
tryToCreateSchemas = true;
}
if (dialect.canCreateCatalog()) {
tryToCreateCatalogs = true;
}
}
final Map<Namespace, NameSpaceTablesInformation> tablesInformation = new HashMap<>();
Set<Identifier> exportedCatalogs = new HashSet<>();
for (Namespace namespace : database.getNamespaces()) {
final NameSpaceTablesInformation nameSpaceTablesInformation = performTablesMigration(metadata, existingDatabase, options, dialect, formatter, exportIdentifiers, tryToCreateCatalogs, tryToCreateSchemas, exportedCatalogs, namespace, targets);
tablesInformation.put(namespace, nameSpaceTablesInformation);
if (schemaFilter.includeNamespace(namespace)) {
for (Sequence sequence : namespace.getSequences()) {
checkExportIdentifier(sequence, exportIdentifiers);
final SequenceInformation sequenceInformation = existingDatabase.getSequenceInformation(sequence.getName());
if (sequenceInformation == null) {
applySqlStrings(false, dialect.getSequenceExporter().getSqlCreateStrings(sequence, metadata), formatter, options, targets);
}
}
}
}
//NOTE : Foreign keys must be created *afterQuery* all tables of all namespaces for cross namespace fks. see HHH-10420
for (Namespace namespace : database.getNamespaces()) {
if (schemaFilter.includeNamespace(namespace)) {
final NameSpaceTablesInformation nameSpaceTablesInformation = tablesInformation.get(namespace);
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table)) {
final TableInformation tableInformation = nameSpaceTablesInformation.getTableInformation(table);
if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
applyForeignKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
}
}
// Create afterQuery-table AuxiliaryDatabaseObjects
for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects()) {
if (auxiliaryDatabaseObject.beforeTablesOnCreation() && auxiliaryDatabaseObject.appliesToDialect(dialect)) {
applySqlStrings(true, auxiliaryDatabaseObject.sqlCreateStrings(dialect), formatter, options, targets);
}
}
}
use of org.hibernate.boot.model.relational.Database in project hibernate-orm by hibernate.
the class AbstractSchemaMigrator method migrateTable.
protected void migrateTable(Table table, TableInformation tableInformation, Dialect dialect, Metadata metadata, Formatter formatter, ExecutionOptions options, GenerationTarget... targets) {
final Database database = metadata.getDatabase();
//noinspection unchecked
applySqlStrings(false, table.sqlAlterStrings(dialect, metadata, tableInformation, getDefaultCatalogName(database, dialect), getDefaultSchemaName(database, dialect)), formatter, options, targets);
}
Aggregations