HHH-12939 - Database name not quoted at schema update
(cherry picked from commit 6e9c1893a1
)
This commit is contained in:
parent
e5873b9fe4
commit
c0f0a731d4
|
@ -111,7 +111,7 @@ ext {
|
|||
mariadb: 'org.mariadb.jdbc:mariadb-java-client:2.2.3',
|
||||
|
||||
oracle: 'com.oracle.jdbc:ojdbc8:12.2.0.1',
|
||||
mssql: 'com.microsoft.sqlserver:mssql-jdbc:6.4.0.jre8',
|
||||
mssql: 'com.microsoft.sqlserver:mssql-jdbc:7.0.0.jre8',
|
||||
db2: 'com.ibm.db2:db2jcc:10.5',
|
||||
hana: 'com.sap.cloud.db.jdbc:ngdbc:2.2.16', // for HANA 1 the minimum required client version is 1.120.20
|
||||
|
||||
|
|
|
@ -441,28 +441,16 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
Dialect dialect,
|
||||
Metadata metadata,
|
||||
TableInformation tableInfo,
|
||||
String defaultCatalog,
|
||||
String defaultSchema) throws HibernateException {
|
||||
Identifier defaultCatalog,
|
||||
Identifier defaultSchema) throws HibernateException {
|
||||
|
||||
final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
|
||||
|
||||
Identifier quotedCatalog = catalog != null && catalog.isQuoted() ?
|
||||
new Identifier( tableInfo.getName().getCatalogName().getText(), true ) :
|
||||
tableInfo.getName().getCatalogName();
|
||||
|
||||
Identifier quotedSchema = schema != null && schema.isQuoted() ?
|
||||
new Identifier( tableInfo.getName().getSchemaName().getText(), true ) :
|
||||
tableInfo.getName().getSchemaName();
|
||||
|
||||
Identifier quotedTable = name != null && name.isQuoted() ?
|
||||
new Identifier( tableInfo.getName().getObjectName().getText(), true ) :
|
||||
tableInfo.getName().getObjectName();
|
||||
|
||||
final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
|
||||
new QualifiedTableName(
|
||||
quotedCatalog,
|
||||
quotedSchema,
|
||||
quotedTable
|
||||
catalog != null ? catalog : defaultCatalog,
|
||||
schema != null ? schema : defaultSchema,
|
||||
name
|
||||
),
|
||||
dialect
|
||||
);
|
||||
|
|
|
@ -300,8 +300,8 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
|
|||
dialect,
|
||||
metadata,
|
||||
tableInformation,
|
||||
getDefaultCatalogName( database, dialect ),
|
||||
getDefaultSchemaName( database, dialect )
|
||||
database.getDefaultNamespace().getPhysicalName().getCatalog(),
|
||||
database.getDefaultNamespace().getPhysicalName().getSchema()
|
||||
),
|
||||
formatter,
|
||||
options,
|
||||
|
@ -581,14 +581,4 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getDefaultCatalogName(Database database, Dialect dialect) {
|
||||
final Identifier identifier = database.getDefaultNamespace().getPhysicalName().getCatalog();
|
||||
return identifier == null ? null : identifier.render( dialect );
|
||||
}
|
||||
|
||||
private String getDefaultSchemaName(Database database, Dialect dialect) {
|
||||
final Identifier identifier = database.getDefaultNamespace().getPhysicalName().getSchema();
|
||||
return identifier == null ? null : identifier.render( dialect );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.schemaupdate;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.dialect.MySQLDialect;
|
||||
import org.hibernate.dialect.PostgreSQL82Dialect;
|
||||
import org.hibernate.dialect.SQLServerDialect;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-12939")
|
||||
public abstract class AbstractAlterTableQuoteSchemaTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
private Dialect dialect = Dialect.getDialect();
|
||||
|
||||
protected String quote(String element) {
|
||||
return dialect.quote( "`" + element + "`" );
|
||||
}
|
||||
|
||||
protected String quote(String schema, String table) {
|
||||
return quote( schema ) + "." + quote( table );
|
||||
}
|
||||
|
||||
protected String regexpQuote(String element) {
|
||||
return dialect.quote( "`" + element + "`" )
|
||||
.replace( "-", "\\-" )
|
||||
.replace( "[", "\\[" )
|
||||
.replace( "]", "\\]" );
|
||||
}
|
||||
|
||||
protected String regexpQuote(String schema, String table) {
|
||||
return regexpQuote( schema ) + "\\." + regexpQuote( table );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,194 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.schemaupdate;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.EnumSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.boot.MetadataBuilder;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.dialect.MySQLDialect;
|
||||
import org.hibernate.dialect.PostgreSQL82Dialect;
|
||||
import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Guillaume Smet
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-12939")
|
||||
@RequiresDialectFeature(DialectChecks.SupportSchemaCreation.class)
|
||||
public class AlterTableQuoteDefaultSchemaTest extends AbstractAlterTableQuoteSchemaTest {
|
||||
|
||||
@Override
|
||||
protected void afterSessionFactoryBuilt() {
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "DROP TABLE " + quote( "default-schema", "my_entity" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "DROP SCHEMA " + quote( "default-schema" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "CREATE SCHEMA " + quote( "default-schema" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanupTest() {
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "DROP SCHEMA " + quote( "default-schema" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSchema() throws IOException {
|
||||
File output = File.createTempFile( "update_script", ".sql" );
|
||||
output.deleteOnExit();
|
||||
|
||||
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
|
||||
.applySetting( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString() )
|
||||
.build();
|
||||
|
||||
try {
|
||||
final MetadataSources metadataSources = new MetadataSources( ssr ) {
|
||||
@Override
|
||||
public MetadataBuilder getMetadataBuilder() {
|
||||
MetadataBuilder metadataBuilder = super.getMetadataBuilder();
|
||||
metadataBuilder.applyImplicitSchemaName( "default-schema" );
|
||||
return metadataBuilder;
|
||||
}
|
||||
};
|
||||
metadataSources.addAnnotatedClass( MyEntity.class );
|
||||
|
||||
final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
|
||||
metadata.validate();
|
||||
|
||||
new SchemaUpdate()
|
||||
.setHaltOnError( true )
|
||||
.setOutputFile( output.getAbsolutePath() )
|
||||
.setDelimiter( ";" )
|
||||
.setFormat( true )
|
||||
.execute( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
|
||||
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
|
||||
|
||||
Pattern fileContentPattern = Pattern
|
||||
.compile( "create table " + regexpQuote( "default-schema", "my_entity" ) );
|
||||
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
|
||||
assertThat( fileContentMatcher.find(), is( true ) );
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
|
||||
ssr = new StandardServiceRegistryBuilder()
|
||||
.applySetting( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString() )
|
||||
.build();
|
||||
try {
|
||||
final MetadataSources metadataSources = new MetadataSources( ssr ) {
|
||||
@Override
|
||||
public MetadataBuilder getMetadataBuilder() {
|
||||
MetadataBuilder metadataBuilder = super.getMetadataBuilder();
|
||||
metadataBuilder.applyImplicitSchemaName( "default-schema" );
|
||||
return metadataBuilder;
|
||||
}
|
||||
};
|
||||
metadataSources.addAnnotatedClass( MyEntityUpdated.class );
|
||||
|
||||
final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
|
||||
metadata.validate();
|
||||
|
||||
new SchemaUpdate()
|
||||
.setHaltOnError( true )
|
||||
.setOutputFile( output.getAbsolutePath() )
|
||||
.setDelimiter( ";" )
|
||||
.setFormat( true )
|
||||
.execute( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
|
||||
Pattern fileContentPattern = Pattern
|
||||
.compile( "alter table.* " + regexpQuote( "default-schema", "my_entity" ) );
|
||||
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
|
||||
assertThat( fileContentMatcher.find(), is( true ) );
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "MyEntity")
|
||||
@Table(name = "my_entity")
|
||||
public static class MyEntity {
|
||||
|
||||
@Id
|
||||
public Integer id;
|
||||
}
|
||||
|
||||
@Entity(name = "MyEntity")
|
||||
@Table(name = "my_entity")
|
||||
public static class MyEntityUpdated {
|
||||
|
||||
@Id
|
||||
public Integer id;
|
||||
|
||||
private String title;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.schemaupdate;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.EnumSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Guillaume Smet
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-12939")
|
||||
@RequiresDialectFeature(DialectChecks.SupportSchemaCreation.class)
|
||||
public class AlterTableQuoteSpecifiedSchemaTest extends AbstractAlterTableQuoteSchemaTest {
|
||||
|
||||
@Override
|
||||
protected void afterSessionFactoryBuilt() {
|
||||
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "DROP TABLE " + quote( "my-schema", "my_entity" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "DROP SCHEMA " + quote( "my-schema" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "CREATE SCHEMA " + quote( "my-schema" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanupTest() {
|
||||
try {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "DROP SCHEMA " + quote( "my-schema" ) )
|
||||
.executeUpdate();
|
||||
} );
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecifiedSchema() throws IOException {
|
||||
File output = File.createTempFile( "update_script", ".sql" );
|
||||
output.deleteOnExit();
|
||||
|
||||
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
|
||||
.applySetting( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString() )
|
||||
.build();
|
||||
|
||||
try {
|
||||
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
|
||||
.addAnnotatedClass( MyEntity.class )
|
||||
.buildMetadata();
|
||||
metadata.validate();
|
||||
|
||||
new SchemaUpdate()
|
||||
.setHaltOnError( true )
|
||||
.setOutputFile( output.getAbsolutePath() )
|
||||
.setDelimiter( ";" )
|
||||
.setFormat( true )
|
||||
.execute( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
|
||||
try {
|
||||
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
|
||||
Pattern fileContentPattern = Pattern.compile( "create table " + regexpQuote( "my-schema", "my_entity" ) );
|
||||
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
|
||||
assertThat( fileContentMatcher.find(), is( true ) );
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
|
||||
ssr = new StandardServiceRegistryBuilder()
|
||||
.applySetting( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString() )
|
||||
.build();
|
||||
try {
|
||||
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
|
||||
.addAnnotatedClass( MyEntityUpdated.class )
|
||||
.buildMetadata();
|
||||
metadata.validate();
|
||||
|
||||
new SchemaUpdate()
|
||||
.setHaltOnError( true )
|
||||
.setOutputFile( output.getAbsolutePath() )
|
||||
.setDelimiter( ";" )
|
||||
.setFormat( true )
|
||||
.execute( EnumSet.of( TargetType.DATABASE, TargetType.SCRIPT ), metadata );
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
|
||||
try {
|
||||
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
|
||||
Pattern fileContentPattern = Pattern.compile( "alter table.* " + regexpQuote( "my-schema", "my_entity" ) );
|
||||
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
|
||||
assertThat( fileContentMatcher.find(), is( true ) );
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "MyEntity")
|
||||
@Table(name = "my_entity", schema = "my-schema")
|
||||
public static class MyEntity {
|
||||
|
||||
@Id
|
||||
public Integer id;
|
||||
}
|
||||
|
||||
@Entity(name = "MyEntity")
|
||||
@Table(name = "my_entity", schema = "my-schema")
|
||||
public static class MyEntityUpdated {
|
||||
|
||||
@Id
|
||||
public Integer id;
|
||||
|
||||
private String title;
|
||||
}
|
||||
}
|
|
@ -151,7 +151,7 @@ public class SqlServerQuoteSchemaTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
try {
|
||||
String fileContent = new String( Files.readAllBytes( output.toPath() ) );
|
||||
Pattern fileContentPattern = Pattern.compile( "alter table .*?\\.\\[my\\-schema\\]\\.\\[my_entity\\]" );
|
||||
Pattern fileContentPattern = Pattern.compile( "alter table \\[my\\-schema\\]\\.\\[my_entity\\]" );
|
||||
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
|
||||
assertThat( fileContentMatcher.find(), is( true ) );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue