diff --git a/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java b/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java index 3402470..2c571b0 100644 --- a/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java +++ b/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java @@ -163,6 +163,9 @@ private String getMetaStoreSchemaVersion(Connection metastoreConn) throw new HiveMetaException("Didn't find version data in metastore"); } String currentSchemaVersion = res.getString(1); + if (res.next()) { + throw new HiveMetaException("Multiple versions were found in metastore."); + } return currentSchemaVersion; } catch (SQLException e) { throw new HiveMetaException("Failed to get schema version.", e); @@ -305,6 +308,7 @@ public void doValidate() throws HiveMetaException { System.out.print("Starting metastore validation"); validateSequences(); validateSchemaTables(); + verifySchemaVersion(); System.out.print("Done with metastore validation"); } diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java index 2209c83..ddc705b 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestSchemaTool.java @@ -187,6 +187,38 @@ public void testSchemaInit() throws Exception { } /** + * Test validation for multiple versions + * @throws Exception + */ + public void testVerifySchemaWithMultipleVersions() throws Exception { + schemaTool.doInit(); + + // Test an invalid case with multiple versions + String[] scripts = new String[] { + "insert into VERSION values(100, '2.2.0', 'Hive release version 2.2.0')" + }; + File scriptFile = generateTestScript(scripts); + schemaTool.runBeeLine(scriptFile.getPath()); + + HiveMetaException e = null; + try { + schemaTool.verifySchemaVersion(); + } catch (HiveMetaException hme) { + e = hme; + } + assertNotNull("VerifySchemaVersion should catch HiveMetaException for multiple versions.", e); + assertTrue("Error message should be \"Multiple versions were found in metastore.\"", + e.getMessage().equalsIgnoreCase("Multiple versions were found in metastore.")); + + scripts = new String[] { + "delete from VERSION where VER_ID = 100" + }; + scriptFile = generateTestScript(scripts); + schemaTool.runBeeLine(scriptFile.getPath()); + schemaTool.verifySchemaVersion(); + } + + /** * Test schema upgrade * @throws Exception */