import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.h2.tools.RunScript;
import org.junit.Test;
public class TestH2 {
@Test
public void testIndexIssue() throws SQLException, ClassNotFoundException {
Class.forName("org.h2.Driver");
InputStream in = getClass().getResourceAsStream("script.sql");
if (in == null) {
System.out.println("Please add the file script.sql to the classpath, package "
+ getClass().getPackage().getName());
} else {
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL;IGNORECASE=TRUE");
RunScript.execute(conn, new InputStreamReader(in));
ResultSet rs = conn.getMetaData().getIndexInfo(null, null, "test_table", false, true);
while (rs.next()) {
System.out.println(rs.getString("INDEX_NAME"));
}
conn.close();
}
}
}
CREATE TABLE test_table (
id serial,
unique_field bigint unsigned not null,
CONSTRAINT pk_test_table PRIMARY KEY (
id ASC
),
CONSTRAINT uk_test UNIQUE KEY (
unique_field
)
);
/* Comment this out to see the available indexes in the test output */
ALTER TABLE test_table DROP INDEX uk_test;
primary_key_c
uk_test_index_c
I'm using Flyway for migrations with H2 as a test database and MySQL in production. Flyway doesn't abstract SQL language differences but that has been okay for the most part as H2 has been compatible however for this particular issue I haven't found a way to handle this short of writing two different migrations or I wrote a Java based migration that just checks for the use of H2 and replaces "DROP INDEX" with "DROP CONSTRAINT".
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database...@googlegroups.com.
To post to this group, send email to h2-da...@googlegroups.com.
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.