Search in sources :

Example 1 with UpgradeRequiredException

use of org.apache.phoenix.exception.UpgradeRequiredException in project phoenix by apache.

the class UpgradeIT method testUpgradingConnectionBypassesUpgradeRequiredCheck.

@Test
public void testUpgradingConnectionBypassesUpgradeRequiredCheck() throws Exception {
    String tableName = generateUniqueName();
    try (Connection conn = getConnection(false, null)) {
        conn.createStatement().execute("CREATE TABLE " + tableName + " (PK1 VARCHAR NOT NULL, PK2 VARCHAR, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2))");
        final ConnectionQueryServices delegate = conn.unwrap(PhoenixConnection.class).getQueryServices();
        ConnectionQueryServices servicesWithUpgrade = new DelegateConnectionQueryServices(delegate) {

            @Override
            public boolean isUpgradeRequired() {
                return true;
            }
        };
        try (PhoenixConnection phxConn = new PhoenixConnection(servicesWithUpgrade, conn.unwrap(PhoenixConnection.class), HConstants.LATEST_TIMESTAMP)) {
            // Because upgrade is required, this SQL should fail.
            try {
                phxConn.createStatement().executeQuery("SELECT * FROM " + tableName);
                fail("SELECT should have failed with UpgradeRequiredException");
            } catch (UpgradeRequiredException expected) {
            }
            // Marking connection as the one running upgrade should let SQL execute fine.
            phxConn.setRunningUpgrade(true);
            phxConn.createStatement().execute("UPSERT INTO " + tableName + " VALUES ('PK1', 'PK2', 'KV1', 'KV2')");
            phxConn.commit();
            try (ResultSet rs = phxConn.createStatement().executeQuery("SELECT * FROM " + tableName)) {
                assertTrue(rs.next());
                assertFalse(rs.next());
            }
        }
    }
}
Also used : UpgradeRequiredException(org.apache.phoenix.exception.UpgradeRequiredException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) Test(org.junit.Test)

Example 2 with UpgradeRequiredException

use of org.apache.phoenix.exception.UpgradeRequiredException in project phoenix by apache.

the class UpgradeIT method testUpgradeRequiredPreventsSQL.

@Test
public void testUpgradeRequiredPreventsSQL() throws SQLException {
    String tableName = generateUniqueName();
    try (Connection conn = getConnection(false, null)) {
        conn.createStatement().execute("CREATE TABLE " + tableName + " (PK1 VARCHAR NOT NULL, PK2 VARCHAR, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2))");
        final ConnectionQueryServices delegate = conn.unwrap(PhoenixConnection.class).getQueryServices();
        ConnectionQueryServices servicesWithUpgrade = new DelegateConnectionQueryServices(delegate) {

            @Override
            public boolean isUpgradeRequired() {
                return true;
            }
        };
        try (PhoenixConnection phxConn = new PhoenixConnection(servicesWithUpgrade, conn.unwrap(PhoenixConnection.class), HConstants.LATEST_TIMESTAMP)) {
            try {
                phxConn.createStatement().execute("CREATE TABLE " + generateUniqueName() + " (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2))");
                fail("CREATE TABLE should have failed with UpgradeRequiredException");
            } catch (UpgradeRequiredException expected) {
            }
            try {
                phxConn.createStatement().execute("SELECT * FROM " + tableName);
                fail("SELECT should have failed with UpgradeRequiredException");
            } catch (UpgradeRequiredException expected) {
            }
            try {
                phxConn.createStatement().execute("DELETE FROM " + tableName);
                fail("DELETE should have failed with UpgradeRequiredException");
            } catch (UpgradeRequiredException expected) {
            }
            try {
                phxConn.createStatement().execute("CREATE INDEX " + tableName + "_IDX ON " + tableName + " (KV1) INCLUDE (KV2)");
                fail("CREATE INDEX should have failed with UpgradeRequiredException");
            } catch (UpgradeRequiredException expected) {
            }
            try {
                phxConn.createStatement().execute("UPSERT INTO " + tableName + " VALUES ('PK1', 'PK2', 'KV1', 'KV2')");
                fail("UPSERT VALUES should have failed with UpgradeRequiredException");
            } catch (UpgradeRequiredException expected) {
            }
        }
    }
}
Also used : UpgradeRequiredException(org.apache.phoenix.exception.UpgradeRequiredException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) DelegateConnectionQueryServices(org.apache.phoenix.query.DelegateConnectionQueryServices) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)2 UpgradeRequiredException (org.apache.phoenix.exception.UpgradeRequiredException)2 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)2 ConnectionQueryServices (org.apache.phoenix.query.ConnectionQueryServices)2 DelegateConnectionQueryServices (org.apache.phoenix.query.DelegateConnectionQueryServices)2 Test (org.junit.Test)2 ResultSet (java.sql.ResultSet)1