Search in sources :

Example 6 with SchemaUpdate

use of org.hibernate.tool.hbm2ddl.SchemaUpdate in project hibernate-orm by hibernate.

the class QuotedTableNameWithForeignKeysSchemaUpdateTest method setUp.

@Before
public void setUp() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addResource("org/hibernate/test/schemaupdate/UserGroup.hbm.xml").buildMetadata();
        metadata.validate();
        new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Before(org.junit.Before)

Example 7 with SchemaUpdate

use of org.hibernate.tool.hbm2ddl.SchemaUpdate in project hibernate-orm by hibernate.

the class QuotedTableNameWithForeignKeysSchemaUpdateTest method testGeneratingUpdateScript.

@Test
public void testGeneratingUpdateScript() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addResource("org/hibernate/test/schemaupdate/UserGroup.hbm.xml").buildMetadata();
        new SchemaUpdate().execute(EnumSet.of(TargetType.STDOUT), metadata);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 8 with SchemaUpdate

use of org.hibernate.tool.hbm2ddl.SchemaUpdate in project hibernate-orm by hibernate.

the class SqlServerQuoteSchemaTest method test.

@Test
public void test() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE.toString()).build();
    try {
        output.deleteOnExit();
        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 \\[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 .*?\\.\\[my\\-schema\\]\\.\\[my_entity\\]");
        Matcher fileContentMatcher = fileContentPattern.matcher(fileContent.toLowerCase());
        assertThat(fileContentMatcher.find(), is(true));
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : Pattern(java.util.regex.Pattern) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Matcher(java.util.regex.Matcher) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) IOException(java.io.IOException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 9 with SchemaUpdate

use of org.hibernate.tool.hbm2ddl.SchemaUpdate in project hibernate-orm by hibernate.

the class SchemaUpdateFormatterTest method testSetFormat.

@Test
public void testSetFormat() throws Exception {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(Environment.HBM2DDL_AUTO, "none").build();
    try {
        File output = File.createTempFile("update_script", ".sql");
        output.deleteOnExit();
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(TestEntity.class).buildMetadata();
        metadata.validate();
        new SchemaUpdate().setHaltOnError(true).setOutputFile(output.getAbsolutePath()).setDelimiter(DELIMITER).setFormat(true).execute(EnumSet.of(TargetType.SCRIPT), metadata);
        String outputContent = new String(Files.readAllBytes(output.toPath()));
        // Old Macs use \r as a new line delimiter
        outputContent = outputContent.replaceAll("\r", "\n");
        // On Windows, \r\n would become \n\n, so we eliminate duplicates
        outputContent = outputContent.replaceAll("\n\n", "\n");
        Assert.assertTrue(Pattern.compile(AFTER_FORMAT).matcher(outputContent).matches());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) File(java.io.File) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 10 with SchemaUpdate

use of org.hibernate.tool.hbm2ddl.SchemaUpdate in project hibernate-orm by hibernate.

the class ForeignKeyMigrationTest method testMigrationOfForeignKeys.

@Test
@TestForIssue(jiraKey = "HHH-9716")
public // @FailureExpected( jiraKey = "HHH-9716" )
void testMigrationOfForeignKeys() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(Box.class).addAnnotatedClass(Thing.class).buildMetadata();
        metadata.validate();
        // first create the schema...
        new SchemaExport().create(EnumSet.of(TargetType.DATABASE), metadata);
        try {
            // try to update the just created schema
            new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
        } finally {
            // clean up
            new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)25 Test (org.junit.Test)23 MetadataSources (org.hibernate.boot.MetadataSources)20 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)20 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)15 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)15 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)9 File (java.io.File)7 TestForIssue (org.hibernate.testing.TestForIssue)7 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 SchemaValidator (org.hibernate.tool.hbm2ddl.SchemaValidator)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)1 Holder (com.evolveum.midpoint.util.Holder)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1