-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
UniqueConstraintDropTest.java
180 lines (157 loc) · 5.56 KB
/
UniqueConstraintDropTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* 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.uniqueconstraint;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.EnumSet;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.tool.schema.internal.DefaultSchemaFilter;
import org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl;
import org.hibernate.tool.schema.internal.HibernateSchemaManagementTool;
import org.hibernate.tool.schema.internal.IndividuallySchemaMigratorImpl;
import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
import org.hibernate.tool.schema.spi.ExceptionHandler;
import org.hibernate.tool.schema.spi.ExecutionOptions;
import org.hibernate.tool.schema.spi.SchemaManagementTool;
import org.hibernate.tool.schema.spi.ScriptTargetOutput;
import org.hibernate.tool.schema.spi.TargetDescriptor;
import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
/**
* @author Andrea Boriero
*/
public class UniqueConstraintDropTest {
private File output;
private MetadataImplementor metadata;
private StandardServiceRegistry ssr;
private HibernateSchemaManagementTool tool;
private ExecutionOptions options;
@Before
public void setUp() throws Exception {
output = File.createTempFile( "update_script", ".sql" );
output.deleteOnExit();
ssr = new StandardServiceRegistryBuilder()
.applySetting( Environment.HBM2DDL_AUTO, "none" )
.applySetting( Environment.FORMAT_SQL, "false" )
.applySetting( Environment.SHOW_SQL, "true" )
.build();
metadata = (MetadataImplementor) new MetadataSources( ssr )
.addResource( "org/hibernate/test/schemaupdate/uniqueconstraint/TestEntity.hbm.xml" )
.buildMetadata();
metadata.validate();
tool = (HibernateSchemaManagementTool) ssr.getService( SchemaManagementTool.class );
final Map configurationValues = ssr.getService( ConfigurationService.class ).getSettings();
options = new ExecutionOptions() {
@Override
public boolean shouldManageNamespaces() {
return true;
}
@Override
public Map getConfigurationValues() {
return configurationValues;
}
@Override
public ExceptionHandler getExceptionHandler() {
return ExceptionHandlerLoggedImpl.INSTANCE;
}
};
}
@After
public void tearDown() {
StandardServiceRegistryBuilder.destroy( ssr );
}
@Test
@TestForIssue(jiraKey = "HHH-11236")
public void testUniqueConstraintIsGenerated() throws Exception {
new IndividuallySchemaMigratorImpl( tool, DefaultSchemaFilter.INSTANCE )
.doMigration(
metadata,
options,
new TargetDescriptorImpl()
);
if ( getDialect() instanceof MySQLDialect ) {
assertThat(
"The test_entity_item table unique constraint has not been dropped",
checkDropIndex( "test_entity_item", "item" ),
is( true )
);
}
else {
assertThat(
"The test_entity_item table unique constraint has not been dropped",
checkDropConstraint( "test_entity_item", "item" ),
is( true )
);
}
}
protected Dialect getDialect() {
return ssr.getService( JdbcEnvironment.class ).getDialect();
}
private boolean checkDropConstraint(String tableName, String columnName) throws IOException {
boolean matches = false;
String regex = getDialect().getAlterTableString( tableName ) + " drop constraint";
if ( getDialect().supportsIfExistsBeforeConstraintName() ) {
regex += " if exists";
}
regex += " uk_(.)*";
if ( getDialect().supportsIfExistsAfterConstraintName() ) {
regex += " if exists";
}
return isMatching( matches, regex );
}
private boolean checkDropIndex(String tableName, String columnName) throws IOException {
boolean matches = false;
String regex = "alter table " + tableName + " drop index";
if ( getDialect().supportsIfExistsBeforeConstraintName() ) {
regex += " if exists";
}
regex += " uk_(.)*";
if ( getDialect().supportsIfExistsAfterConstraintName() ) {
regex += " if exists";
}
return isMatching( matches, regex );
}
private boolean isMatching(boolean matches, String regex) throws IOException {
final String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase();
final String[] split = fileContent.split( System.lineSeparator() );
Pattern p = Pattern.compile( regex );
for ( String line : split ) {
final Matcher matcher = p.matcher( line );
if ( matcher.matches() ) {
matches = true;
}
}
return matches;
}
private class TargetDescriptorImpl implements TargetDescriptor {
public EnumSet<TargetType> getTargetTypes() {
return EnumSet.of( TargetType.SCRIPT );
}
@Override
public ScriptTargetOutput getScriptTargetOutput() {
return new ScriptTargetOutputToFile( output, Charset.defaultCharset().name() );
}
}
}