// read schema version from sentry store private String getMetaStoreSchemaVersion(Connection sentryStoreConn) throws SentryUserException { String versionQuery; if (SentrySchemaHelper.getDbCommandParser(dbType).needsQuotedIdentifier()) { versionQuery = "select t.\"SCHEMA_VERSION\" from \"SENTRY_VERSION\" t"; } else { versionQuery = "select t.SCHEMA_VERSION from SENTRY_VERSION t"; } try { Statement stmt = sentryStoreConn.createStatement(); ResultSet res = stmt.executeQuery(versionQuery); if (!res.next()) { throw new SentryUserException("Didn't find version data in sentry store"); } String currentSchemaVersion = res.getString(1); sentryStoreConn.close(); return currentSchemaVersion; } catch (SQLException e) { throw new SentryUserException("Failed to get schema version.", e); } }
// run beeline on the given sentry store scrip, flatten the nested scripts into single file private void runBeeLine(String scriptDir, String scriptFile) throws IOException { NestedScriptParser dbCommandParser = SentrySchemaHelper.getDbCommandParser(dbType); dbCommandParser.setDbOpts(getDbOpts()); // expand the nested script String sqlCommands = buildCommand(dbCommandParser, scriptDir, scriptFile); File tmpFile = File.createTempFile("schematool", ".sql"); tmpFile.deleteOnExit(); // write out the buffer into a file. Add beeline commands for autocommit and close FileWriter fstream = new FileWriter(tmpFile.getPath()); BufferedWriter out = new BufferedWriter(fstream); out.write("!set Silent " + verbose + System.getProperty("line.separator")); out.write("!autocommit on" + System.getProperty("line.separator")); out.write("!set Isolation TRANSACTION_READ_COMMITTED" + System.getProperty("line.separator")); out.write("!set AllowMultiLineCommand false" + System.getProperty("line.separator")); out.write(sqlCommands); out.write("!closeall" + System.getProperty("line.separator")); out.close(); runBeeLine(tmpFile.getPath()); }