Search in sources :

Example 1 with SchemaAlreadyExistsException

use of org.apache.phoenix.schema.SchemaAlreadyExistsException in project phoenix by apache.

the class CreateSchemaIT method testCreateSchema.

@Test
public void testCreateSchema() throws Exception {
    long ts = nextTimestamp();
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts));
    props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(true));
    String ddl = "CREATE SCHEMA TEST_SCHEMA";
    try (Connection conn = DriverManager.getConnection(getUrl(), props);
        HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
        conn.createStatement().execute(ddl);
        assertNotNull(admin.getNamespaceDescriptor("TEST_SCHEMA"));
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute(ddl);
        fail();
    } catch (SchemaAlreadyExistsException e) {
    // expected
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts - 20));
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute(ddl);
        fail();
    } catch (NewerSchemaAlreadyExistsException e) {
    // expected
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 50));
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
        conn.createStatement().execute("CREATE SCHEMA " + SchemaUtil.SCHEMA_FOR_DEFAULT_NAMESPACE);
        fail();
    } catch (SQLException e) {
        assertEquals(SQLExceptionCode.SCHEMA_NOT_ALLOWED.getErrorCode(), e.getErrorCode());
    }
    try {
        conn.createStatement().execute("CREATE SCHEMA " + SchemaUtil.HBASE_NAMESPACE);
        fail();
    } catch (SQLException e) {
        assertEquals(SQLExceptionCode.SCHEMA_NOT_ALLOWED.getErrorCode(), e.getErrorCode());
    }
    conn.close();
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) NewerSchemaAlreadyExistsException(org.apache.phoenix.schema.NewerSchemaAlreadyExistsException) SQLException(java.sql.SQLException) NewerSchemaAlreadyExistsException(org.apache.phoenix.schema.NewerSchemaAlreadyExistsException) SchemaAlreadyExistsException(org.apache.phoenix.schema.SchemaAlreadyExistsException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)1 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)1 NewerSchemaAlreadyExistsException (org.apache.phoenix.schema.NewerSchemaAlreadyExistsException)1 SchemaAlreadyExistsException (org.apache.phoenix.schema.SchemaAlreadyExistsException)1 Test (org.junit.Test)1